Passed
Pull Request — master (#12)
by Donald
01:39
created

Key::getOrdinal()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 4
rs 9.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
    /**
28
     * Provides the ordinal notation for the specified nth number.
29
     *
30
     * @param  int    $nth the number to determine the ordinal for
31
     * @return string the ordinal
32
     */
33 38
    public function getOrdinal($nth)
34
    {
35 38
        if ($nth < 0) {
36 1
            throw new InvalidArgumentException('$nth must be a positive number');
37
        }
38
39 37
        return $nth > 9 && $nth < 20 ? self::ORDINAL_TH : self::$ordinals[substr($nth, -1)];
40
    }
41
42
    /**
43
     * Parses a key into the separate key and index value.
44
     *
45
     * @example parseKey("Item"): ["Item", null]
46
     * @example parseKey("Item", 1): ["Item", 1]
47
     * @example parseKey("1st Item"): ["Item", 0]
48
     * @example parseKey("2nd Item"): ["Item", 1]
49
     * @example parseKey("3rd Item"): ["Item", 2]
50
     *
51
     * @param  string                   $key   the key to parse.
52
     * @param  int                      $index [optional] the index to return if the key does not contain one.
53
     * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value
54
     *                                        that does not match the index.
55
     * @return array                    a tuple, the 1st being the key with the nth removed, and the 2nd being the
56
     *                                        index.
57
     */
58 61
    public function parse($key, $index = null)
59
    {
60 61
        if (preg_match('/^([1-9][0-9]*)(?:st|nd|rd|th) (.+)$/', $key, $matches)) {
61 38
            if ($index !== null && $index != $matches[1] - 1) {
62 11
                throw new InvalidArgumentException(
63 11
                    "$index was provided for index param when key '$key' contains an nth value, but they do not match"
64
                );
65
            }
66
67 27
            $index = $matches[1] - 1;
68 27
            $key = $matches[2];
69
        }
70
71 50
        return [$key, $index];
72
    }
73
}
74