/** * {@inheritDoc} */ public function save($element) { $class = $this->apiResource->getClass(); if (!$element instanceof $class) { throw new \InvalidArgumentException(sprintf('Resource %s só aceita objetos da classe %s', $this->apiResource->getCurrentPath(), $class)); } $data = $this->rest->formatOutput($element, $this->apiResource->getFormat()); if ($element instanceof ElementInterface) { // mais rápido e fácil $id = $element->getId(); } else { // mapeia as propriedades e retorna uma classe com os metadados $metadata = $this->elementMetadataFactory->getMetadata($class); $id = $metadata->getIdentifier($element); } $response = $this->execute($id ? 'PUT' : 'POST', $this->apiResource->getCurrentPath($id), $data); if (!$id) { return $this->rest->createObject($response, $this->apiResource->getClass(), $this->apiResource->getFormat()); } return $element; }
public function testCreateObject() { $object = $this->rest->createObject('{"prop":"foo"}', 'Broda\\Tests\\Component\\Rest\\Fixtures\\SerializableObject', 'json'); $this->assertInstanceOf('Broda\\Tests\\Component\\Rest\\Fixtures\\SerializableObject', $object); $this->assertEquals('foo', $object->prop); }