| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Modera\UpgradeBundle\Json; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author    Sergei Vizel <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright 2014 Modera Foundation | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | class JsonFile | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     private $path; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->path = $path; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |      * Reads json file. | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * @throws \RuntimeException | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function read() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $json = @file_get_contents($this->path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (false === $json) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 'Could not read '.$this->path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $arr = json_decode($json, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $error = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         switch (json_last_error()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             case JSON_ERROR_NONE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             case JSON_ERROR_DEPTH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $error = 'Maximum stack depth exceeded.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             case JSON_ERROR_STATE_MISMATCH: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 $error = 'Underflow or the modes mismatch.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             case JSON_ERROR_CTRL_CHAR: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $error = 'Unexpected control character found.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             case JSON_ERROR_SYNTAX: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $error = 'Syntax error, malformed JSON.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             case JSON_ERROR_UTF8: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 $error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $error = 'Unknown error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($error) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $error.' Path: '.$this->path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $arr; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * Writes json file. | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * @param array $hash    writes hash into json file | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param int   $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @throws \RuntimeException | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function write(array $hash, $options = 448) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         if (!file_exists(dirname($this->path))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             throw new \RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 'Could not write '.$this->path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $encode = json_encode($hash, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         file_put_contents($this->path, $encode.($options & JSON_PRETTY_PRINT ? "\n" : '')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  | 
            
                        
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: