Completed
Push — master ( ec8ced...26f03f )
by Kévin
13s
created

InheritedPropertyNameCollectionFactory::create()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 11
nc 10
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Metadata\Property\Factory;
15
16
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
17
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
18
19
/**
20
 * Creates a property name collection from eventual child inherited properties.
21
 *
22
 * @author Antoine Bluchet <[email protected]>
23
 */
24
final class InheritedPropertyNameCollectionFactory implements PropertyNameCollectionFactoryInterface
25
{
26
    private $resourceNameCollection;
27
    private $decorated;
28
29
    public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollection, PropertyNameCollectionFactoryInterface $decorated = null)
30
    {
31
        $this->resourceNameCollection = $resourceNameCollection;
32
        $this->decorated = $decorated;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function create(string $resourceClass, array $options = []): PropertyNameCollection
39
    {
40
        $propertyNames = [];
41
42
        // Inherited from parent
43
        if ($this->decorated) {
44
            foreach ($this->decorated->create($resourceClass, $options) as $propertyName) {
45
                $propertyNames[$propertyName] = (string) $propertyName;
46
            }
47
        }
48
49
        foreach ($this->resourceNameCollection->create() as $knownResourceClass) {
50
            if ($resourceClass === $knownResourceClass) {
51
                continue;
52
            }
53
54
            if (is_subclass_of($knownResourceClass, $resourceClass)) {
55
                foreach ($this->create($knownResourceClass) as $propertyName) {
56
                    $propertyNames[$propertyName] = $propertyName;
57
                }
58
            }
59
        }
60
61
        return new PropertyNameCollection(array_values($propertyNames));
62
    }
63
}
64