1 | <?php |
||
35 | abstract class MongoObject implements \IteratorAggregate, \ArrayAccess |
||
36 | { |
||
37 | /** |
||
38 | * @var \MongoCollection collection used by instance |
||
39 | */ |
||
40 | protected $myCollection; |
||
41 | |||
42 | /** |
||
43 | * @var String |
||
44 | */ |
||
45 | protected $myCollectionName; |
||
46 | |||
47 | /** |
||
48 | * @var array container to store the data of this object |
||
49 | */ |
||
50 | protected $myData = null; |
||
51 | |||
52 | /** |
||
53 | * @var MongoServiceInterface |
||
54 | */ |
||
55 | protected $myFactory; |
||
56 | |||
57 | /** |
||
58 | * constructor, meant to setup the object, should be called by derived classes |
||
59 | * |
||
60 | * @param MongoServiceInterface $factory |
||
61 | */ |
||
62 | public function __construct(MongoServiceInterface $factory) |
||
66 | |||
67 | /** |
||
68 | * sets the collection name - this will call |
||
69 | * __wakeup to ensure the right collection is loaded, |
||
70 | * use with care. |
||
71 | * |
||
72 | * @param string $collectioName |
||
73 | * @throws \MongoException |
||
74 | */ |
||
75 | public function setCollection($collectioName) |
||
80 | |||
81 | /** |
||
82 | * load data from array |
||
83 | * |
||
84 | * @param $data |
||
85 | * @return void |
||
86 | */ |
||
87 | public function loadFromArray(array $data) |
||
91 | |||
92 | /** |
||
93 | * remove the instance from the mongodb - this will not kill the object in local space however |
||
94 | * |
||
95 | * @return void |
||
96 | * @throws \MongoCursorException |
||
97 | * @throws \MongoCursorTimeoutException |
||
98 | */ |
||
99 | public function delete() |
||
103 | |||
104 | /** |
||
105 | * save the object in the mongodb, will insert on new object, or update if _id is set |
||
106 | * |
||
107 | * @return void |
||
108 | * @throws \MongoCursorException |
||
109 | * @throws \MongoCursorTimeoutException |
||
110 | * @throws \MongoException |
||
111 | */ |
||
112 | public function save() |
||
116 | |||
117 | /** |
||
118 | * refreshes object from database (all changes be lost!) |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | public function refresh() |
||
128 | // --------------------------- Iterator Fun |
||
129 | /** |
||
130 | * @return \ArrayIterator |
||
131 | */ |
||
132 | public function getIterator() |
||
136 | // --------------------------- ARRAY ACCESS |
||
137 | /** |
||
138 | * @param string $offset |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function offsetExists($offset) |
||
145 | |||
146 | /** |
||
147 | * @param string $offset |
||
148 | * @return mixed |
||
149 | */ |
||
150 | public function offsetGet($offset) |
||
154 | |||
155 | /** |
||
156 | * @param string $offset |
||
157 | */ |
||
158 | public function offsetUnset($offset) |
||
162 | |||
163 | /** |
||
164 | * @param $offset |
||
165 | * @param $value |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function offsetSet($offset, $value) |
||
172 | // --------------------------- OBJECT STYLE ACCESS |
||
173 | /** |
||
174 | * @param string $name |
||
175 | * @param mixed $value |
||
176 | * @return void |
||
177 | */ |
||
178 | public function __set($name, $value) |
||
182 | |||
183 | /** |
||
184 | * @param string $name |
||
185 | * @return mixed |
||
186 | */ |
||
187 | public function __get($name) |
||
195 | // --------------------- unserialize |
||
196 | |||
197 | /** |
||
198 | * Magic wakeup method, will reconnect object on unserialze |
||
199 | * |
||
200 | * @throws \MongoException |
||
201 | * @throws \Exception |
||
202 | * @return void |
||
203 | */ |
||
204 | public function __wakeup() |
||
208 | } |
||
209 |