| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace League\JsonGuard\Constraints; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use League\JsonGuard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use League\JsonGuard\ValidationError; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Format implements PropertyConstraint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     const KEYWORD = 'format'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     const DATE_TIME_PATTERN = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})([Tt]([0-9]{2}):([0-9]{2}):([0-9]{2})(\\.[0-9]+)?)?(([Zz]|([+-])([0-9]{2}):([0-9]{2})))?/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 8 |  |     public static function validate($value, $parameter, $pointer = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         switch ($parameter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 8 |  |             case 'date-time': | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 4 |  |                 return self::validateRegex( | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 4 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 27 | 4 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 4 |  |                     self::DATE_TIME_PATTERN, | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 4 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 6 |  |             case 'uri': | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 4 |  |                 return self::validateFilter( | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 4 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 4 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 4 |  |                     FILTER_VALIDATE_URL, | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 4 |  |                     null, | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 4 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 4 |  |             case 'email': | 
            
                                                                        
                            
            
                                    
            
            
                | 40 | 4 |  |                 return self::validateFilter( | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 4 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 4 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 4 |  |                     FILTER_VALIDATE_EMAIL, | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 4 |  |                     null, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 4 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 2 |  |             case 'ipv4': | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 2 |  |                 return self::validateFilter( | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 2 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 2 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 2 |  |                     FILTER_VALIDATE_IP, | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 2 |  |                     FILTER_FLAG_IPV4, | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 2 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 2 |  |             case 'ipv6': | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 2 |  |                 return self::validateFilter( | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 2 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 2 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 2 |  |                     FILTER_VALIDATE_IP, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |                     FILTER_FLAG_IPV6, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 2 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 2 |  |             case 'hostname': | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 2 |  |                 return self::validateRegex( | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 2 |  |                     $parameter, | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 2 |  |                     $value, | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 2 |  |                     self::HOST_NAME_PATTERN, | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                     $pointer | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 2 |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param string $pattern | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $pointer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return \League\JsonGuard\ValidationError|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |     private static function validateRegex($format, $value, $pattern, $pointer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |         if (!is_string($value) || preg_match($pattern, $value) === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         return new ValidationError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |             'Value {value} does not match the format {format}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |             self::KEYWORD, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 2 |  |             $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |             $pointer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             ['value' => $value, 'format' => $format] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $format | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param mixed  $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param int    $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param mixed  $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $pointer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return \League\JsonGuard\ValidationError|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 6 |  |     private static function validateFilter($format, $value, $filter, $options, $pointer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 6 |  |         if (!is_string($value) || filter_var($value, $filter, $options) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 6 |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         // This workaround allows otherwise valid protocol relative urls to pass. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // @see https://bugs.php.net/bug.php?id=72301 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         if ($filter === FILTER_VALIDATE_URL && is_string($value) && strpos($value, '//') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |             if (filter_var('http:' . $value, $filter, $options) !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |                 return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |         return new ValidationError( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |             'Value {value} does not match the format {format}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 2 |  |             self::KEYWORD, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 2 |  |             $value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 | 2 |  |             $pointer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |             ['value' => $value, 'format' => $format] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |