3 namespace pq\Mapper\Property
;
7 use pq\Mapper\RefProperty
;
8 use pq\Mapper\RefPropertyInterface
;
9 use UnexpectedValueException
;
11 class All
implements RefPropertyInterface
16 * Create a child rows mapping
17 * @param Mapper $mapper
18 * @param string $property
20 function __construct(Mapper
$mapper, $property) {
21 $this->mapper
= $mapper;
22 $this->property
= $property;
26 * Read the child objects
28 * @param object $objectToUpdate
30 function read(Row
$row, $objectToUpdate) {
31 $val = $this->extract($objectToUpdate);
33 $map = $this->mapper
->mapOf($this->refClass
);
34 $all = $map->allOf($row, $this->refName
, $objects);
35 $this->assign($objectToUpdate, $objects);
41 * Write the child rows
42 * @param object $object
43 * @param Row $rowToUpdate
44 * @return callable deferred callback
46 function write($object, Row
$rowToUpdate) {
47 if (($refs = $this->extract($object))) {
48 $property = $this->findRefProperty($object);
49 foreach ($refs as $ref) {
50 $property->assign($ref, $object);
52 return function() use($refs) {
53 $map = $this->mapper
->mapOf($this->refClass
);
54 foreach ($refs as $ref) {
62 * Find the referring property that references $object on our foreign key
63 * @param object $object
64 * @return RefPropertyInterface[]
65 * @throws UnexpectedValueException
67 private function findRefProperty($object) {
68 $map = $this->mapper
->mapOf($this->refClass
);
69 $property = array_filter($map->getProperties(), function($property) use($object) {
70 if ($property instanceof RefPropertyInterface
) {
71 return $property->references($object) && $property->on($this->refName
);
75 if (1 != count($property)) {
76 // FIXME: move the decl
77 throw new UnexpectedValueException(
78 sprintf("%s does not reference %s exactly once through %s",
79 $this->refClass
, $this->container
->getClass(), $this->refName
));
81 return current($property);