1010use PhpTypedValues \Abstract \Array \ArrayType ;
1111use PhpTypedValues \Exception \StringTypeException ;
1212use PhpTypedValues \Exception \TypeException ;
13- use PhpTypedValues \Exception \UndefinedTypeException ;
1413use PhpTypedValues \String \StringNonEmpty ;
15- use PhpTypedValues \Undefined \Alias \Undefined ;
1614
1715/**
1816 * @internal
1917 *
2018 * @psalm-internal PhpTypedValues
2119 *
20+ * @extends ArrayType<StringNonEmpty>
21+ *
2222 * @psalm-immutable
2323 */
2424final readonly class ArrayOfStrings extends ArrayType
2525{
2626 /**
27- * @param StringNonEmpty|Undefined[] $value
27+ * @param non-empty-list< StringNonEmpty> $value
2828 *
2929 * @throws StringTypeException
3030 * @throws TypeException
3131 */
3232 public function __construct (
33+ /** @var non-empty-list<StringNonEmpty> */
3334 private array $ value ,
3435 ) {
3536 if ($ value === []) {
3637 throw new TypeException ('Expected non-empty array ' );
3738 }
3839
3940 foreach ($ value as $ item ) {
40- if (( !$ item instanceof StringNonEmpty) && (! $ item instanceof Undefined) ) {
41+ if (!$ item instanceof StringNonEmpty) {
4142 throw new StringTypeException ('Expected array of StringNonEmpty or Undefined instance ' );
4243 }
4344 }
4445 }
4546
4647 /**
48+ * @psalm-param list<mixed> $value
49+ *
4750 * @throws StringTypeException
4851 * @throws TypeException
4952 */
5053 public static function fromArray (array $ value ): static
5154 {
55+ if ($ value === []) {
56+ throw new TypeException ('Expected non-empty array ' );
57+ }
58+
5259 $ typed = [];
5360 foreach ($ value as $ item ) {
54- $ typed [] = StringNonEmpty::fromString ($ item );
61+ $ typed [] = StringNonEmpty::fromString (( string ) $ item );
5562 }
5663
64+ /** @var non-empty-list<StringNonEmpty> $typed */
5765 return new self ($ typed );
5866 }
5967
6068 /**
61- * @return StringNonEmpty|Undefined[]
69+ * @psalm-param list<mixed> $value
70+ *
71+ * @throws StringTypeException
72+ * @throws TypeException
73+ */
74+ public static function tryFromArray (array $ value ): static
75+ {
76+ return static ::fromArray ($ value );
77+ }
78+
79+ /**
80+ * @psalm-return non-empty-list<StringNonEmpty>
6281 */
6382 public function value (): array
6483 {
@@ -67,47 +86,33 @@ public function value(): array
6786
6887 /**
6988 * @return non-empty-list<non-empty-string>
70- *
71- * @throws UndefinedTypeException
7289 */
7390 public function toArray (): array
7491 {
7592 $ result = [];
7693 foreach ($ this ->value as $ item ) {
77- /** @var non-empty-string $value */
78- $ value = $ item ->toString ();
79- $ result [] = $ value ;
94+ /** @var non-empty-string $str */
95+ $ str = $ item ->toString ();
96+ $ result [] = $ str ;
8097 }
8198
8299 /** @var non-empty-list<non-empty-string> $result */
83100 return $ result ;
84101 }
85102
86- /** @return non-empty-list<non-empty-string> */
103+ /**
104+ * @return non-empty-list<non-empty-string>
105+ */
87106 public function jsonSerialize (): array
88107 {
89108 return $ this ->toArray ();
90109 }
91110
92111 /**
93- * @return Generator<StringNonEmpty>
112+ * @return Generator<int, StringNonEmpty>
94113 */
95114 public function getIterator (): Generator
96115 {
97116 yield from $ this ->value ;
98117 }
99-
100- /**
101- * @throws StringTypeException
102- * @throws TypeException
103- */
104- public static function tryFromArray (array $ value ): static |Undefined
105- {
106- $ typed = [];
107- foreach ($ value as $ item ) {
108- $ typed [] = StringNonEmpty::tryFromMixed ($ item );
109- }
110-
111- return new static ($ typed );
112- }
113118}
0 commit comments