Skip to content

Commit

Permalink
Added support to read and remove current serialized object
Browse files Browse the repository at this point in the history
The method addData($key, $value) allows to add new keys to serialised objects but no further manipulation to the serialised object is possible is post serialize events. This commit added the 2 methods: removeData($key) and getData(). This will allow manipulation of the current serialised object in post serialize event listeners. This is in response to schmittjoh#74, as I'm facing a similar need.
  • Loading branch information
ahsanity committed Mar 3, 2015
1 parent e619ca4 commit 090889d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
* @param string $key
* @param scalar|array $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
* @throws Exception\InvalidArgumentException
*/
public function addData($key, $value)
{
Expand All @@ -169,6 +170,31 @@ public function addData($key, $value)
$this->data[$key] = $value;
}

/**
* Allows you to remove data from the current object/root element.
*
* @param string $key
* @throws Exception\InvalidArgumentException
*/
public function removeData($key)
{
if (!isset($this->data[$key])) {
throw new InvalidArgumentException(sprintf('There is no data for "%s".', $key));
}

unset($this->data[$key]);
}

/**
* Returns the current object
*
* @return mixed
*/
public function getData()
{
return $this->data;
}

public function getRoot()
{
return $this->root;
Expand Down

0 comments on commit 090889d

Please sign in to comment.