GeezParser::pushToQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Geezify\Helper;
4
5
use Geezify\Converter\Converter;
6
use Geezify\Exception\NotGeezArgumentException;
7
use SplQueue as Queue;
8
9
/**
10
 * GeezParser parse the geez number to a queue.
11
 */
12
class GeezParser
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $geez_number;
18
19
    /**
20
     * @var Queue
21
     */
22
    protected $parsed;
23
24
    /**
25
     * GeezParser constructor.
26
     *
27
     * @param $geez_number
28
     *
29
     * @throws NotGeezArgumentException
30
     */
31 99
    public function __construct($geez_number)
32
    {
33 99
        $this->setGeezNumber($geez_number);
34 94
        $this->parsed = null;
35 94
    }
36
37
    /**
38
     * @param $geez_number
39
     *
40
     * @throws NotGeezArgumentException
41
     */
42 99
    protected function setGeezNumber($geez_number)
43
    {
44 99
        if (!is_string($geez_number)) {
45 5
            throw new NotGeezArgumentException(gettype($geez_number));
46
        }
47
48 94
        $this->geez_number = $geez_number;
49 94
    }
50
51 91
    public function getParsed()
52
    {
53 91
        return $this->parsed;
54
    }
55
56
    /**
57
     * Swing the magic wand and say the spell.
58
     */
59 94
    public function parse()
60
    {
61 94
        $this->parsed = new Queue();
62
63 94
        $block = 0;
64
65 94
        $length = $this->getLength($this->geez_number);
66
67 94
        for ($index = 0; $index < $length; $index++) {
68 94
            $this->parseCharacter($index, $block);
69
        }
70
71 91
        $this->pushToQueue($block, 1);
72 91
    }
73
74
    /**
75
     * Get the length of the string.
76
     *
77
     * @param $geez_number
78
     *
79
     * @return int
80
     */
81 94
    protected function getLength($geez_number)
82
    {
83 94
        return \mb_strlen($geez_number, 'UTF-8');
84
    }
85
86
    /**
87
     * Parse a geez character.
88
     *
89
     * @param $index integer
90
     * @param $block integer
91
     *
92
     * @throws \Geezify\Exception\NotGeezArgumentException
93
     */
94 94
    protected function parseCharacter($index, &$block)
95
    {
96 94
        $ascii_number = $this->parseGeezAtIndex($index);
97
98 92
        if ($this->isNotGeezSeparator($ascii_number)) {
99 84
            $block += $ascii_number;
100
        } else {
101 82
            $this->pushToQueue($block, $ascii_number);
102 82
            $block = 0;
103
        }
104 92
    }
105
106
    /**
107
     * Get the ascii number from geez number string.
108
     *
109
     * @param $index
110
     *
111
     * @throws \Geezify\Exception\NotGeezArgumentException
112
     *
113
     * @return int
114
     */
115 94
    protected function parseGeezAtIndex($index)
116
    {
117 94
        $geez_char = $this->getCharacterAt($this->geez_number, $index);
118
119 94
        return $this->getAsciiNumber($geez_char);
120
    }
121
122
    /**
123
     * Fetch z character at $index from the geez number string.
124
     *
125
     * @param $geez_number
126
     * @param $index
127
     *
128
     * @return string
129
     */
130 94
    protected function getCharacterAt($geez_number, $index)
131
    {
132 94
        return \mb_substr($geez_number, $index, 1, 'UTF-8');
133
    }
134
135
    /**
136
     * Convert geez number character to ascii.
137
     *
138
     * @param $geez_number
139
     *
140
     * @throws NotGeezArgumentException
141
     *
142
     * @return int
143
     */
144 94
    protected function getAsciiNumber($geez_number)
145
    {
146 94
        $ascii_number = array_search($geez_number, Converter::GEEZ_NUMBERS, true);
147
148 94
        if ($ascii_number === false) {
149 3
            throw new NotGeezArgumentException($geez_number);
150
        }
151
152 92
        return $ascii_number;
153
    }
154
155
    /**
156
     * @param $ascii_number
157
     *
158
     * @return bool
159
     */
160 92
    protected function isNotGeezSeparator($ascii_number)
161
    {
162 92
        return $ascii_number < 99;
163
    }
164
165
    /**
166
     * Push to the queue.
167
     *
168
     * @param $block
169
     * @param $separator
170
     */
171 91
    protected function pushToQueue($block, $separator)
172
    {
173 91
        $this->parsed->push(
174 91
            compact('block', 'separator')
175
        );
176 91
    }
177
}
178