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

create() public method

public create ( string $resourceClass, array $options = [] ) : PropertyNameCollection
$resourceClass string
$options array
return ApiPlatform\Core\Metadata\Property\PropertyNameCollection
    public function create(string $resourceClass, array $options = []) : PropertyNameCollection
    {
        if ($this->decorated) {
            try {
                $propertyNameCollection = $this->decorated->create($resourceClass, $options);
            } catch (ResourceClassNotFoundException $resourceClassNotFoundException) {
                // Ignore not found exceptions from parent
            }
        }
        try {
            $reflectionClass = new \ReflectionClass($resourceClass);
        } catch (\ReflectionException $reflectionException) {
            if (isset($propertyNameCollection)) {
                return $propertyNameCollection;
            }
            throw new ResourceClassNotFoundException(sprintf('The resource class "%s" does not exist.', $resourceClass));
        }
        $propertyNames = [];
        // Properties
        foreach ($reflectionClass->getProperties() as $reflectionProperty) {
            if (null !== $this->reader->getPropertyAnnotation($reflectionProperty, ApiProperty::class)) {
                $propertyNames[$reflectionProperty->name] = true;
            }
        }
        // Methods
        foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) {
            $propertyName = $this->reflection->getProperty($reflectionMethod->name);
            if (!preg_match('/^[A-Z]{2,}/', $propertyName)) {
                $propertyName = lcfirst($propertyName);
            }
            if (null !== $propertyName && null !== $this->reader->getMethodAnnotation($reflectionMethod, ApiProperty::class)) {
                $propertyNames[$propertyName] = true;
            }
        }
        // Inherited from parent
        if (isset($propertyNameCollection)) {
            foreach ($propertyNameCollection as $propertyName) {
                $propertyNames[$propertyName] = $propertyName;
            }
        }
        return new PropertyNameCollection(array_keys($propertyNames));
    }

Usage Example

 /**
  * @expectedException \ApiPlatform\Core\Exception\ResourceClassNotFoundException
  * @expectedExceptionMessage The resource class "\DoNotExist" does not exist.
  */
 public function testClassDoesNotExist()
 {
     $reader = $this->prophesize(Reader::class);
     $factory = new AnnotationPropertyNameCollectionFactory($reader->reveal());
     $factory->create('\\DoNotExist');
 }
AnnotationPropertyNameCollectionFactory