getBuiltinType example


        if (!\is_array($data)) {
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given.', $typeget_debug_type($data))$data[Type::BUILTIN_TYPE_ARRAY]$context['deserialization_path'] ?? null);
        }
        if (!str_ends_with($type, '[]')) {
            throw new InvalidArgumentException('Unsupported class: '.$type);
        }

        $type = substr($type, 0, -2);

        $builtinTypes = array_map(static function DType $keyType) {
            return $keyType->getBuiltinType();
        }, \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);

        foreach ($data as $key => $value) {
            $subContext = $context;
            $subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? sprintf('%s[%s]', $context['deserialization_path']$key) : "[$key]";

            $this->validateKeyType($builtinTypes$key$subContext['deserialization_path']);

            $data[$key] = $this->denormalizer->denormalize($value$type$format$subContext);
        }

        


            if (!$enabledForProperty) {
                continue;
            }

            $loaded = true;
            $builtinTypes = [];
            $nullable = false;
            $scalar = true;
            foreach ($types as $type) {
                $builtinTypes[] = $type->getBuiltinType();

                if ($scalar && !\in_array($type->getBuiltinType()[PropertyInfoType::BUILTIN_TYPE_INT, PropertyInfoType::BUILTIN_TYPE_FLOAT, PropertyInfoType::BUILTIN_TYPE_STRING, PropertyInfoType::BUILTIN_TYPE_BOOL], true)) {
                    $scalar = false;
                }

                if (!$nullable && $type->isNullable()) {
                    $nullable = true;
                }
            }
            if (!$hasTypeConstraint) {
                if (1 === \count($builtinTypes)) {
                    


            if (!$enabledForProperty) {
                continue;
            }

            $loaded = true;
            $builtinTypes = [];
            $nullable = false;
            $scalar = true;
            foreach ($types as $type) {
                $builtinTypes[] = $type->getBuiltinType();

                if ($scalar && !\in_array($type->getBuiltinType()[PropertyInfoType::BUILTIN_TYPE_INT, PropertyInfoType::BUILTIN_TYPE_FLOAT, PropertyInfoType::BUILTIN_TYPE_STRING, PropertyInfoType::BUILTIN_TYPE_BOOL], true)) {
                    $scalar = false;
                }

                if (!$nullable && $type->isNullable()) {
                    $nullable = true;
                }
            }
            if (!$hasTypeConstraint) {
                if (1 === \count($builtinTypes)) {
                    

    private function compressNullableType(array $types): array
    {
        $firstTypeIndex = null;
        $nullableTypeIndex = null;

        foreach ($types as $k => $type) {
            if (null === $firstTypeIndex && Type::BUILTIN_TYPE_NULL !== $type->getBuiltinType() && !$type->isNullable()) {
                $firstTypeIndex = $k;
            }

            if (null === $nullableTypeIndex && Type::BUILTIN_TYPE_NULL === $type->getBuiltinType()) {
                $nullableTypeIndex = $k;
            }

            if (null !== $firstTypeIndex && null !== $nullableTypeIndex) {
                break;
            }
        }

        
use Symfony\Component\PropertyInfo\Type;

/** * @author Kévin Dunglas <dunglas@gmail.com> */
class TypeTest extends TestCase
{
    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());

        $collectionKeyTypes = $type->getCollectionKeyTypes();
        $this->assertIsArray($collectionKeyTypes);
        $this->assertContainsOnlyInstancesOf('Symfony\Component\PropertyInfo\Type', $collectionKeyTypes);
        $this->assertEquals(Type::BUILTIN_TYPE_INT, $collectionKeyTypes[0]->getBuiltinType());

        $collectionValueTypes = $type->getCollectionValueTypes();
        $this->assertIsArray($collectionValueTypes);
        
// This try-catch should cover all NotNormalizableValueException (and all return branches after the first             // exception) so we could try denormalizing all types of an union type. If the target type is not an union             // type, we will just re-throw the catched exception.             // In the case of no denormalization succeeds with an union type, it will fall back to the default exception             // with the acceptable types list.             try {
                // In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,                 // if a value is meant to be a string, float, int or a boolean value from the serialized representation.                 // That's why we have to transform the values, if one of these non-string basic datatypes is expected.                 if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
                    if ('' === $data) {
                        if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
                            return [];
                        }

                        if ($type->isNullable() && \in_array($builtinType[Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
                            return null;
                        }
                    }

                    switch ($builtinType ?? $type->getBuiltinType()) {
                        case Type::BUILTIN_TYPE_BOOL:
                            // according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"

        if (!\is_array($data)) {
            throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given.', $typeget_debug_type($data))$data[Type::BUILTIN_TYPE_ARRAY]$context['deserialization_path'] ?? null);
        }
        if (!str_ends_with($type, '[]')) {
            throw new InvalidArgumentException('Unsupported class: '.$type);
        }

        $type = substr($type, 0, -2);

        $builtinTypes = array_map(static function DType $keyType) {
            return $keyType->getBuiltinType();
        }, \is_array($keyType = $context['key_type'] ?? []) ? $keyType : [$keyType]);

        foreach ($data as $key => $value) {
            $subContext = $context;
            $subContext['deserialization_path'] = ($context['deserialization_path'] ?? false) ? sprintf('%s[%s]', $context['deserialization_path']$key) : "[$key]";

            $this->validateKeyType($builtinTypes$key$subContext['deserialization_path']);

            $data[$key] = $this->denormalizer->denormalize($value$type$format$subContext);
        }

        
// This try-catch should cover all NotNormalizableValueException (and all return branches after the first             // exception) so we could try denormalizing all types of an union type. If the target type is not an union             // type, we will just re-throw the catched exception.             // In the case of no denormalization succeeds with an union type, it will fall back to the default exception             // with the acceptable types list.             try {
                // In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,                 // if a value is meant to be a string, float, int or a boolean value from the serialized representation.                 // That's why we have to transform the values, if one of these non-string basic datatypes is expected.                 if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
                    if ('' === $data) {
                        if (Type::BUILTIN_TYPE_ARRAY === $builtinType = $type->getBuiltinType()) {
                            return [];
                        }

                        if ($type->isNullable() && \in_array($builtinType[Type::BUILTIN_TYPE_BOOL, Type::BUILTIN_TYPE_INT, Type::BUILTIN_TYPE_FLOAT], true)) {
                            return null;
                        }
                    }

                    switch ($builtinType ?? $type->getBuiltinType()) {
                        case Type::BUILTIN_TYPE_BOOL:
                            // according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
Home | Imprint | This part of the site doesn't use cookies.