Skip to content

Commit 128267f

Browse files
committed
Add Array.php usage example and update Composer script to include it
1 parent b8f5b3a commit 128267f

File tree

8 files changed

+169
-2
lines changed

8 files changed

+169
-2
lines changed

src/Abstract/AbstractTypeInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ interface AbstractTypeInterface
3434
*/
3535
public static function fromString(string $value): static;
3636

37-
// public static function tryFromMixed(mixed $value): static|Undefined;
37+
// public static function tryFromMixed(mixed $value): static|Undefined; //todo enable it
3838

3939
/**
4040
* Returns a normalized string representation of the underlying value.

src/Abstract/Array/ArrayType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Abstract\Array;
6+
7+
use IteratorAggregate;
8+
use JsonSerializable;
9+
use PhpTypedValues\Abstract\AbstractType;
10+
11+
/**
12+
* Base implementation for array typed values.
13+
*
14+
* @internal
15+
*
16+
* @psalm-internal PhpTypedValues
17+
*
18+
* @psalm-immutable
19+
*/
20+
abstract readonly class ArrayType implements ArrayTypeInterface, IteratorAggregate, JsonSerializable
21+
{
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Abstract\Array;
6+
7+
use PhpTypedValues\Undefined\Alias\Undefined;
8+
9+
/**
10+
* Contract for array typed values.
11+
*
12+
* @psalm-immutable
13+
*/
14+
interface ArrayTypeInterface
15+
{
16+
public function value(): array;
17+
18+
public static function fromArray(array $value): static;
19+
20+
public static function tryFromArray(array $value): static|Undefined;
21+
22+
}

src/Usage/Composite/Composite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@
3232
echo $test->getId()->toString() . PHP_EOL;
3333
echo $test->getFirstName()->toString() . PHP_EOL;
3434
echo $test->getHeight()->toString() . PHP_EOL;
35+
36+
echo json_encode($test, JSON_THROW_ON_ERROR) . PHP_EOL;
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpTypedValues\Usage\Example;
6+
7+
require_once 'vendor/autoload.php';
8+
9+
use PhpTypedValues\Abstract\Array\ArrayType;
10+
use PhpTypedValues\Exception\StringTypeException;
11+
use PhpTypedValues\Exception\TypeException;
12+
use PhpTypedValues\String\StringNonEmpty;
13+
use PhpTypedValues\Undefined\Alias\Undefined;
14+
15+
/**
16+
* @internal
17+
*
18+
* @psalm-internal PhpTypedValues
19+
* @psalm-immutable
20+
*/
21+
final readonly class ArrayOfStrings extends ArrayType
22+
{
23+
/**
24+
* @param StringNonEmpty[] $value
25+
*
26+
* @throws StringTypeException
27+
* @throws TypeException
28+
*/
29+
public function __construct(
30+
private array $value,
31+
) {
32+
if ($value === []) {
33+
throw new TypeException('Expected non-empty array');
34+
}
35+
36+
foreach ($value as $item) {
37+
if (!$item instanceof StringNonEmpty) {
38+
throw new StringTypeException('Expected array of StringNonEmpty instance');
39+
}
40+
}
41+
}
42+
43+
/**
44+
* @throws StringTypeException
45+
* @throws TypeException
46+
*/
47+
public static function fromArray(array $value): self
48+
{
49+
$typed = [];
50+
foreach ($value as $item) {
51+
$typed[] = StringNonEmpty::fromString($item);
52+
}
53+
54+
return new self($typed);
55+
}
56+
57+
/**
58+
* @return StringNonEmpty[]
59+
*/
60+
public function value(): array
61+
{
62+
return $this->value;
63+
}
64+
65+
/**
66+
* @return non-empty-list<non-empty-string>
67+
*/
68+
public function toStrings(): array
69+
{
70+
$result = [];
71+
foreach ($this->value as $item) {
72+
/** @var non-empty-string $value */
73+
$value = $item->toString();
74+
$result[] = $value;
75+
}
76+
77+
/** @var non-empty-list<non-empty-string> $result */
78+
return $result;
79+
}
80+
81+
/** @return non-empty-list<non-empty-string> */
82+
public function jsonSerialize(): array
83+
{
84+
return $this->toStrings();
85+
}
86+
87+
/**
88+
* @return Traversable<StringNonEmpty>
89+
*/
90+
public function getIterator(): Traversable
91+
{
92+
yield from $this->value;
93+
}
94+
95+
public static function tryFromArray(array $value): static|Undefined
96+
{
97+
$typed = [];
98+
foreach ($value as $item) {
99+
$typed[] = StringNonEmpty::tryFromString($item);
100+
}
101+
102+
return new self($typed);
103+
}
104+
}

src/Usage/Example/EarlyFail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* @internal
1818
*
1919
* @psalm-internal PhpTypedValues
20+
* @psalm-immutable
2021
*/
2122
final readonly class EarlyFail
2223
{

src/Usage/Example/LateFail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* @internal
1717
*
1818
* @psalm-internal PhpTypedValues
19+
* @psalm-immutable
1920
*/
2021
final readonly class LateFail
2122
{

src/Usage/Example/OptionalFail.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
namespace PhpTypedValues\Usage\Example;
66

7+
use JsonSerializable;
78
use PhpTypedValues\Exception\FloatTypeException;
89
use PhpTypedValues\Exception\IntegerTypeException;
10+
use PhpTypedValues\Exception\UndefinedTypeException;
911
use PhpTypedValues\Float\FloatPositive;
1012
use PhpTypedValues\Integer\IntegerPositive;
1113
use PhpTypedValues\String\StringNonEmpty;
@@ -17,8 +19,9 @@
1719
* @internal
1820
*
1921
* @psalm-internal PhpTypedValues
22+
* @psalm-immutable
2023
*/
21-
final readonly class OptionalFail
24+
final readonly class OptionalFail implements JsonSerializable
2225
{
2326
public function __construct(
2427
private IntegerPositive $id,
@@ -59,4 +62,16 @@ public function getFirstName(): StringNonEmpty|Undefined
5962
{
6063
return $this->firstName;
6164
}
65+
66+
/**
67+
* @throws UndefinedTypeException
68+
*/
69+
public function jsonSerialize(): array
70+
{
71+
return [
72+
'id' => $this->id->toString(),
73+
'firstName' => $this->firstName->toString(),
74+
'height' => $this->height->toString(),
75+
];
76+
}
6277
}

0 commit comments

Comments
 (0)