1: <?php
2:
3: namespace PHPixie\ORM\Loaders\Loader\Embedded;
4:
5: class ArrayNode extends \PHPixie\ORM\Loaders\Loader\Embedded
6: {
7: protected $arrayNode;
8: protected $owner;
9: protected $ownerPropertyName;
10:
11: protected $cachedEntities = array();
12:
13:
14: public function __construct($loaders, $embeddedModel, $modelName, $arrayNode, $owner, $ownerPropertyName)
15: {
16: parent::__construct($loaders, $embeddedModel, $modelName);
17: $this->arrayNode = $arrayNode;
18: $this->owner = $owner;
19: $this->ownerPropertyName = $ownerPropertyName;
20: }
21:
22: public function offsetExists($offset)
23: {
24: return $this->arrayNode->offsetExists($offset);
25: }
26:
27: public function getByOffset($offset)
28: {
29: if(!array_key_exists($offset, $this->cachedEntities)) {
30:
31: if(!$this->offsetExists($offset))
32: throw new \PHPixie\ORM\Exception\Loader("Offset $offset does not exist.");
33:
34: $document = $this->arrayNode->offsetGet($offset);
35:
36: $entity = $this->loadEntity($document);
37: $entity->setOwnerRelationship($this->owner, $this->ownerPropertyName);
38: $this->cachedEntities[$offset] = $entity;
39:
40: }
41:
42: return $this->cachedEntities[$offset];
43: }
44:
45: public function cacheEntity($offset, $model)
46: {
47: $this->cachedEntities[$offset] = $model;
48: }
49:
50: public function shiftCachedEntities($offset)
51: {
52: if(array_key_exists($offset, $this->cachedEntities))
53: unset($this->cachedEntities[$offset]);
54:
55: $keys = array_keys($this->cachedEntities);
56: foreach($keys as $key) {
57: if($key <= $offset)
58: continue;
59:
60: $this->cachedEntities[$key-1] = $this->cachedEntities[$key];
61: unset($this->cachedEntities[$key]);
62: }
63: }
64:
65: public function getCachedEntity($offset)
66: {
67: if(array_key_exists($offset, $this->cachedEntities))
68: return $this->cachedEntities[$offset];
69:
70: return null;
71: }
72:
73: public function getCachedEntities()
74: {
75: return $this->cachedEntities;
76: }
77:
78: public function clearCachedEntities()
79: {
80: $this->cachedEntities = array();
81: }
82:
83: public function count()
84: {
85: return $this->arrayNode->count();
86: }
87:
88: public function arrayNode()
89: {
90: return $this->arrayNode;
91: }
92:
93: }
94: