/**
  * Hydrate $object with the provided $data.
  *
  * @param  array $data
  * @param  EntityInterface $entity
  * @return object
  */
 public function hydrate(\SimpleXMLElement $data, EntityInterface $entity)
 {
     foreach ($this->metadata->getProperties() as $property) {
         $extractor = $property->getExtractor();
         /* @var $extracted \SimpleXMLElement */
         $extracted = $extractor($data);
         // Get SimpleXMLElement from response XML tree.
         if ($property->isOneToMany()) {
             $splObjectStorage = $entity->{$property->getGetter()}();
             $splObjectStorage->removeAll($splObjectStorage);
             $entityMetadata = $property->getTargetEntity();
             $hydrator = new EntityHydrator($entityMetadata);
             $className = $entityMetadata->getName();
             if (!$extracted) {
                 throw new Exception\RuntimeException("Something gone wrong,\n                                                         data could not be parsed " . $data->asXML());
             }
             foreach ($extracted as $xmlElement) {
                 $entity->{$property->getHandler()}($hydrator->hydrate($xmlElement, new $className()));
             }
             continue;
         } else {
             if (empty($extracted)) {
                 continue;
             }
             $entity->{$property->getHandler()}((string) $extracted);
         }
     }
     return $entity;
 }
 /**
  * @param EntityInterface $entity
  * @return mixed | object
  */
 public function save(EntityInterface $entity)
 {
     $httpUri = new Http($this->classMetadata->getServicePath());
     $hydrator = new EntityHydrator($this->classMetadata);
     $element = $this->mapper->save($httpUri, $hydrator->extract($entity));
     return $hydrator->hydrate($element, $entity);
 }