map test
[m6w6/pq-gateway] / lib / pq / Mapper / Property / All.php
index 0f45174a60bb8c2e3cfdbad82861c0dc2f6bf726..6eb49457645c246efb91c41d63b4a2e170e52b46 100644 (file)
@@ -11,12 +11,22 @@ use UnexpectedValueException;
 class All implements RefPropertyInterface
 {
        use RefProperty;
-       
+
+       /**
+        * Create a child rows mapping
+        * @param Mapper $mapper
+        * @param string $property
+        */
        function __construct(Mapper $mapper, $property) {
                $this->mapper = $mapper;
                $this->property = $property;
        }
-       
+
+       /**
+        * Read the child objects
+        * @param Row $row
+        * @param object $objectToUpdate
+        */
        function read(Row $row, $objectToUpdate) {
                $val = $this->extract($objectToUpdate);
                if (!isset($val)) {
@@ -27,20 +37,33 @@ class All implements RefPropertyInterface
                }
        }
 
+       /**
+        * Write the child rows
+        * @param object $object
+        * @param Row $rowToUpdate
+        * @return callable deferred callback
+        */
        function write($object, Row $rowToUpdate) {
                $property = $this->findRefProperty($object);
                $map = $this->mapper->mapOf($this->refClass);
-               $refs = $this->extract($object);
-               foreach ($refs as $ref) {
-                       $property->assign($ref, $object);
-               }
-               return function() use($map, $refs) {
+               if (($refs = $this->extract($object))) {
                        foreach ($refs as $ref) {
-                               $map->unmap($ref);
+                               $property->assign($ref, $object);
                        }
-               };
+                       return function() use($map, $refs) {
+                               foreach ($refs as $ref) {
+                                       $map->unmap($ref);
+                               }
+                       };
+               }
        }
 
+       /**
+        * Find the referring property that references $object on our foreign key
+        * @param object $object
+        * @return RefPropertyInterface[]
+        * @throws UnexpectedValueException
+        */
        private function findRefProperty($object) {
                $map = $this->mapper->mapOf($this->refClass);
                $property = array_filter($map->getProperties(), function($property) use($object) {
@@ -57,4 +80,4 @@ class All implements RefPropertyInterface
                }
                return current($property);
        }
-}
\ No newline at end of file
+}