X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fpq%2FMapper%2FMap.php;h=bf577be38ca1fbce825fa9eab272a1f68829fde8;hb=5c080bb03b5fee867da94cb3c1108d59529f741a;hp=834ffb1ae9c0338c89daffda42e6e848cc9245ed;hpb=e6da4c7430e5c5b4ba482206e42aeebb4363d2b3;p=m6w6%2Fpq-gateway diff --git a/lib/pq/Mapper/Map.php b/lib/pq/Mapper/Map.php index 834ffb1..bf577be 100644 --- a/lib/pq/Mapper/Map.php +++ b/lib/pq/Mapper/Map.php @@ -5,95 +5,96 @@ namespace pq\Mapper; use pq\Gateway\Row; use pq\Gateway\Rowset; use pq\Gateway\Table; +use pq\Gateway\Table\Reference; use pq\Query\Expr; class Map implements MapInterface { + /** + * @var string + */ private $class; + + /** + * @var Table + */ private $gateway; + + /** + * @var ObjectManager + */ private $objects; - private $properties; + /** + * @var PropertyInterface[] + */ + private $properties = []; + + /** + * Create a new object map definition + * @param string $class + * @param Table $gateway + * @param ...PropertyInterface $properties + */ function __construct($class, Table $gateway, PropertyInterface ...$properties) { $this->class = $class; $this->gateway = $gateway; - $this->properties = $properties; foreach ($properties as $property) { - $property->setContainer($this); + $this->addProperty($property); } - $this->objects = new ObjectCache($this); + $this->objects = new ObjectManager($this); } + /** + * Get the name of the mapped class + * @return string + */ function getClass() { return $this->class; } + /** + * Get the object manager + * @return ObjectManager + */ function getObjects() { return $this->objects; } /** + * Get the underlying table gateway * @return Table */ function getGateway() { return $this->gateway; } + /** + * Get the defined properties to map + * @return PropertyInterface[] + */ function getProperties() { return $this->properties; } + /** + * Add a property to map + * @param PropertyInterface $property + * @return Map + */ function addProperty(PropertyInterface $property) { $property->setContainer($this); $this->properties[] = $property; return $this; } -/* - function idOf(Row $row, $check = false) { - $identity = $row->getIdentity(); - if (is_scalar($identity)) { - return $identity; - } - - if ($check && !isset($identity)) { - return false; - } - - if (is_array($identity)) { - if ($check && array_search(null, $identity, true)) { - return false; - } - /* one level is better than no level * / - asort($identity); - } - return json_encode($identity); - } - - function objectOf(Row $row) { - $id = $this->idOf($row); - - if (isset($this->objects["obj"][$id])) { - $obj = $this->objects["obj"][$id]; - } else { - $obj = new $this->class; - $this->objects["obj"][$id] = $obj; - $this->objects["row"][spl_object_hash($obj)] = $row; - } - return $obj; - } - function rowOf($object) { - $id = spl_object_hash($object); - - if (isset($this->objects["row"][$id])) { - $row = $this->objects["row"][$id]; - } else { - $row = new Row($this->gateway); - $this->objects["row"][$id] = $row; - } - return $row; - } -*/ + /** + * Get all child rows by foreign key + * @param Row $row + * @param string $refName + * @param array $objects + * @return Rowset + */ function allOf(Row $row, $refName, &$objects = null) { /* apply objectOf to populate the object cache */ return $this->gateway->of($row, $refName)->apply(function($row) use(&$objects) { @@ -101,6 +102,13 @@ class Map implements MapInterface }); } + /** + * Get the parent row by foreign key + * @param Row $row + * @param string $refName + * @param array $objects + * @return Rowset + */ function refOf(Row $row, $refName, &$objects = null) { $rid = []; $rel = $row->getTable()->getRelation($this->gateway->getName(), $refName); @@ -122,11 +130,22 @@ class Map implements MapInterface }); } + /** + * Get the table relation reference + * @param MapInterface $map + * @param string $refName + * @return Reference + */ function relOf(MapInterface $map, $refName) { return $map->getGateway()->getRelation( $this->gateway->getName(), $refName); } + /** + * Drain the deferred callback queue + * @param callable[] $deferred + * @param callable $exec + */ private function drain(array $deferred, callable $exec) { while ($deferred) { $cb = array_shift($deferred); @@ -136,6 +155,11 @@ class Map implements MapInterface } } + /** + * Map a row to an object + * @param Row $row + * @return object + */ function map(Row $row) { $deferred = []; $object = $this->objects->asObject($row); @@ -150,6 +174,11 @@ class Map implements MapInterface return $object; } + /** + * Map a rowset to an array of objects + * @param Rowset $rows + * @return object[] + */ function mapAll(Rowset $rows) { $objects = []; foreach ($rows as $row) { @@ -158,6 +187,10 @@ class Map implements MapInterface return $objects; } + /** + * Unmap on object + * @param object $object + */ function unmap($object) { $deferred = []; /* @var $row Row */ @@ -194,5 +227,4 @@ class Map implements MapInterface $row->update(); } } - -} \ No newline at end of file +}