| 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|null                 $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 $index is less than -1. Note: It should really be zero or higher, but this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      *                                        method does not assert that. The error is bubbling up from getOrdinal() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return string                   the key with the index, or just the key if index is null. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function build($key, $index) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ($index === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return $key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $nth = $index + 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         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 |  |  |      * @throws InvalidArgumentException if $nth is not a positive number. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return string                   the ordinal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getOrdinal($nth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($nth < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new InvalidArgumentException('$nth must be a positive number'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $nth > 9 && $nth < 20 ? self::ORDINAL_TH : self::$ordinals[substr($nth, -1)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * Parses a key into the separate key and index value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @example parseKey("Item"): ["Item", null] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |      * @throws InvalidArgumentException if the key syntax is invalid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return array                    a tuple, the 1st being the key with the nth removed, and the 2nd being the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *                                      index that the nth translates to, or null if no nth was specified. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function parse($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (!preg_match("/^(([1-9][0-9]*)(?:st|nd|rd|th) )?([^']+)$/", $key, $matches)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             throw new InvalidArgumentException('Key syntax is invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         // @todo use null coalescing operator when upgrading to PHP 7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $index = isset($matches[2]) && $matches[2] !== '' ? $matches[2] - 1 : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $key = $matches[3]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return [$key, $index]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Resolves an index and parsed nth value to an index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Ensures that if both an index and parsed nth value are provided, that they are equivalent. If only one is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * provided, then the appropriate index will be returned. e.g. if an index is provided, it is returned as-is, as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * it is already an index. If an nth is provided, it will be returned decremented by 1. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param  int|null                 $index the index to process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param  int|null                 $nth   the nth to process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @throws InvalidArgumentException if both an $index and $key are provided, but the $key contains an nth value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      *                                        that does not match the index. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws InvalidArgumentException if nth is not null and is less than 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return int                      the resolved index. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     protected function resolveIndex($index, $nth) | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |         // If we don't have an nth, there's nothing to process. We'll just return the $index, even if it's null. | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |         if ($nth === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             return $index; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         $decrementedNth = $nth - 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         // If both index and nth are provided, but they aren't equivalent, we need to error out. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if ($index !== null && $index !== $decrementedNth) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             throw new InvalidArgumentException("index $index was provided with nth $nth, but they are not equivalent"); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         if ($decrementedNth < 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |             throw new InvalidArgumentException('nth must be equal to or larger than 1'); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         return $decrementedNth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |