Skip to content

Commit 6b825e7

Browse files
committed
up: update some method for object helper
1 parent d182614 commit 6b825e7

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/Obj/AbstractObj.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Toolkit\Stdlib\Obj;
1313
use Toolkit\Stdlib\Obj\Traits\QuickInitTrait;
14-
use function get_object_vars;
1514

1615
/**
1716
* Class AbstractObj
@@ -37,6 +36,6 @@ public function __construct(array $config = [])
3736
*/
3837
public function toArray(): array
3938
{
40-
return get_object_vars($this);
39+
return Obj::toArray($this);
4140
}
4241
}

src/Obj/DataObject.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,22 @@ public function getSubObject(string $key, string $dataClass = ''): self
227227
}
228228

229229
/**
230-
* @param array $fields Restrict to return only these fields, and return all of them if it is empty
230+
* @param array $fields
231231
*
232232
* @return array
233+
* @deprecated please use getMulti()
233234
*/
234235
public function getSome(array $fields = []): array
236+
{
237+
return $this->getMulti($fields);
238+
}
239+
240+
/**
241+
* @param array $fields Restrict to return only these fields, and return all of them if it is empty
242+
*
243+
* @return array
244+
*/
245+
public function getMulti(array $fields = []): array
235246
{
236247
$data = [];
237248
foreach ($this->getArrayCopy() as $key => $value) {
@@ -253,12 +264,25 @@ public function toArray(): array
253264
return $this->getArrayCopy();
254265
}
255266

267+
/**
268+
* @return array
269+
*/
270+
public function getKeys(): array
271+
{
272+
$keys = [];
273+
foreach ($this as $key => $val) {
274+
$keys[] = $key;
275+
}
276+
277+
return $keys;
278+
}
279+
256280
/**
257281
* @return string
258282
*/
259283
public function toString(): string
260284
{
261-
return JsonHelper::enc($this->getArrayCopy(), JSON_THROW_ON_ERROR|JSON_UNESCAPED_SLASHES);
285+
return JsonHelper::enc($this->getArrayCopy(), JSON_UNESCAPED_SLASHES);
262286
}
263287

264288
/**

src/Obj/ObjectHelper.php

+10-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use function base64_encode;
2626
use function basename;
2727
use function dirname;
28+
use function get_object_vars;
2829
use function gettype;
2930
use function gzcompress;
3031
use function gzuncompress;
@@ -156,31 +157,26 @@ public static function decode(string $txt, bool|array $allowedClasses): mixed
156157
}
157158

158159
/**
159-
* php对象转换成为数组
160+
* PHP对象转换成为数组
160161
*
161-
* @param Traversable|array $data
162+
* @param object $data
162163
* @param bool $recursive
163164
*
164165
* @return array
165166
*/
166-
public static function toArray(Traversable|array $data, bool $recursive = false): array
167+
public static function toArray(object $data, bool $recursive = false): array
167168
{
168-
$arr = [];
169-
170-
// Ensure the input data is an array.
171-
if (is_object($data)) {
172-
if ($data instanceof Traversable) {
173-
$arr = iterator_to_array($data);
174-
} elseif (method_exists($data, 'toArray')) {
175-
$arr = $data->toArray();
176-
}
169+
if ($data instanceof Traversable) {
170+
$arr = iterator_to_array($data);
171+
} elseif (method_exists($data, 'toArray')) {
172+
$arr = $data->toArray();
177173
} else {
178-
$arr = (array)$data;
174+
$arr = get_object_vars($data);
179175
}
180176

181177
if ($recursive) {
182178
foreach ($arr as $key => $value) {
183-
if (is_array($value) || is_object($value)) {
179+
if (is_object($value)) {
184180
$arr[$key] = static::toArray($value, $recursive);
185181
}
186182
}

0 commit comments

Comments
 (0)