5 use OutOfBoundsException
;
6 use pq\Exception\BadMethodCallException
;
27 * Create a new ObjectManager for a mapping
28 * @param MapInterface $map
30 function __construct(MapInterface
$map) {
36 * @return MapInterface
43 * Reset all managed objects
51 * Get the serialized row identity
53 * When $check is true, the identity will only be serialized if all columns
54 * of the primary key are set.
58 * @return string|false serialized row id or false on failure
60 function rowId(Row
$row, $check = false) {
62 $identity = $row->getIdentity();
63 } catch (OutOfBoundsException
$e) {
66 return $this->serializeRowId($identity, $check);
70 * Get an object's identity
71 * @param object $object
74 function objectId($object) {
75 return spl_object_hash($object);
79 * Extract a row's identity from a mapped object
80 * @param object $object
81 * @return string serialized row identity
83 function extractRowId($object) {
85 foreach ($this->map
->getGateway()->getIdentity() as $col) {
86 foreach ($this->map
->getProperties() as $property) {
87 if ($property->exposes($col)) {
88 $id[$col] = $property->extract($object);
92 return $this->serializeRowId($id, true);
96 * Serialize a row's identity
97 * @param mixed $identity
99 * @return string|false the serialized row identity or false on failure
101 function serializeRowId($identity, $check = false) {
102 if (is_scalar($identity)) {
106 if ($check && !isset($identity)) {
110 if (is_array($identity)) {
111 if ($check && array_search(null, $identity, true)) {
114 /* one level is better than no level */
117 return json_encode($identity);
121 * Check whether a mapped object is already cached in the manager
122 * @param string $row_id
125 function hasObject($row_id) {
126 return isset($this->obj
[$row_id]);
130 * Create a mapped object from $row
134 function createObject(Row
$row) {
135 $rid = $this->rowId($row);
136 $cls = $this->map
->getClass();
138 $oid = $this->objectId($obj);
139 $this->obj
[$rid] = $obj;
140 $this->row
[$oid] = $row;
145 * Forget the mapped object of $row
148 function resetObject(Row
$row) {
149 unset($this->obj
[$this->rowId($row)]);
153 * Get the mapped object of $row
157 function getObject(Row
$row) {
158 $id = $this->rowId($row);
159 return $this->getObjectById($id);
163 * Get the mapped object of $row
164 * @param string $row_id
166 * @throws BadMethodCallException
168 function getObjectById($row_id) {
169 if (!$this->hasObject($row_id)) {
170 throw new BadMethodCallException("Object of row with id $row_id does not exist");
172 return $this->obj
[$row_id];
176 * Check for a mapped object of $row, and create if necessary
180 function asObject(Row
$row){
181 return $this->hasObject($this->rowId($row))
182 ?
$this->getObject($row)
183 : $this->createObject($row);
187 * Check whether a row for a mapped object exists
188 * @param string $obj_id
191 function hasRow($obj_id) {
192 return isset($this->row
[$obj_id]);
196 * Initialize a Row from a mapped object
197 * @param object $object
200 function createRow($object) {
201 $oid = $this->objectId($object);
202 $row = new Row($this->map
->getGateway());
203 $this->row
[$oid] = $row;
208 * Forget about a row of a mapped object
209 * @param object $object
211 function resetRow($object) {
212 unset($this->row
[$this->objectId($object)]);
216 * Get the row of a mapped object
217 * @param object $object
219 * @throws BadMethodCallException
221 function getRow($object) {
222 $id = $this->objectId($object);
224 if (!$this->hasRow($id)) {
225 throw new BadMethodCallException("Row for object with id $id does not exist");
227 return $this->row
[$id];
231 * Check for a row of a mapped object, create from object if neccessary
232 * @param object $object
235 function asRow($object) {
236 return $this->hasRow($this->objectId($object))
237 ?
$this->getRow($object)
238 : $this->createRow($object);