Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 088ce28

Browse files
committed
feat: add some new base object class, add some tests
1 parent 27743c2 commit 088ce28

File tree

10 files changed

+635
-100
lines changed

10 files changed

+635
-100
lines changed

‎phpunit.xml

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="test/bootstrap.php" colors="true"
3-
stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" cacheDirectory=".phpunit.cache"
4-
backupStaticProperties="false">
5-
<testsuites>
6-
<testsuite name="Php Library Test Suite">
7-
<directory>test</directory>
8-
</testsuite>
9-
</testsuites>
10-
<coverage>
11-
<include>
12-
<directory suffix=".php">src</directory>
13-
</include>
14-
</coverage>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="test/bootstrap.php" colors="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<testsuites>
4+
<testsuite name="Php Library Test Suite">
5+
<directory>test</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage/>
9+
<source>
10+
<include>
11+
<directory suffix=".php">src</directory>
12+
</include>
13+
</source>
1514
</phpunit>

‎src/Arr/Traits/ArrayConvertTrait.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,63 @@ public static function toStringV2(iterable $data): string
323323
}
324324
return '{' . implode(', ', $strings) . '}';
325325
}
326+
327+
/**
328+
* simple format array data to string.
329+
*
330+
* @param array $data
331+
* @param int $depth
332+
*
333+
* @return string
334+
*/
335+
public static function toStringV3(array $data, int $depth = 3): string
336+
{
337+
return self::doFormat($data, $depth);
338+
}
339+
340+
private static function doFormat(array $data, int $depth = 3, int $innerDepth = 1): string
341+
{
342+
if (!$data) {
343+
return '{}';
344+
}
345+
346+
if ($depth === 0) {
347+
return json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
348+
}
349+
350+
$count = count($data);
351+
$isList = isset($data[0], $data[$count - 1]);
352+
353+
// number list
354+
$isNumbers = $isList && is_int($data[0]) && is_int($data[$count - 1]);
355+
if ($isNumbers) {
356+
return '[' . implode(',', $data) . "]";
357+
}
358+
359+
$strings = ['{'];
360+
$indents = str_repeat('', $innerDepth * 2);
361+
362+
foreach ($data as $key => $value) {
363+
$sfx = '';
364+
if ($value === null) {
365+
$str = 'null';
366+
} elseif (is_bool($value)) {
367+
$str = $value ? 'true' : 'false';
368+
} elseif (is_scalar($value)) {
369+
$str = (string)$value;
370+
} elseif (is_array($value)) {
371+
$str = self::doFormat($value, $depth - 1, $innerDepth +1);
372+
$sfx = " #len=" . count($value);
373+
} else {
374+
$str = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
375+
}
376+
377+
$keyString = $isList ? '' : "$key: ";
378+
$strings[] = sprintf('%s%s%s,%s', $indents, $keyString, $str, $sfx);
379+
}
380+
381+
$strings[] = str_repeat('', ($innerDepth-1) * 2) . '}';
382+
return implode("\n", $strings);
383+
}
384+
326385
}

‎src/Arr/Traits/ArrayValueGetSetTrait.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use ArrayAccess;
1313
use Toolkit\Stdlib\Php;
14-
use Traversable;
1514
use function array_filter;
1615
use function array_shift;
1716
use function count;
@@ -32,9 +31,9 @@ trait ArrayValueGetSetTrait
3231
/**
3332
* Add an element to an array using "dot" notation if it doesn't exist.
3433
*
35-
* @param array $array
34+
* @param array $array
3635
* @param string $key
37-
* @param mixed $value
36+
* @param mixed $value
3837
*
3938
* @return array
4039
*/
@@ -81,9 +80,9 @@ public static function get(ArrayAccess|array $array, string $key, mixed $default
8180
* Set an array item to a given value using "dot" notation.
8281
* If no key is given to the method, the entire array will be replaced.
8382
*
84-
* @param array $array
83+
* @param array $array
8584
* @param string $key
86-
* @param mixed $value
85+
* @param mixed $value
8786
*
8887
* @return array
8988
*/
@@ -114,9 +113,8 @@ public static function set(array &$array, string $key, mixed $value): array
114113
/**
115114
* Get Multi - 获取多个, 可以设置默认值
116115
*
117-
* @param array $data array data
118-
* @param array $needKeys
119-
* $needKeys = [
116+
* @param array $data array data
117+
* @param array $needKeys = [
120118
* 'name',
121119
* 'password',
122120
* 'status' => '1'
@@ -131,7 +129,7 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
131129

132130
foreach ($needKeys as $key => $default) {
133131
if (is_int($key)) {
134-
$key = $default;
132+
$key = $default;
135133
$default = null;
136134
}
137135

@@ -163,14 +161,14 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
163161
* Get data from array or object by path.
164162
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
165163
*
166-
* @param Traversable|array $data An array or object to get value.
167-
* @param string $path The key path.
164+
* @param iterable $data An array or object to get value.
165+
* @param string $path The key path.
168166
* @param mixed|null $default
169-
* @param string $separator Separator of paths.
167+
* @param string $separator Separator of paths.
170168
*
171169
* @return mixed Found value, null if not exists.
172170
*/
173-
public static function getByPath(Traversable|array $data, string $path, mixed $default = null, string $separator = '.'): mixed
171+
public static function getByPath(iterable $data, string $path, mixed $default = null, string $separator = '.'): mixed
174172
{
175173
if (isset($data[$path])) {
176174
return $data[$path];
@@ -224,12 +222,12 @@ public static function getValueByNodes(array $data, array $nodes, mixed $default
224222
/**
225223
* setByPath
226224
*
227-
* @param ArrayAccess|array &$data
228-
* @param string $path
229-
* @param mixed $value
230-
* @param string $separator
225+
* @param array $data
226+
* @param string $path
227+
* @param mixed $value
228+
* @param string $separator
231229
*/
232-
public static function setByPath(ArrayAccess|array &$data, string $path, mixed $value, string $separator = '.'): void
230+
public static function setByPath(array &$data, string $path, mixed $value, string $separator = '.'): void
233231
{
234232
if (!str_contains($path, $separator)) {
235233
$data[$path] = $value;

‎src/Obj/AbstractObj.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,11 @@
11
<?php declare(strict_types=1);
2-
/**
3-
* This file is part of toolkit/stdlib.
4-
*
5-
* @author https://github.com/inhere
6-
* @link https://github.com/php-toolkit/stdlib
7-
* @license MIT
8-
*/
92

103
namespace Toolkit\Stdlib\Obj;
114

12-
use Toolkit\Stdlib\Obj;
13-
use Toolkit\Stdlib\Obj\Traits\QuickInitTrait;
14-
155
/**
166
* Class AbstractObj
17-
*
18-
* @package Toolkit\Stdlib\Obj
197
*/
20-
abstract class AbstractObj
8+
abstract class AbstractObjextends BaseObject
219
{
22-
use QuickInitTrait;
23-
24-
/**
25-
* Class constructor.
26-
*
27-
* @param array $config
28-
*/
29-
public function __construct(array $config = [])
30-
{
31-
Obj::init($this, $config);
32-
}
3310

34-
/**
35-
* @return array
36-
*/
37-
public function toArray(): array
38-
{
39-
return Obj::toArray($this, false, false);
40-
}
41-
}
11+
}

‎src/Obj/BaseObj.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Obj;
4+
5+
/**
6+
* Class BaseObj
7+
*/
8+
abstract class BaseObj extends BaseObject
9+
{
10+
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /