Skip to content

Commit da81abd

Browse files
committed
Better support for classes in schema
1 parent 08bf7ae commit da81abd

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/ConstantContact/Definition/Base.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,31 @@ public function __set(string $field, $value)
120120
$arrayEnd = \strpos($expectedType, '>');
121121

122122
$arrayType = \trim(\substr($expectedType, $arrayStart + 2, $arrayEnd - $arrayStart - 2), '\\');
123+
$convertToObjects = ! isset(self::$scalars[$arrayType]);
123124

124125
foreach ($value as $index => $element)
125126
{
126127
$elementType = \get_debug_type($element);
127128

129+
// convert members of the array to the correct type if not a standard type
130+
if ($convertToObjects && ! \is_object($element))
131+
{
132+
$value[$index] = new $arrayType($element);
133+
134+
continue;
135+
}
136+
128137
if ($arrayType != $elementType)
129138
{
130-
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} should be an array<{$arrayType}> but index {$index} is of type {$elementType}");
139+
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} should be an array[{$arrayType}] but index {$index} is of type {$elementType}");
131140
}
132141
}
133142
}
134143
}
144+
elseif (! \is_object($value) && ! isset(self::$scalars[$expectedType]))
145+
{
146+
$value = new $expectedType($value);
147+
}
135148
elseif ($expectedType != $type)
136149
{
137150
throw new \PHPFUI\ConstantContact\Exception\InvalidType(static::class . "::{$field} is of type {$type} but should be type {$expectedType}");

0 commit comments

Comments
 (0)