Type::jsonSerialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\type;
12
13
/**
14
 * General Type.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
class Type implements TypeInterface
19
{
20
    /**
21
     * @var int|string
22
     */
23
    protected $id;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30 47
    public function __construct($id, $name = null)
31
    {
32 47
        $this->id = $id;
33 47
        $this->name = $name;
34 47
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 7
    public function getId()
40
    {
41 7
        return $this->id;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 29
    public function getName(): ?string
48
    {
49 29
        return $this->name;
50
    }
51
52 2
    public function hasName(): bool
53
    {
54 2
        return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE;
55
    }
56
57 2
    public function getUniqueId()
58
    {
59 2
        return $this->hasName() ? $this->name : $this->id;
60
    }
61
62 2
    public function equals(TypeInterface $other): bool
63
    {
64 2
        return $this->id === $other->getId() &&
65 2
            $this->name === $other->getName();
66
    }
67
68 22
    public function matches(TypeInterface $other): bool
69
    {
70 22
        return $this->id === self::ANY || $other->getId() === self::ANY
71 18
            ? $this->checkMatches($this->name, $other->getName())
72 22
            : $this->checkMatches($this->id, $other->getId());
73
    }
74
75 22
    protected function checkMatches($lhs, $rhs)
76
    {
77 22
        if ($lhs === self::NONE || $rhs === self::NONE) {
78 1
            return false;
79
        }
80
81 22
        return (string) $lhs === (string) $rhs;
82
    }
83
84 1
    public function jsonSerialize(): array
85
    {
86 1
        return array_filter(get_object_vars($this));
87
    }
88
}
89