From 861bb294d28aba35e011d9d35aad750d93c7dbd7 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 24 Sep 2015 12:02:42 +0200 Subject: [PATCH] storage enhancements --- lib/pq/Mapper/Mapper.php | 2 +- lib/pq/Mapper/Storage.php | 64 +++++++++++++++++++++-------- lib/pq/Mapper/StorageInterface.php | 16 ++++++++ tests/lib/pq/Mapper/StorageTest.php | 14 +++++++ 4 files changed, 77 insertions(+), 19 deletions(-) diff --git a/lib/pq/Mapper/Mapper.php b/lib/pq/Mapper/Mapper.php index 4a8f921..877bcd7 100644 --- a/lib/pq/Mapper/Mapper.php +++ b/lib/pq/Mapper/Mapper.php @@ -73,7 +73,7 @@ class Mapper * @return Storage */ function createStorage($class) { - return new Storage($this->mapOf($class)); + return new Storage($this, $class); } /** diff --git a/lib/pq/Mapper/Storage.php b/lib/pq/Mapper/Storage.php index 4882eda..042b694 100644 --- a/lib/pq/Mapper/Storage.php +++ b/lib/pq/Mapper/Storage.php @@ -13,27 +13,29 @@ class Storage implements StorageInterface * The mapping of this storage * @var MapInterface */ - var $map; + private $map; /** - * The underlying table gateway - * @var Table + * The mapper + * @var Mapper */ - private $gateway; + private $mapper; /** - * Buffered transaction - * @var Transaction + * The underlying table gateway + * @var Table */ - private $xaction; + private $gateway; /** * Create a storage for $map - * @param MapInterface $map + * @param Mapper $mapper + * @param string $class */ - function __construct(MapInterface $map) { - $this->map = $map; - $this->gateway = $map->getGateway(); + function __construct(Mapper $mapper, $class) { + $this->mapper = $mapper; + $this->map = $mapper->mapOf($class); + $this->gateway = $this->map->getGateway(); } /** @@ -44,18 +46,20 @@ class Storage implements StorageInterface function get($pk) { $id = $this->gateway->getIdentity(); if (count($id) == 1 && is_scalar($pk)) { - $pk = [current($id->getColumns()) => $pk]; - } elseif (!is_array($pk) || count($pk) !== count($id)) { + $vals = [$pk]; + } elseif (is_array($pk) && count($pk) === count($id)) { + $vals = $pk; + } else { throw InvalidArgumentException( "Insufficient identity provided; not all fields of %s are provided in %s", json_encode($id->getColumns()), json_encode($pk)); } - $where = []; - foreach ($pk as $k => $v) { - $where["$k="] = $v; - } - $rowset = $this->gateway->find($where); + $keys = array_map(function($v) { + return "$v="; + }, $id->getColumns()); + + $rowset = $this->gateway->find(array_combine($keys, $vals)); return $this->map->map($rowset->current()); } @@ -74,6 +78,30 @@ class Storage implements StorageInterface return $this->map->mapAll($rowset); } + /** + * Find parent + * @param object $object + * @param string $refName + * @return object + */ + function by($object, $refName) { + $row = $this->mapper->mapOf($object)->getObjects()->getRow($object); + $this->map->refOf($row, $refName, $objects); + return current($objects); + } + + /** + * Find childs + * @param object $object + * @param string $refName + * @return array + */ + function of($object, $refName) { + $row = $this->mapper->mapOf($object)->getObjects()->getRow($object); + $this->map->allOf($row, $refName, $objects); + return $objects; + } + /** * Delete * @param object $object diff --git a/lib/pq/Mapper/StorageInterface.php b/lib/pq/Mapper/StorageInterface.php index eb75f63..540d91f 100644 --- a/lib/pq/Mapper/StorageInterface.php +++ b/lib/pq/Mapper/StorageInterface.php @@ -21,6 +21,22 @@ interface StorageInterface */ function find($where = [], $order = null, $limit = null, $offset = null); + /** + * Find parent + * @param object $object + * @param string $refName + * @return object + */ + function by($object, $refName); + + /** + * Find child rows + * @param object $object + * @param string $refName + * @return array + */ + function of($object, $refName); + /** * Delete * @param object $object diff --git a/tests/lib/pq/Mapper/StorageTest.php b/tests/lib/pq/Mapper/StorageTest.php index c577162..0889b13 100644 --- a/tests/lib/pq/Mapper/StorageTest.php +++ b/tests/lib/pq/Mapper/StorageTest.php @@ -80,4 +80,18 @@ class StorageTest extends PHPUnit_Framework_TestCase $this->storage->discard(); $this->assertEquals("the day before", $this->storage->get(1)->data); } + + function testOf() { + $obj = $this->storage->get(1); + $ref = new Storage($this->mapper, RefTestModel::class); + $this->assertSame($obj->ref1, $ref->of($obj, "test")); + $this->assertSame($obj->ref2, $ref->of($obj, "another_test")); + } + + function testBy() { + $ref = new Storage($this->mapper, RefTestModel::class); + $obj = $ref->get([2,2]); + $this->assertSame($obj->one, $this->storage->by($obj, "test")); + $this->assertSame($obj->two, $this->storage->by($obj, "another_test")); + } } -- 2.30.2