| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace VGirol\JsonApiStructure\Concern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use VGirol\JsonApiStructure\Messages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Validations relating to the arrays | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | trait ValidateArrays | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Check if an array is an array of objects. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param array $json | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     public function isArrayOfObjects($json): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->isValidArgument(1, 'array', $json); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 180 |  |         if (\count($json) == 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 180 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 | 177 |  |         return !$this->arrayIsAssociative($json); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 174 |  |      * Validate that an array is an array of objects. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 174 |  |      * @param array       $json | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 21 |  |      * @param string|null $message     An optional message to explain why the test failed | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param mixed       $code | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 156 |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @throws \VGirol\JsonApiStructure\Exception\ValidationException | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     public function mustBeArrayOfObjects($json, ?string $message = '', $code = 403): void | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         if (!$this->isArrayOfObjects($json)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             $this->throw($message ?: Messages::MUST_BE_ARRAY_OF_OBJECTS, $code); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * Check if an array is not an array of objects. | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * @param array  $json | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 180 |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 180 |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     public function isNotArrayOfObjects($json): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 174 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 3 |  |         $this->isValidArgument(1, 'array', $json); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         if (\count($json) == 0) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 171 |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 171 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 18 |  |         return $this->arrayIsAssociative($json); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 153 |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      * Validate that an array is not an array of objects. | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |      * @param array  $json | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $message     An optional message to explain why the test failed | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |      * @param mixed  $code | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * @throws \VGirol\JsonApiStructure\Exception\ValidationException | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 276 |  |     public function mustNotBeArrayOfObjects($json, string $message = '', $code = 403): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 276 |  |         if (!$this->isNotArrayOfObjects($json)) { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |             $this->throw($message ?: Messages::MUST_NOT_BE_ARRAY_OF_OBJECTS, $code); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * Checks if the given array is an associative array. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param array $arr | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @return boolean | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     private function arrayIsAssociative(array $arr): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |         return (\array_keys($arr) !== \range(0, \count($arr) - 1)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 96 |  |  |  |