public function testConstruct() { $type = new Type('object', true, 'ArrayObject', true, new Type('int'), new Type('string')); $this->assertEquals(Type::BUILTIN_TYPE_OBJECT, $type->getBuiltinType()); $this->assertTrue($type->isNullable()); $this->assertEquals('ArrayObject', $type->getClassName()); $this->assertTrue($type->isCollection()); $collectionKeyType = $type->getCollectionKeyType(); $this->assertInstanceOf('Symfony\\Component\\PropertyInfo\\Type', $collectionKeyType); $this->assertEquals(Type::BUILTIN_TYPE_INT, $collectionKeyType->getBuiltinType()); $collectionValueType = $type->getCollectionValueType(); $this->assertInstanceOf('Symfony\\Component\\PropertyInfo\\Type', $collectionValueType); $this->assertEquals(Type::BUILTIN_TYPE_STRING, $collectionValueType->getBuiltinType()); }
/** * Denormalizes a collection of objects. * * @param string $attribute * @param PropertyMetadata $propertyMetadata * @param Type $type * @param string $className * @param mixed $value * @param string|null $format * @param array $context * * @throws InvalidArgumentException * * @return array */ private function denormalizeCollection(string $attribute, PropertyMetadata $propertyMetadata, Type $type, string $className, $value, string $format = null, array $context) : array { if (!is_array($value)) { throw new InvalidArgumentException(sprintf('The type of the "%s" attribute must be "array", "%s" given.', $attribute, gettype($value))); } $collectionKeyType = $type->getCollectionKeyType(); $collectionKeyBuiltinType = null === $collectionKeyType ? null : $collectionKeyType->getBuiltinType(); $values = []; foreach ($value as $index => $obj) { if (null !== $collectionKeyBuiltinType && !call_user_func('is_' . $collectionKeyBuiltinType, $index)) { throw new InvalidArgumentException(sprintf('The type of the key "%s" must be "%s", "%s" given.', $index, $collectionKeyBuiltinType, gettype($index))); } $values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $context); } return $values; }