/** * Serializes Object * * @param Map $map the object structure map * @param object $object candidate to serialize * * @return mixed */ public function serialize(Map $map, $object) { $identifier = $map->getIdentifier(); $data = $map->getData($object); if (!isset($data[$identifier]) || !$data[$identifier]) { $id = spl_object_hash($object); $reflection = new \ReflectionProperty($map->getLocalName(), $identifier); $reflection->setAccessible(true); $reflection->setValue($object, $id); } return serialize($object); }
/** * @param Mapper\Map $map * @param $data * * @return bool */ protected function write(Mapper\Map $map, $data) { $reflectionProperty = new \ReflectionProperty($map->getLocalName(), $map->getIdentifier()); $reflectionProperty->setAccessible(true); $identifier = $reflectionProperty->getValue($data); $new = false; if (!$identifier) { $identifier = spl_object_hash($data); $new = true; } $uri = $this->buildUri($map->getRemoteName()) . '/' . $identifier; $result = (bool) file_put_contents($uri, $this->descriptor->serialize($map, $data), false, $this->context ?: null); if ($result && $new) { $reflectionProperty->setValue($data, $identifier); } return $result; }
public function testGetIdentifier() { $identifier = 'id'; $this->assertEquals($identifier, $this->object->getIdentifier()); $this->assertInternalType('string', $this->object->getIdentifier()); }
public function findByIdentifier($identifierQuery) { $identifier = $this->map->getIdentifier(); return current($this->driver->find($this, [$identifier => $identifierQuery])); }