|
1
|
|
|
<?php namespace Chekote\NounStore; |
|
2
|
|
|
|
|
3
|
|
|
use InvalidArgumentException; |
|
4
|
|
|
|
|
5
|
|
|
class Key |
|
6
|
|
|
{ |
|
7
|
|
|
use Singleton; |
|
8
|
|
|
|
|
9
|
|
|
const ORDINAL_ST = 'st'; |
|
10
|
|
|
const ORDINAL_ND = 'nd'; |
|
11
|
|
|
const ORDINAL_RD = 'rd'; |
|
12
|
|
|
const ORDINAL_TH = 'th'; |
|
13
|
|
|
|
|
14
|
|
|
protected static $ordinals = [ |
|
15
|
|
|
0 => self::ORDINAL_TH, |
|
16
|
|
|
1 => self::ORDINAL_ST, |
|
17
|
|
|
2 => self::ORDINAL_ND, |
|
18
|
|
|
3 => self::ORDINAL_RD, |
|
19
|
|
|
4 => self::ORDINAL_TH, |
|
20
|
|
|
5 => self::ORDINAL_TH, |
|
21
|
|
|
6 => self::ORDINAL_TH, |
|
22
|
|
|
7 => self::ORDINAL_TH, |
|
23
|
|
|
8 => self::ORDINAL_TH, |
|
24
|
|
|
9 => self::ORDINAL_TH, |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Builds a key from it's separate key and index values. |
|
29
|
|
|
* |
|
30
|
|
|
* @example buildKey("Item", null): "Item" |
|
31
|
|
|
* @example buildKey("Item", 0): "1st Item" |
|
32
|
|
|
* @example buildKey("Item", 1): "2nd Item" |
|
33
|
|
|
* @example buildKey("Item", 2): "3rd Item" |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $key The key to check. |
|
36
|
|
|
* @param int $index The index (zero indexed) value for the key. If not specified, the method |
|
37
|
|
|
* will not add an index notation to the key. |
|
38
|
|
|
* @throws InvalidArgumentException if $key is not a string. |
|
39
|
|
|
* @throws InvalidArgumentException if $index is not an int. |
|
40
|
|
|
* @return string the key with the index, or just the key if index is null. |
|
41
|
|
|
*/ |
|
42
|
7 |
|
public function build($key, $index) |
|
43
|
|
|
{ |
|
44
|
7 |
|
if ($index === null) { |
|
45
|
1 |
|
return $key; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
$nth = $index + 1; |
|
49
|
|
|
|
|
50
|
6 |
|
return $nth . $this->getOrdinal($nth) . ' ' . $key; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Provides the ordinal notation for the specified nth number. |
|
55
|
|
|
* |
|
56
|
|
|
* @param int $nth the number to determine the ordinal for |
|
57
|
|
|
* @return string the ordinal |
|
58
|
|
|
*/ |
|
59
|
30 |
|
public function getOrdinal($nth) |
|
60
|
|
|
{ |
|
61
|
30 |
|
if ($nth < 0) { |
|
62
|
1 |
|
throw new InvalidArgumentException('$nth must be a positive number'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
29 |
|
return $nth > 9 && $nth < 20 ? self::ORDINAL_TH : self::$ordinals[substr($nth, -1)]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Parses a key into the separate key and index value. |
|
70
|
|
|
* |
|
71
|
|
|
* @example parseKey("Item"): ["Item", null] |
|
72
|
|
|
* @example parseKey("Item", 1): ["Item", 1] |
|
73
|
|
|
* @example parseKey("1st Item"): ["Item", 0] |
|
74
|
|
|
* @example parseKey("2nd Item"): ["Item", 1] |
|
75
|
|
|
* @example parseKey("3rd Item"): ["Item", 2] |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $key the key to parse. |
|
78
|
|
|
* @param int $index [optional] the index to return if the key does not contain one. |
|
79
|
|
|
* @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value |
|
80
|
|
|
* that does not match the index. |
|
81
|
|
|
* @return array a tuple, the 1st being the key with the nth removed, and the 2nd being the |
|
82
|
|
|
* index. |
|
83
|
|
|
*/ |
|
84
|
36 |
|
public function parse($key, $index = null) |
|
85
|
|
|
{ |
|
86
|
36 |
|
if (preg_match('/^([1-9][0-9]*)(?:st|nd|rd|th) (.+)$/', $key, $matches)) { |
|
87
|
23 |
|
if ($index !== null && $index != $matches[1] - 1) { |
|
88
|
8 |
|
throw new InvalidArgumentException( |
|
89
|
8 |
|
"$index was provided for index param when key '$key' contains an nth value, but they do not match" |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
15 |
|
$index = $matches[1] - 1; |
|
94
|
15 |
|
$key = $matches[2]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
28 |
|
return [$key, $index]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|