Skip to content

Commit

Permalink
feat: add some new base object class, add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 10, 2023
1 parent 27743c2 commit 088ce28
Show file tree
Hide file tree
Showing 10 changed files with 635 additions and 100 deletions.
25 changes: 12 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.0/phpunit.xsd" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<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">
<testsuites>
<testsuite name="Php Library Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
59 changes: 59 additions & 0 deletions src/Arr/Traits/ArrayConvertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,63 @@ public static function toStringV2(iterable $data): string
}
return '{' . implode(', ', $strings) . '}';
}

/**
* simple format array data to string.
*
* @param array $data
* @param int $depth
*
* @return string
*/
public static function toStringV3(array $data, int $depth = 3): string
{
return self::doFormat($data, $depth);
}

private static function doFormat(array $data, int $depth = 3, int $innerDepth = 1): string
{
if (!$data) {
return '{}';
}

if ($depth === 0) {
return json_encode($data, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

$count = count($data);
$isList = isset($data[0], $data[$count - 1]);

// number list
$isNumbers = $isList && is_int($data[0]) && is_int($data[$count - 1]);
if ($isNumbers) {
return '[' . implode(',', $data) . "]";
}

$strings = ['{'];
$indents = str_repeat(' ', $innerDepth * 2);

foreach ($data as $key => $value) {
$sfx = '';
if ($value === null) {
$str = 'null';
} elseif (is_bool($value)) {
$str = $value ? 'true' : 'false';
} elseif (is_scalar($value)) {
$str = (string)$value;
} elseif (is_array($value)) {
$str = self::doFormat($value, $depth - 1, $innerDepth +1);
$sfx = " #len=" . count($value);
} else {
$str = json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}

$keyString = $isList ? '' : "$key: ";
$strings[] = sprintf('%s%s%s,%s', $indents, $keyString, $str, $sfx);
}

$strings[] = str_repeat(' ', ($innerDepth-1) * 2) . '}';
return implode("\n", $strings);
}

}
34 changes: 16 additions & 18 deletions src/Arr/Traits/ArrayValueGetSetTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use ArrayAccess;
use Toolkit\Stdlib\Php;
use Traversable;
use function array_filter;
use function array_shift;
use function count;
Expand All @@ -32,9 +31,9 @@ trait ArrayValueGetSetTrait
/**
* Add an element to an array using "dot" notation if it doesn't exist.
*
* @param array $array
* @param array $array
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @return array
*/
Expand Down Expand Up @@ -81,9 +80,9 @@ public static function get(ArrayAccess|array $array, string $key, mixed $default
* Set an array item to a given value using "dot" notation.
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param array $array
* @param string $key
* @param mixed $value
* @param mixed $value
*
* @return array
*/
Expand Down Expand Up @@ -114,9 +113,8 @@ public static function set(array &$array, string $key, mixed $value): array
/**
* Get Multi - 获取多个, 可以设置默认值
*
* @param array $data array data
* @param array $needKeys
* $needKeys = [
* @param array $data array data
* @param array $needKeys = [
* 'name',
* 'password',
* 'status' => '1'
Expand All @@ -131,7 +129,7 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =

foreach ($needKeys as $key => $default) {
if (is_int($key)) {
$key = $default;
$key = $default;
$default = null;
}

Expand Down Expand Up @@ -163,14 +161,14 @@ public static function gets(array &$data, array $needKeys = [], bool $unsetKey =
* Get data from array or object by path.
* Example: `DataCollector::getByPath($array, 'foo.bar.yoo')` equals to $array['foo']['bar']['yoo'].
*
* @param Traversable|array $data An array or object to get value.
* @param string $path The key path.
* @param iterable $data An array or object to get value.
* @param string $path The key path.
* @param mixed|null $default
* @param string $separator Separator of paths.
* @param string $separator Separator of paths.
*
* @return mixed Found value, null if not exists.
*/
public static function getByPath(Traversable|array $data, string $path, mixed $default = null, string $separator = '.'): mixed
public static function getByPath(iterable $data, string $path, mixed $default = null, string $separator = '.'): mixed
{
if (isset($data[$path])) {
return $data[$path];
Expand Down Expand Up @@ -224,12 +222,12 @@ public static function getValueByNodes(array $data, array $nodes, mixed $default
/**
* setByPath
*
* @param ArrayAccess|array &$data
* @param string $path
* @param mixed $value
* @param string $separator
* @param array $data
* @param string $path
* @param mixed $value
* @param string $separator
*/
public static function setByPath(ArrayAccess|array &$data, string $path, mixed $value, string $separator = '.'): void
public static function setByPath(array &$data, string $path, mixed $value, string $separator = '.'): void
{
if (!str_contains($path, $separator)) {
$data[$path] = $value;
Expand Down
34 changes: 2 additions & 32 deletions src/Obj/AbstractObj.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,11 @@
<?php declare(strict_types=1);
/**
* This file is part of toolkit/stdlib.
*
* @author https://github.com/inhere
* @link https://github.com/php-toolkit/stdlib
* @license MIT
*/

namespace Toolkit\Stdlib\Obj;

use Toolkit\Stdlib\Obj;
use Toolkit\Stdlib\Obj\Traits\QuickInitTrait;

/**
* Class AbstractObj
*
* @package Toolkit\Stdlib\Obj
*/
abstract class AbstractObj
abstract class AbstractObj extends BaseObject
{
use QuickInitTrait;

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
Obj::init($this, $config);
}

/**
* @return array
*/
public function toArray(): array
{
return Obj::toArray($this, false, false);
}
}
}
11 changes: 11 additions & 0 deletions src/Obj/BaseObj.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types=1);

namespace Toolkit\Stdlib\Obj;

/**
* Class BaseObj
*/
abstract class BaseObj extends BaseObject
{

}
Loading

0 comments on commit 088ce28

Please sign in to comment.