Completed
Push — master ( 2f74f3...29426d )
by Samson
02:55
created

GeezParser::pushToQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
    public function __construct($geez_number)
32
    {
33
        $this->setGeezNumber($geez_number);
34
        $this->parsed = null;
35
    }
36
37
    /**
38
     * @param $geez_number
39
     *
40
     * @throws NotGeezArgumentException
41
     */
42
    protected function setGeezNumber($geez_number)
43
    {
44
        if (!is_string($geez_number)) {
45
            throw new NotGeezArgumentException(gettype($geez_number));
46
        }
47
48
        $this->geez_number = $geez_number;
49
    }
50
51
    public function getParsed()
52
    {
53
        return $this->parsed;
54
    }
55
56
    /**
57
     * Swing the magic wand and say the spell.
58
     */
59
    public function parse()
60
    {
61
        $this->parsed = new Queue();
62
63
        $block = 0;
64
65
        $length = $this->getLength($this->geez_number);
66
67
        for ($index = 0; $index < $length; $index++) {
68
            $this->parseCharacter($index, $block);
69
        }
70
71
        $this->pushToQueue($block, 1);
72
    }
73
74
    /**
75
     * Get the length of the string.
76
     *
77
     * @param $geez_number
78
     *
79
     * @return int
80
     */
81
    protected function getLength($geez_number)
82
    {
83
        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
    protected function parseCharacter($index, &$block)
95
    {
96
        $ascii_number = $this->parseGeezAtIndex($index);
97
98
        if ($this->isNotGeezSeparator($ascii_number)) {
99
            $block += $ascii_number;
100
        } else {
101
            $this->pushToQueue($block, $ascii_number);
102
            $block = 0;
103
        }
104
    }
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
    protected function parseGeezAtIndex($index)
116
    {
117
        $geez_char = $this->getCharacterAt($this->geez_number, $index);
118
119
        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
    protected function getCharacterAt($geez_number, $index)
131
    {
132
        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
    protected function getAsciiNumber($geez_number)
145
    {
146
        $ascii_number = array_search($geez_number, Converter::GEEZ_NUMBERS, true);
147
148
        if ($ascii_number === false) {
149
            throw new NotGeezArgumentException($geez_number);
150
        }
151
152
        return $ascii_number;
153
    }
154
155
    /**
156
     * @param $ascii_number
157
     *
158
     * @return bool
159
     */
160
    protected function isNotGeezSeparator($ascii_number)
161
    {
162
        return $ascii_number < 99;
163
    }
164
165
    /**
166
     * Push to the queue.
167
     *
168
     * @param $block
169
     * @param $separator
170
     */
171
    protected function pushToQueue($block, $separator)
172
    {
173
        $this->parsed->push(
174
            compact('block', 'separator')
175
        );
176
    }
177
}
178