Convert a type definition to a type

@param TypeDefinitionInterface $typeDefinition
@return ObjectTypeInterface
@throws CmisRuntimeException
494560
public function convertTypeDefinition(TypeDefinitionInterface $typeDefinition)
    {
        if ($typeDefinition instanceof DocumentTypeDefinitionInterface) {
            return new DocumentType($this->session, $typeDefinition);
        } elseif ($typeDefinition instanceof FolderTypeDefinitionInterface) {
            return new FolderType($this->session, $typeDefinition);
        } elseif ($typeDefinition instanceof RelationshipTypeDefinitionInterface) {
            return new RelationshipType($this->session, $typeDefinition);
        } elseif ($typeDefinition instanceof PolicyTypeDefinitionInterface) {
            return new PolicyType($this->session, $typeDefinition);
        } elseif ($typeDefinition instanceof ItemTypeDefinitionInterface) {
            return new ItemType($this->session, $typeDefinition);
        } elseif ($typeDefinition instanceof SecondaryTypeDefinitionInterface) {
            return new SecondaryType($this->session, $typeDefinition);
        } else {
            throw new CmisRuntimeException(
                sprintf('Unknown base type! Received "%s"', get_class($typeDefinition)),
                1422028427
            );
        }
    }
434209
public static function resolveConcreteType(Endpoint $endpoint, PolymorphicDefinitionInterface $definition, $object)
    {
        //if discriminator map is set is used to get the value type
        if ($map = $definition->getDiscriminatorMap()) {
            //get concrete type based on property
            if ($definition->getDiscriminatorProperty()) {
                $property = $definition->getDiscriminatorProperty();
                $accessor = new PropertyAccessor();
                $propValue = $accessor->getValue($object, $property);
                $resolvedType = $map[$propValue] ?? null;
            }

            //get concrete type based on class
            if (!$resolvedType) {
                $class = DoctrineClassUtils::getClass($object);
                $resolvedType = $map[$class] ?? null;
            }
        }

        //final solution in case of not mapping is guess type based on class
        if (!$resolvedType && $definition instanceof InterfaceDefinition) {
            foreach ($definition->getImplementors() as $implementor) {
                $implementorDef = $endpoint->getType($implementor);
                if ($implementorDef instanceof ClassAwareDefinitionInterface
                    && $implementorDef->getClass() === DoctrineClassUtils::getClass($object)) {
                    $resolvedType = $implementorDef->getName();
                }
            }
        }

        if ($endpoint->hasType($resolvedType)) {
            $resolvedTypeDefinition = $endpoint->getType($resolvedType);
            if ($resolvedTypeDefinition instanceof PolymorphicDefinitionInterface) {
                $resolvedType = self::resolveConcreteType($endpoint, $resolvedTypeDefinition, $object);
            }
        }

        return $resolvedType;
    }
1239142
public function remove(TypeInterface $typeToRemove)
    {
        foreach ($this->collection as $key => $type) {
            if ($type === $typeToRemove) {
                unset($this->collection[$key]);

                return;
            }
        }

        throw new \RuntimeException(\sprintf('Cannot remove given type %s', \var_export($typeToRemove, true)));
    }
1119814
private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
    {
        if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
            $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
            $matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]);

            throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices));
        }

        if (!$typeHint->isInstantiable()) {
            $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
            throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface));
        }

        $argumentId = sprintf('autowired.%s', $typeHint->name);

        $argumentDefinition = $this->container->register($argumentId, $typeHint->name);
        $argumentDefinition->setPublic(false);

        $this->populateAvailableType($argumentId, $argumentDefinition);

        try {
            $this->completeDefinition($argumentId, $argumentDefinition);
        } catch (RuntimeException $e) {
            $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
            $message = sprintf('Unable to autowire argument of type "%s" for the service "%s". No services were found matching this %s and it cannot be auto-registered.', $typeHint->name, $id, $classOrInterface);
            throw new RuntimeException($message, 0, $e);
        }

        return new Reference($argumentId);
    }
759968
private MutableSecondaryTypeDefinition getSecondaryTypeDefinition(final String typeId, final boolean includePropertyDefinitions, final boolean baseType) {

		final TypeDefinitionFactory factory      = TypeDefinitionFactory.newInstance();
		final MutableSecondaryTypeDefinition def = factory.createSecondaryTypeDefinition(CmisVersion.CMIS_1_1, baseType ? null : BaseTypeId.CMIS_SECONDARY.value());

		def.setIsCreatable(false);

		initializeExtendedType(def, typeId);

		if (!includePropertyDefinitions) {
			def.removeAllPropertyDefinitions();
		}

		return def;
	}
1027747
protected function lexDefinition(): NodeInterface
    {
        if ($this->peek(TokenKindEnum::NAME)) {
            $token = $this->lexer->getToken();

            switch ($token->getValue()) {
                case KeywordEnum::QUERY:
                case KeywordEnum::MUTATION:
                case KeywordEnum::SUBSCRIPTION:
                case KeywordEnum::FRAGMENT:
                    return $this->lexExecutableDefinition();
                case KeywordEnum::SCHEMA:
                case KeywordEnum::SCALAR:
                case KeywordEnum::TYPE:
                case KeywordEnum::INTERFACE:
                case KeywordEnum::UNION:
                case KeywordEnum::ENUM:
                case KeywordEnum::INPUT:
                case KeywordEnum::DIRECTIVE:
                    return $this->lexTypeSystemDefinition();
                case KeywordEnum::EXTEND:
                    return $this->lexTypeSystemExtension();

            }
        } elseif ($this->peek(TokenKindEnum::BRACE_L)) {
            return $this->lexExecutableDefinition();
        } elseif ($this->peekDescription()) {
            return $this->lexTypeSystemDefinition();
        }

        throw $this->unexpected();
    }
1304036
private function resolveContainer(): ContainerInterface
    {
        if ($this->container === null) {
            $container = $this->containerFactory->build($this);

            if (!$container instanceof ContainerInterface) {
                throw new \RuntimeException(
                    "Invalid factory result, expected ContainerInterface"
                );
            }

            $this->container = $container;
        }

        return $this->container;
    }
692620
public function addType(DefinitionInterface $type): Endpoint
    {
        if ($this->hasType($type->getName())) {
            throw new \RuntimeException(sprintf('Duplicate definition for type with name "%s"', $type->getName()));
        }
        $this->types[$type->getName()] = $type;

        if ($type instanceof InterfaceDefinition) {
            $this->interfaces[$type->getName()] = $type;
        }

        if ($type instanceof ClassAwareDefinitionInterface) {
            if ($type->getClass()) {
                $class = $type->getClass();
                //all classes are saved without \ at the beginning
                $class = preg_replace('/^\\\\/', null, $class);
                $this->typeMap[$type->getName()] = $class;
            }
        }

        return $this;
    }
681292
public function updateType($repositoryId, TypeDefinitionInterface $type, ExtensionDataInterface $extension = null)
    {
        $url = $this->getRepositoryUrl($repositoryId);

        $url->getQuery()->modify(
            [
                Constants::CONTROL_CMISACTION => Constants::CMISACTION_UPDATE_TYPE,
                Constants::CONTROL_TYPE => json_encode($this->getJsonConverter()->convertFromTypeDefinition($type))
            ]
        );

        return $this->getJsonConverter()->convertTypeDefinition($this->postJson($url));
    }
721593
public function getFieldType($identifier)
    {
        if (!isset($this->fieldTypes[$identifier])) {
            throw new FieldTypeNotFoundException($identifier);
        }

        if ($this->fieldTypes[$identifier] instanceof SPIFieldType) {
            return $this->fieldTypes[$identifier];
        } elseif (is_callable($this->fieldTypes[$identifier])) {
            /** @var $closure \Closure */
            $closure = $this->fieldTypes[$identifier];
            $this->fieldTypes[$identifier] = $closure();
        }

        if (!$this->fieldTypes[$identifier] instanceof SPIFieldType) {
            throw new RuntimeException("\$fieldTypes[$identifier] must be instance of SPI\\FieldType\\FieldType or callable");
        }

        return $this->fieldTypes[$identifier];
    }
691237
public function createCmisBrowserBinding(array $sessionParameters, Cache $typeDefinitionCache = null)
    {
        $this->validateCmisBrowserBindingParameters($sessionParameters);

        return new CmisBinding(new Session(), $sessionParameters, $typeDefinitionCache);
    }
817030
protected function lexInputObjectTypeDefinition(): InputObjectTypeDefinitionNode
    {
        $start = $this->lexer->getToken();

        $description = $this->lexDescription();

        $this->expectKeyword(KeywordEnum::INPUT);

        return new InputObjectTypeDefinitionNode(
            $description,
            $this->lexName(),
            $this->lexDirectives(true),
            $this->lexInputFieldsDefinition(),
            $this->createLocation($start)
        );
    }
1151454
private function getCacheDefinition($type, ContainerBuilder $container)
    {
        $class  = $this->getDefinitionClass($type, $container);
        $object = new $class($type);

        return $object;
    }
10284
protected function getTypeDefinitionInternal($repositoryId, $typeId)
    {
        if (empty($repositoryId)) {
            throw new CmisInvalidArgumentException('Repository id must not be empty!');
        }

        if (empty($typeId)) {
            throw new CmisInvalidArgumentException('Type id must not be empty!');
        }

        // build URL
        $url = $this->getRepositoryUrl($repositoryId, Constants::SELECTOR_TYPE_DEFINITION);
        $url->getQuery()->modify([Constants::PARAM_TYPE_ID => $typeId]);

        return $this->getJsonConverter()->convertTypeDefinition(
            (array) $this->readJson($url)
        );
    }
1215645
protected function _getDefinitionResolver(DefinitionInterface $definition)
    {
        $definitionType = get_class($definition);

        if (! isset($this->_definitionResolvers[$definitionType])) {
            throw new NotFoundException(
                "No definition resolver was configured for definition " .
                "of type $definitionType"
            );
        }

        return $this->_definitionResolvers[$definitionType];
    }
993435
public function handle(TypeDefinitionNode $definition): Type
    {
        $nodeValue = new NodeValue($definition);

        return $this->pipeline
            ->send($nodeValue)
            ->through(
                $this->directiveFactory->createNodeMiddleware($definition)
            )
            ->via('handleNode')
            ->then(function (NodeValue $value) use ($definition): Type {
                $nodeResolver = $this->directiveFactory->createNodeResolver($definition);

                if ($nodeResolver) {
                    return $nodeResolver->resolveNode($value);
                }

                return $this->resolveType($definition);
            });
    }
684036
protected function copyFieldsFromInterface(InterfaceDefinition $intDef, FieldsAwareDefinitionInterface $fieldsAwareDefinition)
    {
        foreach ($intDef->getFields() as $field) {
            if (!$fieldsAwareDefinition->hasField($field->getName())) {
                $newField = clone $field;
                $newField->addInheritedFrom($intDef->getName());
                $fieldsAwareDefinition->addField($newField);
            } else {
                $fieldsAwareDefinition->getField($field->getName())->addInheritedFrom($intDef->getName());
            }
        }
    }