Passed
Pull Request — master (#76)
by
unknown
14:38
created

Type::equals()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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
    public const string MONTHLY = 'monthly';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 20 at column 24
Loading history...
21
22
    /**
23
     * @var int|string
24
     */
25
    protected $id;
26
27
    /** @var null|string|float */
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
    public function isDefined(): bool
90
    {
91
        return $this->id !== null || $this->name !== null;
92
    }
93
94
    public function isMonthly(): bool
95
    {
96
        return $this->belongsToGroup(self::MONTHLY);
97
    }
98
99
    public function belongsToGroup(string $group): bool
100
    {
101
        return $this->groupName() === $group;
102
    }
103
104
    public function groupName(): string
105
    {
106
        $groupIndex = 0;
107
108
        return $this->extractNamePart($groupIndex);
109
    }
110
111
    /**
112
     * @param int $index - 0 - group index, 1 - local index
113
     * @return string
114
     */
115
    private function extractNamePart(int $index): string
116
    {
117
        $name = $this->getName();
118
        if (strpos($name, ',') > 0) {
119
            $parts = explode(',', $name, 2);
120
        } else {
121
            return $name;
122
        }
123
124
        return $parts[$index] ?? '';
125
    }
126
127
    public function belongsToLocalCategory(string $local): bool
128
    {
129
        return $this->localName() === $local;
130
    }
131
132
    public function localName(): string
133
    {
134
        $localIndex = 1;
135
136
        return $this->extractNamePart($localIndex);
137
    }
138
}
139