Skip to content

Commit 5cd1a64

Browse files
committed
Valid min and max array sizes
1 parent e1d519d commit 5cd1a64

File tree

4 files changed

+64
-25
lines changed

4 files changed

+64
-25
lines changed

Tests/DefinitionTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,10 @@ public function testBadArrayTypes() : void
133133
];
134134
}
135135

136-
public function testBadArrayOfTypeSize() : void
136+
public function testBadArrayOfTypeSizeMax() : void
137137
{
138138
$fixture = new \Tests\Fixtures\TypeTest();
139-
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidValue::class);
139+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidLength::class);
140140
$fixture->classArraySize = [
141141
new \Tests\Fixtures\ClassTest(),
142142
new \Tests\Fixtures\ClassTest(),
@@ -146,6 +146,15 @@ public function testBadArrayOfTypeSize() : void
146146
];
147147
}
148148

149+
public function testBadArrayOfTypeSizeMin() : void
150+
{
151+
$fixture = new \Tests\Fixtures\TypeTest();
152+
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidLength::class);
153+
$fixture->classArraySizeMin = [
154+
new \Tests\Fixtures\ClassTest(),
155+
];
156+
}
157+
149158
public function testBadClassType() : void
150159
{
151160
$fixture = new \Tests\Fixtures\TypeTest();
@@ -180,4 +189,22 @@ public function testGeneratedClass() : void
180189
$this->expectException(\PHPFUI\ConstantContact\Exception\InvalidType::class);
181190
$fixture->physical_address_in_footer = new \DateTime();
182191
}
192+
193+
public function testConstructFromArray() : void
194+
{
195+
$original = [
196+
'boolean' => true,
197+
'integer' => 10,
198+
'string' => 'string',
199+
'enum' => 'primary_email',
200+
'array' => ['one' => 1, 'two' => 2, 'three' => 3],
201+
'float' => 3.1415926,
202+
'ucEnum' => 'SCHEDULED',
203+
'intEnum' => 5,
204+
'class' => [['float' => 234.567], ],
205+
'classArray' => [['boolean' => false], ['integer' => 22], ['array' => [1,2,3]], ],
206+
];
207+
$fixture = new \Tests\Fixtures\TypeTest($original);
208+
$this->assertEquals($original, $fixture->getData());
209+
}
183210
}

Tests/Fixtures/TypeTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ class TypeTest extends \PHPFUI\ConstantContact\Definition\Base
1414
'ucEnum' => ['DRAFT', 'SCHEDULED', 'EXECUTING', 'DONE', 'ERROR', 'REMOVED'],
1515
'intEnum' => [1, 2, 3, 4, 5],
1616
'class' => 'Tests\\Fixtures\\ClassTest',
17-
'classArray' => 'array[\\Tests\\Fixtures\\ClassTest]',
18-
'classArraySize' => 'array[\\Tests\\Fixtures\\ClassTest][3]',
17+
'classArray' => 'array<\\Tests\\Fixtures\\ClassTest>',
18+
'classArraySize' => 'array<\\Tests\\Fixtures\\ClassTest>',
19+
'classArraySizeMin' => 'array<\\Tests\\Fixtures\\ClassTest>',
1920
];
2021

2122
protected static array $minLength = [
2223
'string' => 10,
24+
'classArraySizeMin' => 2,
2325
];
2426

2527
protected static array $maxLength = [
2628
'string' => 50,
29+
'classArraySize' => 3,
2730
];
2831
}

Tools/Generator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ public function generateDefinition(string $class, array $properties) : void
133133

134134
if ('array' == $type && $itemType)
135135
{
136-
$docType = 'array<' . $itemType . '>';
137-
$type = 'array[' . $itemType . ']';
136+
$type = 'array<' . $itemType . '>';
138137

139138
if ($details['maxItems'] ?? false)
140139
{
141-
$type .= '[' . $details['maxItems'] . ']';
140+
$maxLength[$name] = (int)$details['maxItems'];
141+
}
142+
143+
if ($details['minItems'] ?? false)
144+
{
145+
$minLength[$name] = (int)$details['minItems'];
142146
}
143147
}
144148
}
@@ -165,7 +169,7 @@ public function generateDefinition(string $class, array $properties) : void
165169
$type = $originalType;
166170
}
167171
$type = \str_replace('\\\\', '\\', $type);
168-
$docBlock[] = "{$docType} {$dollar}{$name} {$description}";
172+
$docBlock[] = "{$type} {$dollar}{$name} {$description}";
169173
}
170174
}
171175
$this->generateFromTemplate($class, ['fields' => $fields, 'minLength' => $minLength, 'maxLength' => $maxLength, ], $docBlock);

src/ConstantContact/Definition/Base.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,26 @@ public function __set(string $field, $value)
8484

8585
if ('array' == $type && \str_starts_with($expectedType, 'array'))
8686
{
87-
$arrayStart = \strpos($expectedType, '[');
87+
$arrayStart = \strpos($expectedType, '<');
8888

8989
if ($arrayStart)
9090
{
91-
$arrayEnd = \strpos($expectedType, ']');
91+
$arrayEnd = \strpos($expectedType, '>');
9292

93-
if (\strlen($expectedType) > $arrayEnd + 1)
94-
{
95-
$maxSize = (int)\trim(\substr($expectedType, $arrayEnd), '[]');
96-
97-
if (\count($value) > $maxSize)
98-
{
99-
throw new \PHPFUI\ConstantContact\Exception\InvalidValue(static::class . "::{$field} array has a limit of {$maxSize} values");
100-
}
101-
}
102-
$expectedType = \trim(\substr($expectedType, $arrayStart + 2, $arrayEnd - $arrayStart - 2), '\\');
93+
$arrayType = \trim(\substr($expectedType, $arrayStart + 2, $arrayEnd - $arrayStart - 2), '\\');
10394
}
10495
else
10596
{
106-
$expectedType = 'string';
97+
$arrayType = 'string';
10798
}
10899

109100
foreach ($value as $index => $element)
110101
{
111102
$elementType = \get_debug_type($element);
112103

113-
if ($expectedType != $elementType)
104+
if ($arrayType != $elementType)
114105
{
115-
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} should be an array[{$expectedType}] but index {$index} is of type {$elementType}");
106+
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} should be an array<{$arrayType}> but index {$index} is of type {$elementType}");
116107
}
117108
}
118109
}
@@ -126,7 +117,14 @@ public function __set(string $field, $value)
126117
{
127118
$minLength = static::$minLength[$field];
128119

129-
if (\strlen($value) < $minLength)
120+
if ('array' == $type && \str_starts_with($expectedType, 'array'))
121+
{
122+
if (\count($value) < $minLength)
123+
{
124+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} array must have at least {$minLength} values");
125+
}
126+
}
127+
elseif (\strlen($value) < $minLength)
130128
{
131129
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be at least {$minLength} characters long");
132130
}
@@ -136,7 +134,14 @@ public function __set(string $field, $value)
136134
{
137135
$maxLength = static::$maxLength[$field];
138136

139-
if (\strlen($value) > $maxLength)
137+
if ('array' == $type && \str_starts_with($expectedType, 'array'))
138+
{
139+
if (\count($value) > $maxLength)
140+
{
141+
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} array has a limit of {$maxLength} values");
142+
}
143+
}
144+
elseif (\strlen($value) > $maxLength)
140145
{
141146
throw new \PHPFUI\ConstantContact\Exception\InvalidLength(static::class . "::{$field} must be at less than {$maxLength} characters long");
142147
}

0 commit comments

Comments
 (0)