Passed
Pull Request — master (#66)
by Alex
02:50
created

TypeAnnotationHelpers::findPropertyBinding()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 1
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlgoWeb\ODataMetadata\Helpers;
6
7
use AlgoWeb\ODataMetadata\Interfaces\Annotations\IPropertyValueBinding;
8
use AlgoWeb\ODataMetadata\Interfaces\IProperty;
9
10
/**
11
 * Trait TypeAnnotationHelpers.
12
 * @package AlgoWeb\ODataMetadata\Helpers
13
 */
14
trait TypeAnnotationHelpers
15
{
16
    /**
17
     * Gets the binding of a property of the type term of a type annotation.
18
     *
19
     * @param  IProperty|string           $property property (Or Property Name) to search for
20
     * @return IPropertyValueBinding|null the binding of the property in the type annotation, or null if no binding
21
     *                                             exists
22
     */
23
    public function findPropertyBinding($property): ?IPropertyValueBinding
24
    {
25
        assert(
26
            $property instanceof IProperty || is_string($property), /** @phpstan-ignore-line */
27
            'The property to search for must either be a string representing the name or IProperty' .
28
            ' representing the property'
29
        );
30
        /** @var string $nuProperty */
31
        $nuProperty = $property instanceof IProperty ? $property->getName() : $property;
32
        foreach ($this->getPropertyValueBindings() as $propertyBinding) {
33
            $prop = $propertyBinding->getBoundProperty();
34
            if ($prop->getName() == $nuProperty) {
35
                return $propertyBinding;
36
            }
37
        }
38
        return null;
39
    }
40
41
    /**
42
     * @return IPropertyValueBinding[] gets the value annotations for the properties of the type
43
     */
44
    abstract public function getPropertyValueBindings(): array;
45
}
46