1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\CLDR; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\CLDR\Plurals\Rule; |
15
|
|
|
use ICanBoogie\CLDR\Plurals\Samples; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Representation of plurals |
19
|
|
|
*/ |
20
|
|
|
class Plurals extends \ArrayObject |
21
|
|
|
{ |
22
|
|
|
const COUNT_ZERO = 'zero'; |
23
|
|
|
const COUNT_ONE = 'one'; |
24
|
|
|
const COUNT_TWO = 'two'; |
25
|
|
|
const COUNT_FEW = 'few'; |
26
|
|
|
const COUNT_MANY = 'many'; |
27
|
|
|
const COUNT_OTHER = 'other'; |
28
|
|
|
|
29
|
|
|
const RULE_COUNT_PREFIX = 'pluralRule-count-'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Rule[][] |
33
|
|
|
*/ |
34
|
|
|
private $rules = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Samples[][] |
38
|
|
|
*/ |
39
|
|
|
private $samples = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param number $number |
43
|
|
|
* @param string $locale |
44
|
|
|
* |
45
|
|
|
* @return string One of `COUNT_*`. |
46
|
|
|
*/ |
47
|
|
|
public function rule_for($number, $locale) |
48
|
|
|
{ |
49
|
|
|
foreach ($this->rule_instances_for($locale) as $count => $rule) |
50
|
|
|
{ |
51
|
|
|
if ($rule->validate($number)) |
52
|
|
|
{ |
53
|
|
|
return $count; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return self::COUNT_OTHER; // @codeCoverageIgnore |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $locale |
62
|
|
|
* |
63
|
|
|
* @return string[] |
64
|
|
|
*/ |
65
|
|
|
public function rules_for($locale) |
66
|
|
|
{ |
67
|
|
|
return array_keys($this->rule_instances_for($locale)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $locale |
72
|
|
|
* |
73
|
|
|
* @return Samples[] |
74
|
|
|
*/ |
75
|
|
|
public function samples_for($locale) |
76
|
|
|
{ |
77
|
|
|
$samples = &$this->samples[$locale]; |
78
|
|
|
|
79
|
|
|
return $samples ?: $samples = $this->create_samples_for($locale); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $locale |
84
|
|
|
* |
85
|
|
|
* @return Rule[] |
86
|
|
|
*/ |
87
|
|
|
private function rule_instances_for($locale) |
88
|
|
|
{ |
89
|
|
|
$rules = &$this->rules[$locale]; |
90
|
|
|
|
91
|
|
|
return $rules ?: $rules = $this->create_rules_for($locale); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $locale |
96
|
|
|
* |
97
|
|
|
* @return Rule[] |
98
|
|
|
*/ |
99
|
|
View Code Duplication |
private function create_rules_for($locale) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$rules = []; |
102
|
|
|
$prefix_length = strlen(self::RULE_COUNT_PREFIX); |
103
|
|
|
|
104
|
|
|
foreach ($this[$locale] as $count => $rule_string) |
105
|
|
|
{ |
106
|
|
|
$count = substr($count, $prefix_length); |
107
|
|
|
$rules[$count] = Rule::from($this->extract_rule($rule_string)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $rules; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $rule_string |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
private function extract_rule($rule_string) |
119
|
|
|
{ |
120
|
|
|
$rule = explode('@', $rule_string, 2); |
121
|
|
|
$rule = array_shift($rule); |
122
|
|
|
$rule = trim($rule); |
123
|
|
|
|
124
|
|
|
return $rule; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $locale |
129
|
|
|
* |
130
|
|
|
* @return Samples[] |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
private function create_samples_for($locale) |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$samples = []; |
135
|
|
|
$prefix_length = strlen(self::RULE_COUNT_PREFIX); |
136
|
|
|
|
137
|
|
|
foreach ($this[$locale] as $count => $rule_string) |
138
|
|
|
{ |
139
|
|
|
$count = substr($count, $prefix_length); |
140
|
|
|
$samples[$count] = Samples::from($this->extract_samples($rule_string)); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $samples; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $rule_string |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
private function extract_samples($rule_string) |
152
|
|
|
{ |
153
|
|
|
return substr($rule_string, strpos($rule_string, '@')); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.