3 namespace pq\Mapper\Property
;
7 use pq\Mapper\PropertyInterface
;
8 use pq\Mapper\RefProperty
;
9 use pq\Mapper\RefPropertyInterface
;
10 use UnexpectedValueException
;
12 class Ref
implements RefPropertyInterface
17 * Create a parent row mapping
18 * @param Mapper $mapper
19 * @param string $property
21 function __construct(Mapper
$mapper, $property) {
22 $this->mapper
= $mapper;
23 $this->property
= $property;
27 * Read the parent object
29 * @param object $objectToUpdate
31 function read(Row
$row, $objectToUpdate) {
32 $val = $this->extract($objectToUpdate);
34 $map = $this->mapper
->mapOf($this->refClass
);
35 $ref = $map->refOf($row, $this->refName
, $objects)->current();
36 $this->assign($objectToUpdate, current($objects));
42 * Write the parent row's foreign key
43 * @param object $object
44 * @param Row $rowToUpdate
45 * @throws UnexpectedValueException
47 function write($object, Row
$rowToUpdate) {
48 $map = $this->mapper
->mapOf($this->refClass
);
49 $ref = $this->extract($object);
50 if (!$rel = $map->relOf($this->container
, $this->refName
)) {
51 throw new UnexpectedValueException(
52 sprintf("Unrelated reference from %s to %s with name %s",
53 $this->container
->getGateway()->getName(),
54 $map->getGateway()->getName(),
57 foreach ($rel as $fgn => $col) {
58 foreach ($this->findFieldProperty($col) as $property) {
59 $value = $property->extract($ref);
60 $rowToUpdate->$fgn = $value;
66 * Find the property exposing $col
68 * @return PropertyInterface[]
70 private function findFieldProperty($col) {
71 $map = $this->mapper
->mapOf($this->refClass
);
72 return array_filter($map->getProperties(), function($property) use($col) {
73 return $property->exposes($col);