Skip to content

Commit 40630ac

Browse files
committed
New ArrayOfObjects type
Remove `ArrayOfStrings` and related test; refactor types and examples to align with `PrimitiveType` namespace changes.
1 parent 537f9a5 commit 40630ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+679
-362
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use TypedValues\Integer\IntegerPositive;
3939
$id = IntegerPositive::fromString('123');
4040
```
4141

42-
Instead of spreading validation across an application:
42+
Instead of spreading validation across an application
4343

4444
```php
4545
$id = (int) '123';

src/Abstract/Array/ArrayType.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,23 @@
44

55
namespace PhpTypedValues\Abstract\Array;
66

7-
use IteratorAggregate;
8-
use JsonSerializable;
9-
107
/**
118
* Base implementation for array typed values.
129
*
13-
* Provides an immutable, iterable, and JSON‑serializable collection of
14-
* typed items. Concrete implementations define item validation and
15-
* factory behavior.
10+
* Provides an immutable, iterable, countable, and JSON‑serializable
11+
* collection of typed items. Concrete implementations define item
12+
* validation and factory behavior.
1613
*
1714
* @internal
1815
*
1916
* @psalm-internal PhpTypedValues
2017
*
2118
* @template TItem
2219
*
23-
* @implements IteratorAggregate<int, TItem>
24-
*
2520
* @template-implements ArrayTypeInterface<TItem>
2621
*
2722
* @psalm-immutable
2823
*/
29-
abstract readonly class ArrayType implements ArrayTypeInterface, IteratorAggregate, JsonSerializable
24+
abstract readonly class ArrayType implements ArrayTypeInterface
3025
{
3126
}

src/Abstract/Array/ArrayTypeInterface.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,67 @@
44

55
namespace PhpTypedValues\Abstract\Array;
66

7-
use PhpTypedValues\Undefined\Alias\Undefined;
7+
use Countable;
8+
use IteratorAggregate;
9+
use JsonSerializable;
10+
use PhpTypedValues\Abstract\Shared\ArrayOfObjectsAndUndefinedInterface;
11+
use PhpTypedValues\Abstract\Shared\IsEmptyInterface;
12+
use PhpTypedValues\Abstract\Shared\IsUndefinedInterface;
13+
use PhpTypedValues\Abstract\TypeInterface;
14+
use PhpTypedValues\Exception\ArrayTypeException;
815

916
/**
1017
* Contract for array typed values.
1118
*
1219
* Represents a read‑only collection of typed items with factory helpers
1320
* to construct the collection from raw arrays. Implementations are
14-
* immutable and iterable.
21+
* immutable, iterable, countable, and serializable.
1522
*
1623
* @internal
1724
*
1825
* @psalm-internal PhpTypedValues
1926
*
2027
* @template TItem
2128
*
29+
* @extends IteratorAggregate<int, TItem>
30+
*
2231
* @psalm-immutable
2332
*/
24-
interface ArrayTypeInterface
33+
interface ArrayTypeInterface extends TypeInterface, JsonSerializable, IteratorAggregate, Countable, IsEmptyInterface, IsUndefinedInterface, ArrayOfObjectsAndUndefinedInterface
2534
{
2635
/**
27-
* Returns the underlying typed items.
36+
* Returns the underlying Objects array.
2837
*
2938
* @psalm-return list<TItem>
3039
*/
3140
public function value(): array;
3241

3342
/**
34-
* Creates a new collection from a list of raw values.
43+
* Creates a new collection from a list of Objects.
3544
* Implementations MUST fail early on invalid input.
3645
*
37-
* @param array $value raw input values
46+
* @param list<mixed> $value
3847
*
39-
* @psalm-param list<mixed> $value
48+
* @throws ArrayTypeException
4049
*/
4150
public static function fromArray(array $value): static;
4251

4352
/**
44-
* Creates a new collection from a list of raw values, allowing
45-
* late/optional failure semantics via `Undefined` where applicable.
53+
* Creates a new collection from a list of Objects or scalars (which
54+
* will be converted to Undefined type class), allowing late/optional
55+
* failure semantics via `Undefined` where applicable.
56+
*
57+
* @param list<mixed> $value
4658
*
47-
* @param array $value raw input values
59+
* @throws ArrayTypeException
60+
*/
61+
public static function tryFromArray(array $value): static;
62+
63+
/**
64+
* Convert to an array of scalars from an array of Objects.
65+
* Each Item should implement JsonSerializable interface.
4866
*
49-
* @psalm-param list<mixed> $value
67+
* @throws ArrayTypeException
5068
*/
51-
public static function tryFromArray(array $value): static|Undefined;
69+
public function toArray(): array;
5270
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\Bool;
5+
namespace PhpTypedValues\Abstract\Primitive\Bool;
66

7-
use PhpTypedValues\Abstract\AbstractType;
7+
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
88

99
/**
1010
* Base implementation for boolean typed values.
@@ -23,6 +23,6 @@
2323
*
2424
* @psalm-immutable
2525
*/
26-
abstract readonly class BoolType extends AbstractType implements BoolTypeInterface
26+
abstract readonly class BoolType extends PrimitiveType implements BoolTypeInterface
2727
{
2828
}

src/Abstract/Bool/BoolTypeInterface.php renamed to src/Abstract/Primitive/Bool/BoolTypeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\Bool;
5+
namespace PhpTypedValues\Abstract\Primitive\Bool;
66

77
use PhpTypedValues\Exception\BoolTypeException;
88
use PhpTypedValues\Undefined\Alias\Undefined;

src/Abstract/DateTime/DateTimeType.php renamed to src/Abstract/Primitive/DateTime/DateTimeType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\DateTime;
5+
namespace PhpTypedValues\Abstract\Primitive\DateTime;
66

77
use const PHP_EOL;
88

99
use DateTimeImmutable;
1010
use DateTimeZone;
11-
use PhpTypedValues\Abstract\AbstractType;
11+
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
1212
use PhpTypedValues\Exception\DateTimeTypeException;
1313
use PhpTypedValues\Exception\ReasonableRangeDateTimeTypeException;
1414

@@ -32,7 +32,7 @@
3232
*
3333
* @psalm-immutable
3434
*/
35-
abstract readonly class DateTimeType extends AbstractType implements DateTimeTypeInterface
35+
abstract readonly class DateTimeType extends PrimitiveType implements DateTimeTypeInterface
3636
{
3737
protected const FORMAT = '';
3838
protected const ZONE = 'UTC';

src/Abstract/DateTime/DateTimeTypeInterface.php renamed to src/Abstract/Primitive/DateTime/DateTimeTypeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\DateTime;
5+
namespace PhpTypedValues\Abstract\Primitive\DateTime;
66

77
use DateTimeImmutable;
88
use PhpTypedValues\Undefined\Alias\Undefined;

src/Abstract/Float/FloatType.php renamed to src/Abstract/Primitive/Float/FloatType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\Float;
5+
namespace PhpTypedValues\Abstract\Primitive\Float;
66

7-
use PhpTypedValues\Abstract\AbstractType;
7+
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
88
use PhpTypedValues\Exception\FloatTypeException;
99

1010
use function sprintf;
@@ -26,7 +26,7 @@
2626
*
2727
* @psalm-immutable
2828
*/
29-
abstract readonly class FloatType extends AbstractType implements FloatTypeInterface
29+
abstract readonly class FloatType extends PrimitiveType implements FloatTypeInterface
3030
{
3131
/**
3232
* @throws FloatTypeException

src/Abstract/Float/FloatTypeInterface.php renamed to src/Abstract/Primitive/Float/FloatTypeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\Float;
5+
namespace PhpTypedValues\Abstract\Primitive\Float;
66

77
use PhpTypedValues\Undefined\Alias\Undefined;
88

src/Abstract/Integer/IntType.php renamed to src/Abstract/Primitive/Integer/IntType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
declare(strict_types=1);
44

5-
namespace PhpTypedValues\Abstract\Integer;
5+
namespace PhpTypedValues\Abstract\Primitive\Integer;
66

7-
use PhpTypedValues\Abstract\AbstractType;
7+
use PhpTypedValues\Abstract\Primitive\PrimitiveType;
88
use PhpTypedValues\Exception\IntegerTypeException;
99

1010
use function sprintf;
@@ -26,7 +26,7 @@
2626
*
2727
* @psalm-immutable
2828
*/
29-
abstract readonly class IntType extends AbstractType implements IntTypeInterface
29+
abstract readonly class IntType extends PrimitiveType implements IntTypeInterface
3030
{
3131
/**
3232
* @throws IntegerTypeException

0 commit comments

Comments
 (0)