ApiPlatform\Core\Metadata\Property\Factory\AnnotationPropertyMetadataFactory::create PHP Method

create() public method

public create ( string $resourceClass, string $property, array $options = [] ) : PropertyMetadata
$resourceClass string
$property string
$options array
return ApiPlatform\Core\Metadata\Property\PropertyMetadata
    public function create(string $resourceClass, string $property, array $options = []) : PropertyMetadata
    {
        $parentPropertyMetadata = null;
        if ($this->decorated) {
            try {
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
            } catch (PropertyNotFoundException $propertyNotFoundException) {
                // Ignore not found exception from decorated factories
            }
        }
        try {
            $reflectionClass = new \ReflectionClass($resourceClass);
        } catch (\ReflectionException $reflectionException) {
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
        }
        if ($reflectionClass->hasProperty($property)) {
            $annotation = $this->reader->getPropertyAnnotation($reflectionClass->getProperty($property), ApiProperty::class);
            if (null !== $annotation) {
                return $this->createMetadata($annotation, $parentPropertyMetadata);
            }
        }
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
            $methodName = $prefix . ucfirst($property);
            if (!$reflectionClass->hasMethod($methodName)) {
                continue;
            }
            $reflectionMethod = $reflectionClass->getMethod($methodName);
            if (!$reflectionMethod->isPublic()) {
                continue;
            }
            $annotation = $this->reader->getMethodAnnotation($reflectionMethod, ApiProperty::class);
            if (null !== $annotation) {
                return $this->createMetadata($annotation, $parentPropertyMetadata);
            }
        }
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
    }

Usage Example

 public function testClassNotFoundButParentFound()
 {
     $propertyMetadata = new PropertyMetadata();
     $decoratedProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
     $decoratedProphecy->create('\\DoNotExist', 'foo', [])->willReturn($propertyMetadata);
     $factory = new AnnotationPropertyMetadataFactory($this->prophesize(Reader::class)->reveal(), $decoratedProphecy->reveal());
     $this->assertEquals($propertyMetadata, $factory->create('\\DoNotExist', 'foo'));
 }