Passed
Pull Request — 2.0 (#36)
by Donald
04:39 queued 02:10
created

Key::isPossessive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 3
     * @return string                   the key with the index, or just the key if index is null.
43
     */
44 3
    public function build($key, $index)
45 1
    {
46
        if ($index === null) {
47
            return $key;
48 2
        }
49
50 2
        $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 24
     * @return string                   the ordinal
61
     */
62 24
    public function getOrdinal($nth)
63 1
    {
64
        if ($nth < 0) {
65
            throw new InvalidArgumentException('$nth must be a positive number');
66 23
        }
67
68
        return $nth > 9 && $nth < 20 ? self::ORDINAL_TH : self::$ordinals[substr($nth, -1)];
69
    }
70
71
    /**
72
     * Parses a key into the separate key and index value.
73
     *
74
     * @example parseKey("Item"): ["Item", null]
75
     * @example parseKey("Item", 1): ["Item", 1]
76
     * @example parseKey("1st Item"): ["Item", 0]
77
     * @example parseKey("2nd Item"): ["Item", 1]
78
     * @example parseKey("3rd Item"): ["Item", 2]
79
     *
80
     * @param  string                   $key   the key to parse.
81
     * @param  int                      $index [optional] the index to return if the key does not contain one.
82
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
83
     *                                        that does not match the index.
84
     * @return array                    a tuple, the 1st being the key with the nth removed, and the 2nd being the
85 14
     *                                        index.
86
     */
87 14
    public function parse($key, $index = null)
88 11
    {
89 5
        if (preg_match('/^([1-9][0-9]*)(?:st|nd|rd|th) (.+)$/', $key, $matches)) {
90 5
            if ($index !== null && $index != $matches[1] - 1) {
91
                throw new InvalidArgumentException(
92
                    "$index was provided for index param when key '$key' contains an nth value, but they do not match"
93
                );
94 6
            }
95 6
96
            $index = $matches[1] - 1;
97
            $key = $matches[2];
98 9
        }
99
100
        return [$key, $index];
101
    }
102
103
    /**
104
     * Determines if the specified key is a possessive noun
105
     *
106
     * @param  string $key
107
     * @return bool
108
     */
109
    protected function isPossessive($key) {
110
      return strpos($key, self::POSSESSION) !== false;
111
    }
112
}
113