| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace gamringer\JSONPatch\Operation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use gamringer\JSONPatch\Operation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use gamringer\JSONPointer\Pointer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use gamringer\JSONPointer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use gamringer\JSONPointer\VoidValue; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class Add extends Operation implements Atomic | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     private $previousValue; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct(string $path, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->assertValueAddability($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->value = $value; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function assertValueAddability($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         if (!in_array(gettype($value), ['object', 'array', 'string', 'double', 'integer', 'boolean', 'NULL'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             throw new Exception('Value is not a valid type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function apply(Pointer $target) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->previousValue = $target->insert($this->path, $this->value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } catch (JSONPointer\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             throw new Exception($e->getMessage(), 0, $e); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function revert(Pointer $target) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if ($this->previousValue instanceof VoidValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $target->remove(preg_replace('/\/-$/', '/'.$this->previousValue->getTarget(), $this->path)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $target->set($this->path, $this->previousValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public static function fromDecodedJSON($operationContent): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         self::assertValidOperationContent($operationContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return new self($operationContent->path, $operationContent->value); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     private static function assertValidOperationContent($operationContent) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         if (!property_exists($operationContent, 'path') || !property_exists($operationContent, 'value')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             throw new Operation\Exception('"Add" Operations must contain a "path" and "value" member'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     public function __toString(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return json_encode([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'op' => Operation::OP_ADD, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'path' => $this->path, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'value' => $this->value, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |