Skip to content

Commit c1fdbe8

Browse files
committed
Add inline documentation to clarify purpose, usage, and behavior of ArrayOfStrings, ArrayTypeInterface, and examples
1 parent b023eb1 commit c1fdbe8

File tree

7 files changed

+118
-2
lines changed

7 files changed

+118
-2
lines changed

src/Abstract/Array/ArrayType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
/**
1111
* Base implementation for array typed values.
1212
*
13+
* Provides an immutable, iterable, and JSON‑serializable collection of
14+
* typed items. Concrete implementations define item validation and
15+
* factory behavior.
16+
*
1317
* @internal
1418
*
1519
* @psalm-internal PhpTypedValues

src/Abstract/Array/ArrayTypeInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,43 @@
99
/**
1010
* Contract for array typed values.
1111
*
12+
* Represents a read‑only collection of typed items with factory helpers
13+
* to construct the collection from raw arrays. Implementations are
14+
* immutable and iterable.
15+
*
16+
* @internal
17+
*
18+
* @psalm-internal PhpTypedValues
19+
*
1220
* @template TItem
1321
*
1422
* @psalm-immutable
1523
*/
1624
interface ArrayTypeInterface
1725
{
1826
/**
27+
* Returns the underlying typed items.
28+
*
1929
* @psalm-return list<TItem>
2030
*/
2131
public function value(): array;
2232

2333
/**
34+
* Creates a new collection from a list of raw values.
35+
* Implementations MUST fail early on invalid input.
36+
*
37+
* @param array $value raw input values
38+
*
2439
* @psalm-param list<mixed> $value
2540
*/
2641
public static function fromArray(array $value): static;
2742

2843
/**
44+
* Creates a new collection from a list of raw values, allowing
45+
* late/optional failure semantics via `Undefined` where applicable.
46+
*
47+
* @param array $value raw input values
48+
*
2949
* @psalm-param list<mixed> $value
3050
*/
3151
public static function tryFromArray(array $value): static|Undefined;

src/Usage/Example/ArrayOfStrings.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
use PhpTypedValues\String\StringNonEmpty;
1414

1515
/**
16+
* Immutable collection of non-empty strings represented as `StringNonEmpty`.
17+
*
18+
* Provides factory helpers to construct from raw arrays, exposes the typed
19+
* items via `value()`, supports iteration, and can be converted to a plain
20+
* array or JSON.
21+
*
1622
* @internal
1723
*
1824
* @psalm-internal PhpTypedValues
@@ -24,7 +30,9 @@
2430
final readonly class ArrayOfStrings extends ArrayType
2531
{
2632
/**
27-
* @param non-empty-list<StringNonEmpty> $value
33+
* Creates a new collection from a non-empty list of `StringNonEmpty`.
34+
*
35+
* @param non-empty-list<StringNonEmpty> $value items must be instances of `StringNonEmpty`
2836
*
2937
* @throws StringTypeException
3038
* @throws TypeException
@@ -45,6 +53,11 @@ public function __construct(
4553
}
4654

4755
/**
56+
* Creates a collection from a non-empty list of raw values by casting each
57+
* value to string and validating it as `StringNonEmpty`.
58+
*
59+
* @param array $value raw values to convert
60+
*
4861
* @psalm-param list<mixed> $value
4962
*
5063
* @throws StringTypeException
@@ -66,6 +79,11 @@ public static function fromArray(array $value): static
6679
}
6780

6881
/**
82+
* Same as `fromArray()` but intended for scenarios where late/optional
83+
* failure might be desirable. Current implementation mirrors `fromArray()`.
84+
*
85+
* @param array $value raw values to convert
86+
*
6987
* @psalm-param list<mixed> $value
7088
*
7189
* @throws StringTypeException
@@ -77,6 +95,8 @@ public static function tryFromArray(array $value): static
7795
}
7896

7997
/**
98+
* Returns the underlying typed items.
99+
*
80100
* @psalm-return non-empty-list<StringNonEmpty>
81101
*/
82102
public function value(): array
@@ -85,6 +105,8 @@ public function value(): array
85105
}
86106

87107
/**
108+
* Converts the collection to a non-empty list of raw strings.
109+
*
88110
* @return non-empty-list<non-empty-string>
89111
*/
90112
public function toArray(): array
@@ -101,6 +123,8 @@ public function toArray(): array
101123
}
102124

103125
/**
126+
* JSON serialization proxy that returns the same as `toArray()`.
127+
*
104128
* @return non-empty-list<non-empty-string>
105129
*/
106130
public function jsonSerialize(): array
@@ -109,6 +133,8 @@ public function jsonSerialize(): array
109133
}
110134

111135
/**
136+
* Iterates over the underlying `StringNonEmpty` items.
137+
*
112138
* @return Generator<int, StringNonEmpty>
113139
*/
114140
public function getIterator(): Generator

src/Usage/Example/EarlyFail.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
use PhpTypedValues\String\StringNonEmpty;
1515

1616
/**
17+
* Example of strict early‑fail semantics for constructing a composite value.
18+
*
19+
* All fields must be valid on creation time. Any invalid input immediately
20+
* raises a domain exception from the underlying typed values.
21+
*
1722
* @internal
1823
*
1924
* @psalm-internal PhpTypedValues
@@ -30,6 +35,12 @@ public function __construct(
3035
}
3136

3237
/**
38+
* Factory that validates all inputs and fails immediately on invalid data.
39+
*
40+
* @param int $id positive integer identifier
41+
* @param string $firstName non-empty person name
42+
* @param float $height positive height value
43+
*
3344
* @throws IntegerTypeException
3445
* @throws FloatTypeException
3546
* @throws StringTypeException
@@ -46,16 +57,25 @@ public static function fromScalars(
4657
);
4758
}
4859

60+
/**
61+
* Returns validated height value.
62+
*/
4963
public function getHeight(): FloatPositive
5064
{
5165
return $this->height;
5266
}
5367

68+
/**
69+
* Returns validated identifier.
70+
*/
5471
public function getId(): IntegerPositive
5572
{
5673
return $this->id;
5774
}
5875

76+
/**
77+
* Returns validated first name.
78+
*/
5979
public function getFirstName(): StringNonEmpty
6080
{
6181
return $this->firstName;

src/Usage/Example/LateFail.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
use PhpTypedValues\Undefined\Alias\Undefined;
1414

1515
/**
16+
* Example of late‑fail semantics using `Undefined` for optional/invalid inputs.
17+
*
18+
* - `id` must be valid at construction time (early fail).
19+
* - `firstName` and `height` accept mixed inputs and may become `Undefined`;
20+
* accessing their string/primitive values may fail later.
21+
*
1622
* @internal
1723
*
1824
* @psalm-internal PhpTypedValues
@@ -29,6 +35,14 @@ public function __construct(
2935
}
3036

3137
/**
38+
* Factory that validates only the strictly required field (`id`) early,
39+
* while optional/mixed inputs for `firstName` and `height` may result in
40+
* `Undefined` values (late‑fail on access).
41+
*
42+
* @param int $id positive integer identifier (validated immediately)
43+
* @param mixed $firstName non-empty string or will become `Undefined`
44+
* @param string|float|int|null $height positive numeric or `null` to become `Undefined`
45+
*
3246
* @throws IntegerTypeException
3347
*/
3448
public static function fromScalars(
@@ -43,16 +57,25 @@ public static function fromScalars(
4357
);
4458
}
4559

60+
/**
61+
* Returns height, which may be `Undefined`.
62+
*/
4663
public function getHeight(): FloatPositive|Undefined
4764
{
4865
return $this->height;
4966
}
5067

68+
/**
69+
* Returns validated identifier.
70+
*/
5171
public function getId(): IntegerPositive
5272
{
5373
return $this->id;
5474
}
5575

76+
/**
77+
* Returns first name or `Undefined`.
78+
*/
5679
public function getFirstName(): StringNonEmpty|Undefined
5780
{
5881
return $this->firstName;

src/Usage/Example/OptionalFail.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
require_once 'vendor/autoload.php';
1717

1818
/**
19+
* Example of optional/late‑fail semantics.
20+
*
21+
* - `id` must be valid at construction time (early fail).
22+
* - `firstName` uses `tryFromMixed` and may be `Undefined` (late fail on access).
23+
* - `height` fails early only when provided; `null` becomes `Undefined` (late fail).
24+
*
1925
* @internal
2026
*
2127
* @psalm-internal PhpTypedValues
@@ -32,6 +38,12 @@ public function __construct(
3238
}
3339

3440
/**
41+
* Factory that supports optional and late‑fail inputs.
42+
*
43+
* @param int $id positive integer identifier (validated immediately)
44+
* @param string|null $firstName non-empty string or empty/invalid treated as `Undefined`
45+
* @param string|float|int|null $height positive numeric value; `null` produces `Undefined`
46+
*
3547
* @throws IntegerTypeException
3648
* @throws FloatTypeException
3749
*/
@@ -49,22 +61,33 @@ public static function fromScalars(
4961
);
5062
}
5163

64+
/**
65+
* Returns height, which may be `Undefined` when it was omitted.
66+
*/
5267
public function getHeight(): FloatPositive|Undefined
5368
{
5469
return $this->height;
5570
}
5671

72+
/**
73+
* Returns validated identifier.
74+
*/
5775
public function getId(): IntegerPositive
5876
{
5977
return $this->id;
6078
}
6179

80+
/**
81+
* Returns first name or `Undefined` when the input was empty/invalid.
82+
*/
6283
public function getFirstName(): StringNonEmpty|Undefined
6384
{
6485
return $this->firstName;
6586
}
6687

6788
/**
89+
* Serializes to an associative array of strings.
90+
*
6891
* @throws UndefinedTypeException
6992
*/
7093
public function jsonSerialize(): array

tests/Unit/Usage/Example/ArrayOfStringsTest.php

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

8888
$arrayOfStrings = ArrayOfStrings::fromArray($array);
8989

90-
$arrayOfStringsJson = json_encode($arrayOfStrings, JSON_THROW_ON_ERROR);
90+
$arrayOfStringsJson = json_encode($arrayOfStrings, \JSON_THROW_ON_ERROR);
9191
$arrayOfStringsJsonDecoded = json_decode($arrayOfStringsJson, true);
9292

9393
expect($arrayOfStrings->toArray())->toBe($array);

0 commit comments

Comments
 (0)