Esempio n. 1
0
 /**
  * Update this resource
  *
  * @param array  $params
  * @param Schema $schema
  * @return \Guzzle\Http\Message\Response
  * @throws \RuntimeException
  */
 public function update(array $params, Schema $schema = null)
 {
     $schema = $schema ?: $this->getService()->getImageSchema();
     $document = new JsonDocument();
     foreach ($params as $propertyName => $value) {
         // find property object
         if (!($property = $schema->getProperty($propertyName))) {
             // check whether additional properties are found
             if (false === ($property = $schema->validateAdditionalProperty($value))) {
                 throw new \RuntimeException('If a property does not exist in the schema, the `additionalProperties` property must be set');
             }
         }
         // do validation checks
         $property->setName($propertyName);
         $property->setValue($value);
         $property->validate();
         // decide operation type
         if (!$value) {
             $operationType = OperationType::REMOVE;
         } elseif ($this->offsetExists($propertyName)) {
             $operationType = OperationType::REPLACE;
         } else {
             $operationType = $schema->decideOperationType($property);
         }
         // create JSON-patch operation
         $operation = JsonOperation::factory($schema, $property, $operationType);
         // add to JSON document
         $document->addOperation($operation);
     }
     // create request
     $body = $document->toString();
     return $this->getClient()->patch($this->getUrl(), $this->getService()->getPatchHeaders(), $body)->send();
 }
Esempio n. 2
0
 public function test_To_String()
 {
     $document = new Document();
     $operation = new Operation();
     $operation->setPath('/foo');
     $operation->setType(OperationType::REPLACE);
     $operation->setValue('bar');
     $document->addOperation($operation);
     $this->assertEquals('[{"op": "replace", "path": "/foo", "value": "bar"}]', $document->toString());
 }