834ffb1ae9c0338c89daffda42e6e848cc9245ed
10 class Map
implements MapInterface
17 function __construct($class, Table
$gateway, PropertyInterface
...$properties) {
18 $this->class = $class;
19 $this->gateway
= $gateway;
20 $this->properties
= $properties;
21 foreach ($properties as $property) {
22 $property->setContainer($this);
24 $this->objects
= new ObjectCache($this);
31 function getObjects() {
32 return $this->objects
;
38 function getGateway() {
39 return $this->gateway
;
42 function getProperties() {
43 return $this->properties
;
46 function addProperty(PropertyInterface
$property) {
47 $property->setContainer($this);
48 $this->properties
[] = $property;
52 function idOf(Row $row, $check = false) {
53 $identity = $row->getIdentity();
54 if (is_scalar($identity)) {
58 if ($check && !isset($identity)) {
62 if (is_array($identity)) {
63 if ($check && array_search(null, $identity, true)) {
66 /* one level is better than no level * /
69 return json_encode($identity);
72 function objectOf(Row $row) {
73 $id = $this->idOf($row);
75 if (isset($this->objects["obj"][$id])) {
76 $obj = $this->objects["obj"][$id];
78 $obj = new $this->class;
79 $this->objects["obj"][$id] = $obj;
80 $this->objects["row"][spl_object_hash($obj)] = $row;
85 function rowOf($object) {
86 $id = spl_object_hash($object);
88 if (isset($this->objects["row"][$id])) {
89 $row = $this->objects["row"][$id];
91 $row = new Row($this->gateway);
92 $this->objects["row"][$id] = $row;
97 function allOf(Row
$row, $refName, &$objects = null) {
98 /* apply objectOf to populate the object cache */
99 return $this->gateway
->of($row, $refName)->apply(function($row) use(&$objects) {
100 $objects[] = $this->objects
->asObject($row);
104 function refOf(Row
$row, $refName, &$objects = null) {
106 $rel = $row->getTable()->getRelation($this->gateway
->getName(), $refName);
107 // FIXME: check if foreign key points to primary key
108 foreach ($rel as $fgn => $col) {
109 $rid[$col] = $row->$fgn->get();
111 $rid = $this->objects
->serializeRowId($rid);
112 if ($this->objects
->hasObject($rid)) {
113 $object = $this->objects
->getObjectById($rid);
114 $row = $this->objects
->getRow($object);
115 $objects[] = $object;
116 $rowset = new Rowset($this->gateway
);
117 return $rowset->append($row);
119 /* apply objectOf to populate the object cache */
120 return $this->gateway
->by($row, $refName)->apply(function($row) use(&$objects) {
121 $objects[] = $this->objects
->asObject($row);
125 function relOf(MapInterface
$map, $refName) {
126 return $map->getGateway()->getRelation(
127 $this->gateway
->getName(), $refName);
130 private function drain(array $deferred, callable
$exec) {
132 $cb = array_shift($deferred);
133 if (($cb = $exec($cb))) {
139 function map(Row
$row) {
141 $object = $this->objects
->asObject($row);
142 foreach ($this->properties
as $property) {
143 if (($cb = $property->read($row, $object))) {
147 $this->drain($deferred, function(callable
$cb) use($row, $object) {
148 return $cb($row, $object);
153 function mapAll(Rowset
$rows) {
155 foreach ($rows as $row) {
156 $objects[] = $this->map($row);
161 function unmap($object) {
164 $row = $this->objects
->asRow($object);
165 $upd = $this->objects
->rowId($row, true);
166 foreach ($this->properties
as $property) {
167 if (($cb = $property->write($object, $row))) {
171 foreach ($this->gateway
->getIdentity() as $col) {
172 if (null === $row->$col->get()
173 ||
($row->$col->isExpr() && $row->$col->get()->isNull()))
175 $row->$col = new Expr("DEFAULT");
178 if ($row->isDirty()) {
185 foreach ($this->properties
as $property) {
186 if (($cb = $property->read($row, $object))) {
190 $this->drain($deferred, function($cb) use($object, $row) {
191 return $cb($object, $row);
193 if ($row->isDirty()) {