Failed Conditions
Push — complex_graph ( 39e222...60f2a1 )
by Donald
01:38
created

Key::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
    const POSSESSION = "'s ";
28
29
    /**
30
     * Builds a key from it's separate key and index values.
31
     *
32
     * @example buildKey("Item", null): "Item"
33
     * @example buildKey("Item", 0): "1st Item"
34
     * @example buildKey("Item", 1): "2nd Item"
35
     * @example buildKey("Item", 2): "3rd Item"
36
     *
37
     * @param  string                   $key   The key to check.
38
     * @param  int|null                 $index The index (zero indexed) value for the key. If not specified, the method
39
     *                                         will not add an index notation to the key.
40
     * @throws InvalidArgumentException if $index is less than -1. Note: It should really be zero or higher, but this
41
     *                                        method does not assert that. The error is bubbling up from getOrdinal()
42
     * @return string                   the key with the index, or just the key if index is null.
43
     */
44
    public function build($key, $index)
45
    {
46
        if ($index === null) {
47
            return $key;
48
        }
49
50
        $nth = $index + 1;
51
52
        return $nth . $this->getOrdinal($nth) . ' ' . $key;
53
    }
54
55
    /**
56
     * Provides the ordinal notation for the specified nth number.
57
     *
58
     * @param  int                      $nth the number to determine the ordinal for
59
     * @throws InvalidArgumentException if $nth is not a positive number.
60
     * @return string                   the ordinal
61
     */
62
    public function getOrdinal($nth)
63
    {
64
        if ($nth < 0) {
65
            throw new InvalidArgumentException('$nth must be a positive number');
66
        }
67
68
        return $nth > 9 && $nth < 20 ? self::ORDINAL_TH : self::$ordinals[substr($nth, -1)];
69
    }
70
71
    /**
72
     * Parses a key into dot-notation.
73
     *
74
     * @example parseNoun("Customer"): "Customer"
75
     * @example parseNoun("2nd Customer"): "Customer.1"
76
     * @example parseNoun("Customer's Car"): "Customer.Car"
77
     * @example parseNoun("2nd Customer's Car"): "Customer.1.Car"
78
     * @example parseNoun("4th Customer's 3rd Car"): "Customer.3.Car.2"
79
     *
80
     * @param  string                   $key the key to parse.
81
     * @throws InvalidArgumentException if the key syntax is invalid.
82
     * @return string                   the dot-notation string.
83
     */
84
    public function parse($key)
85
    {
86
        return join('.', array_map([$this, 'parseNoun'], $this->splitPossessions($key)));
87
    }
88
89
    /**
90
     * Parses a single noun into dot-notation.
91
     *
92
     * @example parseNoun("Item"): "Item"
93
     * @example parseNoun("1st Item"): "Item.0"
94
     * @example parseNoun("2nd Item"): "Item.1"
95
     * @example parseNoun("3rd Item"): "Item.2"
96
     *
97
     * @param  string                   $noun the key to parse.
98
     * @throws InvalidArgumentException if the key syntax is invalid.
99
     * @return string                   the dot-notation string.
100
     */
101
    public function parseNoun($noun)
102
    {
103
        if (!preg_match("/^(([1-9][0-9]*)(?:st|nd|rd|th) )?([^']+)$/", $noun, $matches)) {
104
            throw new InvalidArgumentException('Key syntax is invalid');
105
        }
106
107
        // @todo use null coalescing operator when upgrading to PHP 7
108
        $index = isset($matches[2]) && $matches[2] !== '' ? $matches[2] - 1 : null;
109
        $noun = $matches[3];
110
111
        return $index !== null ? "$noun.$index" : $noun;
112
    }
113
114
    /**
115
     * Splits a possessive key into its separate nouns.
116
     *
117
     * @example splitPossessions("Customer's Car"): ['Customer', 'Car']
118
     * @example splitPossessions("8th Customer's Car"): ['8th Customer', 'Car']
119
     * @example splitPossessions("Customer's 2nd Car"): ['Customer', '2nd Car']
120
     * @example splitPossessions("7th Customer's 4th Car"): ['7th Customer', '4th Car']
121
     * @example splitPossessions("7th Customer's 4th Car's 2nd Wheel"): ['7th Customer', '4th Car', '2nd Wheel']
122
     *
123
     * @param  string   $key the possessive key to parse
124
     * @return string[] an array of nouns
125
     */
126
    protected function splitPossessions($key)
127
    {
128
        return ($nouns = preg_split('/' . self::POSSESSION . '/', $key)) ? $nouns : [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $nouns = preg_spl...key) ? $nouns : array() returns an array which contains values of type array which are incompatible with the documented value type string.
Loading history...
129
    }
130
}
131