cleanups
authorMichael Wallner <mike@php.net>
Sat, 19 Sep 2015 19:56:11 +0000 (21:56 +0200)
committerMichael Wallner <mike@php.net>
Sat, 19 Sep 2015 19:56:11 +0000 (21:56 +0200)
composer.json
composer.lock
lib/pq/Mapper/Map.php
lib/pq/Mapper/MapInterface.php
lib/pq/Mapper/Mapper.php
lib/pq/Mapper/ObjectCache.php [deleted file]
lib/pq/Mapper/ObjectManager.php [new file with mode: 0644]
lib/pq/Mapper/Property/All.php
lib/pq/Mapper/Property/Ref.php
lib/pq/Mapper/Storage.php
lib/pq/Mapper/StorageInterface.php

index 5c87f36cb8ca95ee7e8028e02f756695e7269323..e9363032769ac5f10e5d365445d7059bbc58a96a 100644 (file)
@@ -15,7 +15,7 @@
                "psr-0": {
                        "pq\\Gateway": "lib",
                        "pq\\Query": "lib",
-                       "pq\\Data": "lib"
+                       "pq\\Mapper": "lib"
                }
        },
        "require-dev": {
index 748936954476db44fe581edbc1c0d680271532ad..e9cdceaf98ad6d3efe78595a316e1572bc8fa523 100644 (file)
@@ -1,37 +1,38 @@
 {
     "_readme": [
         "This file locks the dependencies of your project to a known state",
-        "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
         "This file is @generated automatically"
     ],
-    "hash": "813817d14a924e8b4189c1365120cf86",
+    "hash": "206c8b9f89c5af3066d5ecc320c866be",
+    "content-hash": "2106329c981b9ba6675481473db7f850",
     "packages": [],
     "packages-dev": [
         {
             "name": "amphp/amp",
-            "version": "v1.0.0-beta4",
+            "version": "v1.0.4",
             "source": {
                 "type": "git",
                 "url": "https://github.com/amphp/amp.git",
-                "reference": "9fa6010f192f82a81381ae2dd372e1e75107d332"
+                "reference": "880c965fbdad9d6bcc382b7484c00173ee17df13"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/amphp/amp/zipball/9fa6010f192f82a81381ae2dd372e1e75107d332",
-                "reference": "9fa6010f192f82a81381ae2dd372e1e75107d332",
+                "url": "https://api.github.com/repos/amphp/amp/zipball/880c965fbdad9d6bcc382b7484c00173ee17df13",
+                "reference": "880c965fbdad9d6bcc382b7484c00173ee17df13",
                 "shasum": ""
             },
             "require": {
                 "php": ">=5.5"
             },
             "require-dev": {
-                "phpunit/phpunit": "~4.4.0"
+                "fabpot/php-cs-fixer": "~1.9",
+                "phpunit/phpunit": "~4.8"
             },
             "type": "library",
             "extra": {
                 "branch-alias": {
-                    "dev-master": "2.0.0-dev",
-                    "dev-v1.0.x": "1.0.0-dev"
+                    "dev-master": "1.0.x-dev"
                 }
             },
             "autoload": {
                 "async",
                 "concurrency",
                 "event",
-                "future",
                 "non-blocking",
                 "promise"
             ],
-            "time": "2015-05-20 03:12:32"
+            "time": "2015-09-10 21:19:43"
         },
         {
             "name": "react/promise",
-            "version": "v2.2.0",
+            "version": "v2.2.1",
             "source": {
                 "type": "git",
                 "url": "https://github.com/reactphp/promise.git",
-                "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef"
+                "reference": "3b6fca09c7d56321057fa8867c8dbe1abf648627"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/reactphp/promise/zipball/365fcee430dfa4ace1fbc75737ca60ceea7eeeef",
-                "reference": "365fcee430dfa4ace1fbc75737ca60ceea7eeeef",
+                "url": "https://api.github.com/repos/reactphp/promise/zipball/3b6fca09c7d56321057fa8867c8dbe1abf648627",
+                "reference": "3b6fca09c7d56321057fa8867c8dbe1abf648627",
                 "shasum": ""
             },
             "require": {
             "authors": [
                 {
                     "name": "Jan Sorgalla",
-                    "email": "jsorgalla@googlemail.com"
+                    "email": "jsorgalla@gmail.com"
                 }
             ],
             "description": "A lightweight implementation of CommonJS Promises/A for PHP",
-            "time": "2014-12-30 13:32:42"
+            "time": "2015-07-03 13:48:55"
         }
     ],
     "aliases": [],
index 834ffb1ae9c0338c89daffda42e6e848cc9245ed..b63ba46213a95b1501956ef57ea68eff6b9b4611 100644 (file)
@@ -21,7 +21,7 @@ class Map implements MapInterface
                foreach ($properties as $property) {
                        $property->setContainer($this);
                }
-               $this->objects = new ObjectCache($this);
+               $this->objects = new ObjectManager($this);
        }
 
        function getClass() {
@@ -48,52 +48,7 @@ class Map implements MapInterface
                $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;
-       }
-*/
        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) {
index 0c3430ceb8a45d786afce9ded06451851f3a74db..0e0db3a3e97e9c08e5e0c3326b388a6898fc7b3c 100644 (file)
@@ -28,6 +28,29 @@ interface MapInterface
         */
        function addProperty(PropertyInterface $property);
 
+       /**
+        * @param Row $row
+        * @param string $refName
+        * @param array $objects
+        * @return Rowset
+        */
+       function allOf(Row $row, $refName, &$objects = null);
+
+       /**
+        * @param Row $row
+        * @param string $refName
+        * @param array $objects
+        * @return Rowset
+        */
+       function refOf(Row $row, $refName, &$objects = null);
+
+       /**
+        * @param MapInterface $map origin
+        * @param string $refName relations reference name
+        * @return array relation reference
+        */
+       function relOf(MapInterface $map, $refName);
+
        /**
         * @param Row $row
         * @return object
@@ -35,7 +58,7 @@ interface MapInterface
        function map(Row $row);
 
        /**
-        * @param \pq\Mapper\Rowset $rows
+        * @param Rowset $rows
         * @return array
         */
        function mapAll(Rowset $rows);
@@ -45,4 +68,4 @@ interface MapInterface
         * @return Row
         */
        function unmap($object);
-}
\ No newline at end of file
+}
index 725a50c9ce28eb605bb4f9ef145832bda196e3bc..1c70f726fb81b0e477a8b683fb380a26cb726547 100644 (file)
@@ -53,7 +53,7 @@ class Mapper
         * @return \pq\Mapper\Storage
         */
        function createStorage($class) {
-               return new Storage($this, $class);
+               return new Storage($this->mapOf($class));
        }
 
        /**
diff --git a/lib/pq/Mapper/ObjectCache.php b/lib/pq/Mapper/ObjectCache.php
deleted file mode 100644 (file)
index 777dd51..0000000
+++ /dev/null
@@ -1,133 +0,0 @@
-<?php
-
-namespace pq\Mapper;
-
-use OutOfBoundsException;
-use pq\Exception\BadMethodCallException;
-use pq\Gateway\Row;
-
-class ObjectCache
-{
-       private $map;
-       private $obj = [];
-       private $row = [];
-
-       function __construct(MapInterface $map) {
-               $this->map = $map;
-       }
-
-       function reset() {
-               $this->obj = [];
-               $this->row = [];
-       }
-
-       function rowId(Row $row, $check = false) {
-               try {
-                       $identity = $row->getIdentity();
-               } catch (OutOfBoundsException $e) {
-                       return false;
-               }
-               return $this->serializeRowId($identity, $check);
-       }
-
-       function objectId($object) {
-               return spl_object_hash($object);
-       }
-
-       function extractRowId($object) {
-               $id = [];
-               foreach ($this->map->getGateway()->getIdentity() as $col) {
-                       foreach ($this->map->getProperties() as $property) {
-                               if ($property->exposes($col)) {
-                                       $id[$col] = $property->extract($object);
-                               }
-                       }
-               }
-               return $this->serializeRowId($id, true);
-       }
-
-       function serializeRowId($identity, $check = false) {
-               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 hasObject($row_id) {
-               return isset($this->obj[$row_id]);
-       }
-
-       function createObject(Row $row) {
-               $rid = $this->rowId($row);
-               $cls = $this->map->getClass();
-               $obj = new $cls;
-               $oid = $this->objectId($obj);
-               $this->obj[$rid] = $obj;
-               $this->row[$oid] = $row;
-               return $obj;
-       }
-
-       function resetObject(Row $row) {
-               unset($this->obj[$this->rowId($row)]);
-       }
-
-       function getObject(Row $row) {
-               $id = $this->rowId($row);
-               return $this->getObjectById($id);
-       }
-
-       function getObjectById($row_id) {
-               if (!$this->hasObject($row_id)) {
-                       throw new BadMethodCallException("Object of row with id $row_id does not exist");
-               }
-               return $this->obj[$row_id];
-       }
-
-       function asObject(Row $row){
-               return $this->hasObject($this->rowId($row))
-                       ? $this->getObject($row)
-                       : $this->createObject($row);
-       }
-
-       function hasRow($obj_id) {
-               return isset($this->row[$obj_id]);
-       }
-
-       function createRow($object) {
-               $oid = $this->objectId($object);
-               $row = new Row($this->map->getGateway());
-               $this->row[$oid] = $row;
-               return $row;
-       }
-
-       function resetRow($object) {
-               unset($this->row [$this->objectId($object)]);
-       }
-       
-       function getRow($object) {
-               $id = $this->objectId($object);
-
-               if (!$this->hasRow($id)) {
-                       throw new BadMethodCallException("Row for object with id $id does not exist");
-               }
-               return $this->row[$id];
-       }
-
-       function asRow($object) {
-               return $this->hasRow($this->objectId($object))
-                       ? $this->getRow($object)
-                       : $this->createRow($object);
-       }
-}
\ No newline at end of file
diff --git a/lib/pq/Mapper/ObjectManager.php b/lib/pq/Mapper/ObjectManager.php
new file mode 100644 (file)
index 0000000..557d494
--- /dev/null
@@ -0,0 +1,133 @@
+<?php
+
+namespace pq\Mapper;
+
+use OutOfBoundsException;
+use pq\Exception\BadMethodCallException;
+use pq\Gateway\Row;
+
+class ObjectManager
+{
+       private $map;
+       private $obj = [];
+       private $row = [];
+
+       function __construct(MapInterface $map) {
+               $this->map = $map;
+       }
+
+       function reset() {
+               $this->obj = [];
+               $this->row = [];
+       }
+
+       function rowId(Row $row, $check = false) {
+               try {
+                       $identity = $row->getIdentity();
+               } catch (OutOfBoundsException $e) {
+                       return false;
+               }
+               return $this->serializeRowId($identity, $check);
+       }
+
+       function objectId($object) {
+               return spl_object_hash($object);
+       }
+
+       function extractRowId($object) {
+               $id = [];
+               foreach ($this->map->getGateway()->getIdentity() as $col) {
+                       foreach ($this->map->getProperties() as $property) {
+                               if ($property->exposes($col)) {
+                                       $id[$col] = $property->extract($object);
+                               }
+                       }
+               }
+               return $this->serializeRowId($id, true);
+       }
+
+       function serializeRowId($identity, $check = false) {
+               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 hasObject($row_id) {
+               return isset($this->obj[$row_id]);
+       }
+
+       function createObject(Row $row) {
+               $rid = $this->rowId($row);
+               $cls = $this->map->getClass();
+               $obj = new $cls;
+               $oid = $this->objectId($obj);
+               $this->obj[$rid] = $obj;
+               $this->row[$oid] = $row;
+               return $obj;
+       }
+
+       function resetObject(Row $row) {
+               unset($this->obj[$this->rowId($row)]);
+       }
+
+       function getObject(Row $row) {
+               $id = $this->rowId($row);
+               return $this->getObjectById($id);
+       }
+
+       function getObjectById($row_id) {
+               if (!$this->hasObject($row_id)) {
+                       throw new BadMethodCallException("Object of row with id $row_id does not exist");
+               }
+               return $this->obj[$row_id];
+       }
+
+       function asObject(Row $row){
+               return $this->hasObject($this->rowId($row))
+                       ? $this->getObject($row)
+                       : $this->createObject($row);
+       }
+
+       function hasRow($obj_id) {
+               return isset($this->row[$obj_id]);
+       }
+
+       function createRow($object) {
+               $oid = $this->objectId($object);
+               $row = new Row($this->map->getGateway());
+               $this->row[$oid] = $row;
+               return $row;
+       }
+
+       function resetRow($object) {
+               unset($this->row [$this->objectId($object)]);
+       }
+       
+       function getRow($object) {
+               $id = $this->objectId($object);
+
+               if (!$this->hasRow($id)) {
+                       throw new BadMethodCallException("Row for object with id $id does not exist");
+               }
+               return $this->row[$id];
+       }
+
+       function asRow($object) {
+               return $this->hasRow($this->objectId($object))
+                       ? $this->getRow($object)
+                       : $this->createRow($object);
+       }
+}
\ No newline at end of file
index b53efdd44979c5726979513faa61a23dcbfe4415..0f45174a60bb8c2e3cfdbad82861c0dc2f6bf726 100644 (file)
@@ -39,21 +39,6 @@ class All implements RefPropertyInterface
                                $map->unmap($ref);
                        }
                };
-               
-               if (!$this->container->getObjects()->rowId($rowToUpdate, true)) {
-                       return [$this, "write"];
-               } else {
-                       /* $object = User */
-                       /* $refs = array(Email) */
-                       /* $property = Property\Ref(Email::$user)->to(User)->by("email_user") */
-                       /* now update array(Email) with id of User, i.e. $ref->user_id = $object->id */
-                       $map = $this->mapper->mapOf($this->refClass);
-                       $refs = $this->extract($object);
-                       foreach ($refs as $ref) {
-                               $property->assign($ref, $object);
-                               $map->unmap($ref);
-                       }
-               }
        }
 
        private function findRefProperty($object) {
@@ -72,24 +57,4 @@ class All implements RefPropertyInterface
                }
                return current($property);
        }
-
-       function read2(RowGateway $row) {
-               #echo __METHOD__." ".$this;
-               $ref = $this->getRefMap()->ref($row, $this->refName);
-               $value = $this->mapper->map($ref, $this->refClass);
-               return [$this->property => $value];
-       }
-       
-       function write2($object) {
-               #echo __METHOD__." ".$this;
-               $value = $this->extract($object);
-               foreach ($value as $ref) {
-                       $this->mapper->queue(function() use(&$object, &$ref) {
-                               $map = $this->getRefMap()->getRefMapping($this->refName);
-                               $map->assign($ref, $object);
-                               $this->mapper->unmap($ref, $this->getRefMap());
-                       });
-               }
-               return [];
-       }
 }
\ No newline at end of file
index 851df41e8aaefe3580d1ed0731d07c10f4760aa0..84f39f41e7cbd660969983967158b5aa862e1f59 100644 (file)
@@ -6,6 +6,7 @@ use pq\Gateway\Row;
 use pq\Mapper\Mapper;
 use pq\Mapper\RefProperty;
 use pq\Mapper\RefPropertyInterface;
+use UnexpectedValueException;
 
 class Ref implements RefPropertyInterface
 {
@@ -29,7 +30,13 @@ class Ref implements RefPropertyInterface
        function write($object, Row $rowToUpdate) {
                $map = $this->mapper->mapOf($this->refClass);
                $ref = $this->extract($object);
-               $rel = $map->relOf($this->container, $this->refName);
+               if (!$rel = $map->relOf($this->container, $this->refName)) {
+                       throw new UnexpectedValueException(
+                               sprintf("Unrelated reference from %s to %s with name %s",
+                                       $this->container->getGateway()->getName(),
+                                       $map->getGateway()->getName(),
+                                       $this->refName));
+               }
                foreach ($rel as $fgn => $col) {
                        foreach ($this->findFieldProperty($col) as $property) {
                                $value = $property->extract($ref);
@@ -44,32 +51,4 @@ class Ref implements RefPropertyInterface
                        return $property->exposes($col);
                });
        }
-
-
-       function read2(RowGateway $row) {
-               #echo __METHOD__." ".$this;
-               $map = $this->getRefMap();
-               $rel = $this->container->getGateway()->getRelation(
-                       $map->getGateway()->getName(), $this->refName);
-               $key = array_combine($rel->referencedColumns, array_map(function($c) use($row) {
-                       return $row->$c->get();
-               }, $rel->foreignColumns));
-               if (($obj = $this->mapper->objectOfRowId($this->refClass, $key))) {
-                       yield $this->property => $obj;
-               } else foreach ($map->getGateway()->by($row, $this->refName) as $row) {
-                       yield $this->property => $this->mapper->objectOf($this->refClass, $row);
-               }
-       }
-       
-       function write2($object) {
-               #echo __METHOD__." ".$this;
-               $map = $this->getRefMap();
-               $rel = $this->container->getGateway()->getRelation(
-                       $map->getGateway()->getName(), $this->refName);
-               $ref = $this->extract($object);
-               foreach ($rel as $fgn => $col) {
-                       $fld = $map->getFieldMapping($col);
-                       yield $fgn => $fld->extract($ref);
-               }
-       }
 } 
\ No newline at end of file
index a27ea685dd96dc9864623685e8f130923fb72826..8e0fcb4df7b20f8b8347e8e241e3c9626f180820 100644 (file)
@@ -4,53 +4,37 @@ namespace pq\Mapper;
 
 class Storage implements StorageInterface
 {
-       /**
-        * @var string
-        */
-       private $class;
-
-       /**
-        * @var \pq\Mapper\Mapper
-        */
-       private $mapper;
-
        /**
         *
         * @var pq\Mapper\MapInterface
         */
-       private $mapping;
+       private $map;
 
        /**
         * @var \pq\Gateway\Table
         */
        private $gateway;
        
-       function __construct(Mapper $mapper, $class) {
-               $this->class = $class;
-               $this->mapper = $mapper;
-               $this->mapping = $mapper->mapOf($class);
-               $this->gateway = $this->mapping->getGateway();
-       }
-       
-       function getMapper() {
-               return $this->mapper;
+       function __construct(MapInterface $map) {
+               $this->map = $map;
+               $this->gateway = $map->getGateway();
        }
        
        function find($where = [], $order = null, $limit = null, $offset = null) {
                /* @var pq\Gateway\Rowset $rowset */
                $rowset = $this->gateway->find($where, $order, $limit, $offset);
-               return $this->mapping->mapAll($rowset);
+               return $this->map->mapAll($rowset);
        }
        
        function delete($object) {
-               $cache = $this->mapping->getObjects();
+               $cache = $this->map->getObjects();
                $row = $cache->asRow($object)->delete();
                $cache->resetObject($row);
                $cache->resetRow($object);
        }
        
-       function persist($object) {
-               $this->mapping->unmap($object);
+       function save($object) {
+               $this->map->unmap($object);
        }
        
 }
\ No newline at end of file
index db16df52f7504f662e8b7e8117c7356d68ab2410..188318aa64d1f9816e309f2795e9135a23bb6f31 100644 (file)
@@ -4,12 +4,7 @@ namespace pq\Mapper;
 
 interface StorageInterface
 {
-       /**
-        * @return pq\Mapper\Mapper
-        */
-       function getMapper();
-       
        function find($where, $order = null, $limit = null, $offset = null);
        function delete($object);
-       function persist($object);
-}
\ No newline at end of file
+       function save($object);
+}