5 use OutOfBoundsException
;
6 use pq\Exception\BadMethodCallException
;
15 function __construct(MapInterface
$map) {
24 function rowId(Row
$row, $check = false) {
26 $identity = $row->getIdentity();
27 } catch (OutOfBoundsException
$e) {
30 return $this->serializeRowId($identity, $check);
33 function objectId($object) {
34 return spl_object_hash($object);
37 function extractRowId($object) {
39 foreach ($this->map
->getGateway()->getIdentity() as $col) {
40 foreach ($this->map
->getProperties() as $property) {
41 if ($property->exposes($col)) {
42 $id[$col] = $property->extract($object);
46 return $this->serializeRowId($id, true);
49 function serializeRowId($identity, $check = false) {
50 if (is_scalar($identity)) {
54 if ($check && !isset($identity)) {
58 if (is_array($identity)) {
59 if ($check && array_search(null, $identity, true)) {
62 /* one level is better than no level */
65 return json_encode($identity);
68 function hasObject($row_id) {
69 return isset($this->obj
[$row_id]);
72 function createObject(Row
$row) {
73 $rid = $this->rowId($row);
74 $cls = $this->map
->getClass();
76 $oid = $this->objectId($obj);
77 $this->obj
[$rid] = $obj;
78 $this->row
[$oid] = $row;
82 function resetObject(Row
$row) {
83 unset($this->obj
[$this->rowId($row)]);
86 function getObject(Row
$row) {
87 $id = $this->rowId($row);
88 return $this->getObjectById($id);
91 function getObjectById($row_id) {
92 if (!$this->hasObject($row_id)) {
93 throw new BadMethodCallException("Object of row with id $row_id does not exist");
95 return $this->obj
[$row_id];
98 function asObject(Row
$row){
99 return $this->hasObject($this->rowId($row))
100 ?
$this->getObject($row)
101 : $this->createObject($row);
104 function hasRow($obj_id) {
105 return isset($this->row
[$obj_id]);
108 function createRow($object) {
109 $oid = $this->objectId($object);
110 $row = new Row($this->map
->getGateway());
111 $this->row
[$oid] = $row;
115 function resetRow($object) {
116 unset($this->row
[$this->objectId($object)]);
119 function getRow($object) {
120 $id = $this->objectId($object);
122 if (!$this->hasRow($id)) {
123 throw new BadMethodCallException("Row for object with id $id does not exist");
125 return $this->row
[$id];
128 function asRow($object) {
129 return $this->hasRow($this->objectId($object))
130 ?
$this->getRow($object)
131 : $this->createRow($object);