storage enhancements mapper
authorMichael Wallner <mike@php.net>
Thu, 24 Sep 2015 10:02:42 +0000 (12:02 +0200)
committerMichael Wallner <mike@php.net>
Thu, 24 Sep 2015 10:02:42 +0000 (12:02 +0200)
lib/pq/Mapper/Mapper.php
lib/pq/Mapper/Storage.php
lib/pq/Mapper/StorageInterface.php
tests/lib/pq/Mapper/StorageTest.php

index 4a8f921e5888c426c9a74f824d6839b19d5f82e6..877bcd7c84bcf8a4bd6747e394df60121817b15c 100644 (file)
@@ -73,7 +73,7 @@ class Mapper
         * @return Storage
         */
        function createStorage($class) {
-               return new Storage($this->mapOf($class));
+               return new Storage($this, $class);
        }
 
        /**
index 4882eda881fb512232d6eb85b4455a92ad6e4afc..042b6947874b0addc923f1f39684d14d1139f8a6 100644 (file)
@@ -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
index eb75f633b63fdfe9d2ce375a71ec60d719c4b5b2..540d91f5dddfba58c43625dc0de4cfbd9506b37f 100644 (file)
@@ -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
index c57716269528a0741a5c9593615ef0303f0f5191..0889b13eb16c951e7cf3b3f4ecdbbf7df97e119b 100644 (file)
@@ -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"));
+       }
 }