Skip to content

Commit b867e36

Browse files
committed
update: add some new methods, add new tests
1 parent b2d5c50 commit b867e36

File tree

7 files changed

+90
-32
lines changed

7 files changed

+90
-32
lines changed

src/Arr/ArrObject.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\Stdlib\Arr;
4+
5+
use Toolkit\Stdlib\Obj\DataObject;
6+
7+
/**
8+
* class ArrObject
9+
*
10+
* @author inhere
11+
*/
12+
class ArrObject extends DataObject
13+
{
14+
15+
}

src/Obj/DataObject.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
use JsonSerializable;
1414
use Toolkit\Stdlib\Helper\JsonHelper;
1515
use Toolkit\Stdlib\Obj;
16+
use Toolkit\Stdlib\Str;
1617
use UnexpectedValueException;
1718
use function in_array;
1819

1920
/**
20-
* Class ConfigObject
21+
* Class DataObject
2122
*
2223
* @package Toolkit\Stdlib\Obj
2324
*/
@@ -28,7 +29,7 @@ class DataObject extends ArrayObject implements JsonSerializable
2829
*
2930
* @return static
3031
*/
31-
public static function new(array $data = []): self
32+
public static function new(array $data = []): static
3233
{
3334
return new static($data);
3435
}
@@ -152,6 +153,20 @@ public function getString(string $key, string $default = ''): string
152153
return $default;
153154
}
154155

156+
/**
157+
* @param string $key
158+
* @param array $default
159+
* @param string $sep
160+
*
161+
* @return array
162+
*/
163+
public function getStrings(string $key, array $default = [], string $sep = ','): array
164+
{
165+
$str = $this->getString($key);
166+
167+
return $str ? Str::toArray($str, $sep) : $default;
168+
}
169+
155170
/**
156171
* @param string $key
157172
*

src/Std/Collection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ public function all(): array
250250
return $this->data;
251251
}
252252

253+
/**
254+
* @return array
255+
*/
256+
public function getData(): array
257+
{
258+
return $this->data;
259+
}
260+
253261
/**
254262
* @return array
255263
*/

src/Str/Traits/StringConvertTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public static function explode(string $str, string $separator = '.', int $limit
185185
*/
186186
public static function toArray(string $str, string $sep = ',', int $limit = 0): array
187187
{
188-
return self::toNoEmptyArray($str, $sep, $limit);;
188+
return self::toNoEmptyArray($str, $sep, $limit);
189189
}
190190

191191
/**

src/Type.php

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -112,34 +112,15 @@ public static function getDefault(string $type): float|bool|int|array|string|nul
112112
*/
113113
public static function fmtValue(string $type, mixed $value): mixed
114114
{
115-
switch ($type) {
116-
case self::INT:
117-
case self::INTEGER:
118-
$value = (int)$value;
119-
break;
120-
case self::BOOL:
121-
case self::BOOLEAN:
122-
if (is_string($value)) {
123-
$value = Str::toBool2($value);
124-
} else {
125-
$value = (bool)$value;
126-
}
127-
break;
128-
case self::FLOAT:
129-
$value = (float)$value;
130-
break;
131-
case self::DOUBLE:
132-
$value = (double)$value;
133-
break;
134-
case self::STRING:
135-
$value = (string)$value;
136-
break;
137-
case self::ARRAY:
138-
$value = (array)$value;
139-
break;
140-
}
141-
142-
return $value;
115+
return match ($type) {
116+
self::INT, self::INTEGER => (int)$value,
117+
self::BOOL, self::BOOLEAN => is_string($value) ? Str::toBool2($value) : (bool)$value,
118+
self::FLOAT => (float)$value,
119+
self::DOUBLE => (double)$value,
120+
self::STRING => (string)$value,
121+
self::ARRAY => (array)$value,
122+
default => $value
123+
};
143124
}
144125

145126
/**

src/Util/Stream/DataStream.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function new(array|Traversable $data): static
4646
public static function empty(): self
4747
{
4848
if (!self::$emptyObj) {
49-
self::$emptyObj = new self();
49+
self::$emptyObj = new static();
5050
}
5151

5252
return self::$emptyObj;
@@ -591,6 +591,14 @@ public function getArray(): array
591591
return $this->getArrayCopy();
592592
}
593593

594+
/**
595+
* close and clear data.
596+
*/
597+
public function close(): void
598+
{
599+
// clear all data.
600+
}
601+
594602
/**
595603
* @return int
596604
*/

test/TypeTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Toolkit\StdlibTest;
4+
5+
use Toolkit\Stdlib\Type;
6+
7+
/**
8+
* class TypeTest
9+
*
10+
* @author inhere
11+
*/
12+
class TypeTest extends BaseLibTestCase
13+
{
14+
public function testType_fmtValue(): void
15+
{
16+
$tests = [
17+
['23', Type::INT, 23],
18+
['23', Type::INTEGER, 23],
19+
['23', 'unknown', '23'],
20+
['true', Type::BOOL, true],
21+
['yes', Type::BOOL, true],
22+
['no', Type::BOOL, false],
23+
['true', Type::STRING, 'true'],
24+
['no', Type::STRING, 'no'],
25+
];
26+
27+
foreach ($tests as [$value, $type, $want]) {
28+
$this->assertEquals($want, Type::fmtValue($type, $value));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)