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) {
35 * Reset all managed objects
43 * Get the serialized row identity
45 * When $check is true, the identity will only be serialized if all columns
46 * of the primary key are set.
50 * @return string|false serialized row id or false on failure
52 function rowId(Row
$row, $check = false) {
54 $identity = $row->getIdentity();
55 } catch (OutOfBoundsException
$e) {
58 return $this->serializeRowId($identity, $check);
62 * Get an object's identity
63 * @param object $object
66 function objectId($object) {
67 return spl_object_hash($object);
71 * Extract a row's identity from a mapped object
72 * @param object $object
73 * @return string serialized row identity
75 function extractRowId($object) {
77 foreach ($this->map
->getGateway()->getIdentity() as $col) {
78 foreach ($this->map
->getProperties() as $property) {
79 if ($property->exposes($col)) {
80 $id[$col] = $property->extract($object);
84 return $this->serializeRowId($id, true);
88 * Serialize a row's identity
89 * @param mixed $identity
91 * @return string|false the serialized row identity or false on failure
93 function serializeRowId($identity, $check = false) {
94 if (is_scalar($identity)) {
98 if ($check && !isset($identity)) {
102 if (is_array($identity)) {
103 if ($check && array_search(null, $identity, true)) {
106 /* one level is better than no level */
109 return json_encode($identity);
113 * Check whether a mapped object is already cached in the manager
114 * @param string $row_id
117 function hasObject($row_id) {
118 return isset($this->obj
[$row_id]);
122 * Create a mapped object from $row
126 function createObject(Row
$row) {
127 $rid = $this->rowId($row);
128 $cls = $this->map
->getClass();
130 $oid = $this->objectId($obj);
131 $this->obj
[$rid] = $obj;
132 $this->row
[$oid] = $row;
137 * Forget the mapped object of $row
140 function resetObject(Row
$row) {
141 unset($this->obj
[$this->rowId($row)]);
145 * Get the mapped object of $row
149 function getObject(Row
$row) {
150 $id = $this->rowId($row);
151 return $this->getObjectById($id);
155 * Get the mapped object of $row
156 * @param string $row_id
158 * @throws BadMethodCallException
160 function getObjectById($row_id) {
161 if (!$this->hasObject($row_id)) {
162 throw new BadMethodCallException("Object of row with id $row_id does not exist");
164 return $this->obj
[$row_id];
168 * Check for a mapped object of $row, and create if necessary
172 function asObject(Row
$row){
173 return $this->hasObject($this->rowId($row))
174 ?
$this->getObject($row)
175 : $this->createObject($row);
179 * Check whether a row for a mapped object exists
180 * @param string $obj_id
183 function hasRow($obj_id) {
184 return isset($this->row
[$obj_id]);
188 * Initialize a Row from a mapped object
189 * @param object $object
192 function createRow($object) {
193 $oid = $this->objectId($object);
194 $row = new Row($this->map
->getGateway());
195 $this->row
[$oid] = $row;
200 * Forget about a row of a mapped object
201 * @param object $object
203 function resetRow($object) {
204 unset($this->row
[$this->objectId($object)]);
208 * Get the row of a mapped object
209 * @param object $object
211 * @throws BadMethodCallException
213 function getRow($object) {
214 $id = $this->objectId($object);
216 if (!$this->hasRow($id)) {
217 throw new BadMethodCallException("Row for object with id $id does not exist");
219 return $this->row
[$id];
223 * Check for a row of a mapped object, create from object if neccessary
224 * @param object $object
227 function asRow($object) {
228 return $this->hasRow($this->objectId($object))
229 ?
$this->getRow($object)
230 : $this->createRow($object);