Passed
Pull Request — master (#4)
by Pieter
01:45
created

DiscriminatorColumn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess\Getters;
4
5
use Symfony\Component\PropertyInfo\Type;
6
use W2w\Lib\ApieObjectAccessNormalizer\Getters\GetterInterface;
7
8
class DiscriminatorColumn implements GetterInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $name;
14
15
    /**
16
     * @var array
17
     */
18
    private $discriminatorMapping;
19
20
    public function __construct(string $name, array $discriminatorMapping)
21
    {
22
        $this->name = $name;
23
        $this->discriminatorMapping = $discriminatorMapping;
24
    }
25
26
    public function getName(): string
27
    {
28
        return $this->name;
29
    }
30
31
    public function getValue($object)
32
    {
33
        $objectClassName = get_class($object);
34
        foreach ($this->discriminatorMapping as $value => $className) {
35
            if ($objectClassName === $className) {
36
                return $value;
37
            }
38
        }
39
        foreach ($this->discriminatorMapping as $value => $className) {
40
            if (is_a($objectClassName, $className)) {
41
                return $value;
42
            }
43
        }
44
        return null;
45
    }
46
47
    public function toType(): ?Type
48
    {
49
        return new Type(Type::BUILTIN_TYPE_STRING);
50
    }
51
52
    public function getPriority(): int
53
    {
54
        return 15;
55
    }
56
}
57