| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dgame\Ensurance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dgame\Type\Type; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dgame\Type\TypeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class ArrayEnsurance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package Dgame\Ensurance | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | final class ArrayEnsurance implements EnsuranceInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     use EnsuranceTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * ArrayEnsurance constructor. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @param EnsuranceInterface $ensurance | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct(EnsuranceInterface $ensurance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 9 |  |         $this->transferEnsurance($ensurance); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->value = $ensurance->else([]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 | 9 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 9 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @param callable $callback | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private function filterBy(callable $callback): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return array_filter(array_map($callback, $this->value)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 35 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 | 1 |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param callable $callback | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function all(callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $this->ensure(count($this->filterBy($callback)) === count($this->value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param callable $callback | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function any(callable $callback): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->ensure(!empty($this->filterBy($callback))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 | 1 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |      * @param string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return ArrayEnsurance | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function allTypeOf(string $type): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $type = Type::import($type); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this->all(function ($value) use ($type): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |             return TypeFactory::expression($value)->isSame($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         }); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @return ArrayEnsurance | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function anyTypeOf(string $type): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $type = Type::import($type); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         return $this->any(function ($value) use ($type): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return TypeFactory::expression($value)->isSame($type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         }); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param $key | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |     public function hasKey($key): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         $this->ensure(array_key_exists($key, $this->value)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |              ->orThrow('Key "%s" is not contained in %s', $key, $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      * @param $value | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |     public function hasValue($value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         $this->ensure(in_array($value, $this->value)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 1 |  |              ->orThrow('Value "%s" is not contained in ', $value, $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 1 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |      * @param int $length | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 1 |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |     public function hasLengthOf(int $length): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 1 |  |         $this->ensure($count === $length)->orThrow('array has not the length %d (%d)', $length, $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 1 |  |      * @param int $length | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 1 |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 134 | 1 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function isShorterThan(int $length): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         $this->ensure($count < $length)->orThrow('array is not shorter than %d (%d)', $length, $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |      * @param int $length | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     public function isShorterOrEqualsTo(int $length): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $this->ensure($count <= $length)->orThrow('array is not shorter or equal to %d (%d)', $length, $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |      * @param int $length | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     public function isLongerThan(int $length): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         $this->ensure($count > $length)->orThrow('array is longer than %d (%d)', $length, $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * @param int $length | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     public function isLongerOrEqualTo(int $length): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         $this->ensure($count >= $length)->orThrow('array is not longer or equal to %d (%d)', $length, $count); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     public function isAssociative(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $this->ensure(array_keys($this->value) !== range(0, $count - 1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |              ->orThrow('array %s is not associative', $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 192 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 194 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 195 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 196 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     public function isNotAssociative(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $count = count($this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         $this->ensure(array_keys($this->value) === range(0, $count - 1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |              ->orThrow('array %s is associative', $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 206 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 207 |  |  |      * @return ArrayEnsurance | 
            
                                                                        
                            
            
                                    
            
            
                | 208 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     public function isCallable(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $this->ensure(is_callable($this->value))->orThrow('Value is not a callable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 214 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 215 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 216 |  |  |  | 
            
                        
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.