Passed
Pull Request — master (#76)
by
unknown
15:12
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|null|float The unique identifier of the type. Can be an integer or string.
24
     *                            Special values:
25
     *                            - `Type::ANY` indicates that the type can match any ID.
26
     *                            - `Type::NONE` indicates that there is no valid ID.
27
     */
28
    protected $id;
29
30 47
    /**
31
     * @var string|null|float The name of the type. Can be a specific name or one of the special values:
32 47
     *                        - `Type::ANY` indicates that the type can match any name.
33 47
     *                        - `Type::NONE` indicates that there is no valid name.
34 47
     */
35
    protected $name;
36
37
    public function __construct($id, $name = self::ANY)
38
    {
39 7
        $this->id = $id;
40
        $this->name = $name;
41 7
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getId()
47 29
    {
48
        return $this->id;
49 29
    }
50
51
    /**
52 2
     * {@inheritdoc}
53
     */
54 2
    public function getName(): ?string
55
    {
56
        return $this->name;
57 2
    }
58
59 2
    public function hasName(): bool
60
    {
61
        return !empty($this->name) && $this->name !== self::ANY && $this->name !== self::NONE;
62 2
    }
63
64 2
    public function getUniqueId()
65 2
    {
66
        return $this->hasName() ? $this->name : $this->id;
67
    }
68 22
69
    public function equals(TypeInterface $other): bool
70 22
    {
71 18
        return $this->id === $other->getId() &&
72 22
            $this->name === $other->getName();
73
    }
74
75 22
    public function matches(TypeInterface $other): bool
76
    {
77 22
        return $this->id === self::ANY || $other->getId() === self::ANY
78 1
            ? $this->checkMatches($this->name, $other->getName())
79
            : $this->checkMatches($this->id, $other->getId());
80
    }
81 22
82
    protected function checkMatches($lhs, $rhs)
83
    {
84 1
        if ($lhs === self::NONE || $rhs === self::NONE) {
85
            return false;
86 1
        }
87
88
        return (string) $lhs === (string) $rhs;
89
    }
90
91
    public function jsonSerialize(): array
92
    {
93
        return array_filter(get_object_vars($this));
94
    }
95
96
    public function isDefined(): bool
97
    {
98
        return $this->id !== null || $this->name !== null;
99
    }
100
101
    public static function anyId($name): TypeInterface
102
    {
103
        return new self(self::ANY, $name);
104
    }
105
106
    public function isMonthly(): bool
107
    {
108
        return $this->belongsToGroup(self::MONTHLY);
109
    }
110
111
    public function belongsToGroup(string $group): bool
112
    {
113
        return $this->groupName() === $group;
114
    }
115
116
    public function groupName(): string
117
    {
118
        $groupIndex = 0;
119
120
        return $this->extractNamePart($groupIndex);
121
    }
122
123
    /**
124
     * @param int $index - 0 - group index, 1 - local index
125
     * @return string
126
     */
127
    private function extractNamePart(int $index): string
128
    {
129
        $name = $this->getName();
130
        if (strpos($name, ',') > 0) {
131
            $parts = explode(',', $name, 2);
132
        } else {
133
            return $name;
134
        }
135
136
        return $parts[$index] ?? '';
137
    }
138
139
    public function belongsToLocalCategory(string $local): bool
140
    {
141
        return $this->localName() === $local;
142
    }
143
144
    public function localName(): string
145
    {
146
        $localIndex = 1;
147
148
        return $this->extractNamePart($localIndex);
149
    }
150
}
151