Skip to content

Commit f443e20

Browse files
committed
Remove ArrayOfTypedValues and its references; refactor types and tests to align with new ArrayOfObjects implementation.
1 parent b49ede0 commit f443e20

File tree

10 files changed

+227
-129
lines changed

10 files changed

+227
-129
lines changed

src/Abstract/Array/ArrayTypeInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @template TItem
2828
*
2929
* @extends IteratorAggregate<int, TItem>
30+
* @extends ArrayOfObjectsAndUndefinedInterface<TItem>
3031
*
3132
* @psalm-immutable
3233
*/

src/Abstract/Primitive/PrimitiveTypeInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpTypedValues\Abstract\Primitive;
66

7+
use JsonSerializable;
78
use PhpTypedValues\Abstract\TypeInterface;
89
use PhpTypedValues\Exception\TypeException;
910
use PhpTypedValues\Undefined\Alias\Undefined;
@@ -22,7 +23,7 @@
2223
*
2324
* @psalm-immutable
2425
*/
25-
interface PrimitiveTypeInterface extends TypeInterface
26+
interface PrimitiveTypeInterface extends TypeInterface, JsonSerializable
2627
{
2728
/**
2829
* Create an instance from a validated string representation.
@@ -44,4 +45,13 @@ public function toString(): string;
4445
* Alias of {@see toString} for convenient casting.
4546
*/
4647
public function __toString(): string;
48+
49+
/**
50+
* JSON representation of the value.
51+
*
52+
* Marked as mutation-free so Psalm treats calls as pure in immutable contexts.
53+
*
54+
* @psalm-mutation-free
55+
*/
56+
public function jsonSerialize(): mixed;
4757
}

src/Array/ArrayOfObjects.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace PhpTypedValues\Array;
66

7-
use IteratorAggregate;
87
use JsonSerializable;
98
use PhpTypedValues\Abstract\Array\ArrayType;
10-
use PhpTypedValues\Abstract\Array\ArrayTypeInterface;
119
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
1210
use PhpTypedValues\Exception\ArrayTypeException;
1311
use PhpTypedValues\Undefined\Alias\Undefined;
@@ -21,33 +19,36 @@
2119
*
2220
* @template TItem of PrimitiveType
2321
*
24-
* @implements IteratorAggregate<int, TItem>
25-
*
26-
* @template-implements ArrayTypeInterface<TItem>
22+
* @template-extends ArrayType<TItem>
2723
*
2824
* @psalm-immutable
2925
*/
3026
readonly class ArrayOfObjects extends ArrayType
3127
{
28+
/**
29+
* @var list<TItem>
30+
*/
31+
private array $value;
32+
3233
/**
3334
* @param list<TItem> $value
3435
*
3536
* @throws ArrayTypeException
3637
*/
37-
public function __construct(private array $value)
38+
public function __construct(array $value)
3839
{
3940
foreach ($value as $item) {
4041
if (!is_object($item)) {
4142
throw new ArrayTypeException('Expected array of Object instances');
4243
}
4344
}
45+
46+
$this->value = $value;
4447
}
4548

4649
/**
4750
* @param list<mixed> $value
4851
*
49-
* @return ArrayOfObjects<TItem>
50-
*
5152
* @throws ArrayTypeException
5253
*/
5354
public static function fromArray(array $value): static
@@ -59,8 +60,6 @@ public static function fromArray(array $value): static
5960
/**
6061
* @param list<mixed> $value
6162
*
62-
* @return ArrayOfObjects<TItem>
63-
*
6463
* @throws ArrayTypeException
6564
*/
6665
public static function tryFromArray(array $value): static
@@ -74,7 +73,7 @@ public static function tryFromArray(array $value): static
7473
}
7574
}
7675

77-
/** @var list<TItem> $value */
76+
/** @var list<TItem> $valueWithUndefined */
7877
return new static($valueWithUndefined);
7978
}
8079

@@ -120,6 +119,7 @@ public function toArray(): array
120119
if (!$item instanceof JsonSerializable) {
121120
throw new ArrayTypeException('Conversion to array of scalars failed, should implement JsonSerializable interface');
122121
}
122+
123123
$result[] = $item->jsonSerialize();
124124
}
125125

src/Array/ArrayOfTypedValues.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/Usage/Composite/Composite.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
echo $test->getFirstName()->toString() . PHP_EOL;
4444
echo $test->getHeight()->toString() . PHP_EOL;
4545
$nickNames = $test->getNickNames();
46-
echo $nickNames->isUndefined() . ' ' . $nickNames->isEmpty() . ' ' . $nickNames->hasUndefined() . ' ' . $nickNames->count() . PHP_EOL;
46+
echo ($nickNames->isUndefined() ? 'true' : 'false')
47+
. ' ' . ($nickNames->isEmpty() ? 'true' : 'false')
48+
. ' ' . ($nickNames->hasUndefined() ? 'true' : 'false')
49+
. ' ' . $nickNames->count() . PHP_EOL;
4750
echo json_encode($nickNames->toArray(), JSON_THROW_ON_ERROR) . PHP_EOL;
4851
echo json_encode($test, JSON_THROW_ON_ERROR) . PHP_EOL;

src/Usage/Example/WithArrays.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,6 @@ public function getFirstName(): StringNonEmpty|Undefined
109109
return $this->firstName;
110110
}
111111

112-
/**
113-
* @return ArrayOfObjects<TNickNames>
114-
*/
115112
public function getNickNames(): ArrayOfObjects
116113
{
117114
return $this->nickNames;

src/Usage/Primitive/Array.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<?php
22

3-
namespace App\Usage\Primitive;
3+
namespace PhpTypedValues\Usage\Primitive;
44

55
require_once 'vendor/autoload.php';
66

77
use const JSON_THROW_ON_ERROR;
88
use const PHP_EOL;
99

1010
use PhpTypedValues\Array\ArrayOfObjects;
11+
use PhpTypedValues\Bool\Alias\Boolean;
1112
use PhpTypedValues\Exception\ArrayTypeException;
1213
use PhpTypedValues\Integer\IntegerNonNegative;
1314
use PhpTypedValues\Undefined\Alias\Undefined;
1415
use PhpTypedValues\Usage\Example\OptionalFail;
1516

17+
use function count;
18+
1619
/**
1720
* Array.
1821
*/
@@ -42,9 +45,9 @@
4245
],
4346
);
4447
echo $collection->count() . PHP_EOL;
45-
echo $collection->hasUndefined() ? 'true' : 'false' . PHP_EOL;
46-
echo $collection->isEmpty() ? 'true' : 'false' . PHP_EOL;
47-
echo $collection->isUndefined() ? 'true' : 'false' . PHP_EOL;
48+
echo Boolean::fromBool($collection->hasUndefined())->toString() . PHP_EOL;
49+
echo Boolean::fromBool($collection->isEmpty())->toString() . PHP_EOL;
50+
echo Boolean::fromBool($collection->isUndefined())->toString() . PHP_EOL;
4851

4952
foreach ($collection->value() as $item) {
5053
if (!$item instanceof Undefined) {
@@ -53,3 +56,7 @@
5356
echo 'Undefined' . PHP_EOL;
5457
}
5558
}
59+
60+
// Demonstrate usage of getDefinedItems() to exclude Undefined values
61+
$defined = $collection->getDefinedItems();
62+
echo 'Defined items count: ' . count($defined) . PHP_EOL;

tests/Unit/Abstract/Primitive/PrimitiveTypeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
88
use PhpTypedValues\Exception\TypeException;
9+
use stdClass;
10+
use Stringable;
911

1012
use function is_resource;
1113

0 commit comments

Comments
 (0)