/** * 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(); }
public function test_Factory() { $schema = Schema::factory($this->getSchemaData()); $property = Property::factory($this->getPropertyData()); $operation = Operation::factory($schema, $property, OperationType::REPLACE); $this->assertEquals($schema, $operation->getSchema()); $this->assertEquals($property->getPath(), $operation->getPath()); $this->assertEquals($property->getValue(), $operation->getValue()); }