Skip to content
Newer
Older
100644 211 lines (187 sloc) 6.19 KB
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
1 <?php
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored Jan 2, 2012
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
8
9 namespace lithium\data\collection;
10
11 class DocumentSet extends \lithium\data\Collection {
12
13 /**
14 * PHP magic method used when setting properties on the `Document` instance, i.e.
15 * `$document->title = 'Lorem Ipsum'`. If `$value` is a complex data type (i.e. associative
16 * array), it is wrapped in a sub-`Document` object before being appended.
17 *
18 * @param $name The name of the field/property to write to, i.e. `title` in the above example.
19 * @param $value The value to write, i.e. `'Lorem Ipsum'`.
20 * @return void
21 */
22 public function __set($name, $value = null) {
23 if (is_array($name) && !$value) {
24 foreach ($name as $key => $value) {
25 $this->__set($key, $value);
26 }
27 return;
28 }
29
30 if (is_string($name) && strpos($name, '.')) {
31 $current = $this;
32 $path = explode('.', $name);
33 $length = count($path) - 1;
34
35 for ($i = 0; $i < $length; $i++) {
36 $key = $path[$i];
37 $next = $current->__get($key);
38
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored Oct 11, 2010
39 if (!is_object($next) && ($model = $this->_model)) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored Dec 29, 2010
40 $next = $model::connection()->cast($this, $next);
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored Oct 11, 2010
41 $current->_data[$key] = $next;
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
42 }
43 $current = $next;
44 }
45 $current->__set(end($path), $value);
46 }
47
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
48 if (is_array($value)) {
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
49 $value = $this->_relation('set', $name, $value);
50 }
51 $this->_data[$name] = $value;
52 }
53
54 /**
55 * PHP magic method used to check the presence of a field as document properties, i.e.
56 * `$document->_id`.
57 *
58 * @param $name The field name, as specified with an object property.
59 * @return boolean True if the field specified in `$name` exists, false otherwise.
60 */
61 public function __isset($name) {
62 return isset($this->_data[$name]);
63 }
64
65 /**
66 * PHP magic method used when unset() is called on a `Document` instance.
67 * Use case for this would be when you wish to edit a document and remove a field, ie. :
68 * {{{ $doc = Post::find($id); unset($doc->fieldName); $doc->save(); }}}
69 *
70 * @param unknown_type $name
71 * @return unknown_type
72 */
73 public function __unset($name) {
74 unset($this->_data[$name]);
75 }
76
77 /**
78 * Allows several properties to be assigned at once.
79 *
80 * For example:
81 * {{{
82 * $doc->set(array('title' => 'Lorem Ipsum', 'value' => 42));
83 * }}}
84 *
85 * @param $values An associative array of fields and values to assign to the `Document`.
86 * @return void
87 */
88 public function set($values) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
89 foreach ($values as $key => $val) {
90 $this[$key] = $val;
91 }
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
92 }
93
94 /**
95 * Allows document fields to be accessed as array keys, i.e. `$document['_id']`.
96 *
97 * @param mixed $offset String or integer indicating the offset or index of a document in a set,
98 * or the name of a field in an individual document.
99 * @return mixed Returns either a sub-object in the document, or a scalar field value.
100 */
101 public function offsetGet($offset) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
102 $data = null;
103 $null = null;
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
104 $model = $this->_model;
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
105
106 if (!isset($this->_data[$offset]) && !$data = $this->_populate(null, $offset)) {
107 return $null;
108 }
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored Oct 11, 2010
109 if (is_array($data = $this->_data[$offset]) && $model) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored Dec 29, 2010
110 $this->_data[$offset] = $model::connection()->cast($this, $data);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
111 }
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored Oct 25, 2010
112 if (isset($this->_data[$offset])) {
113 return $this->_data[$offset];
114 }
115 return $null;
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
116 }
117
118 /**
119 * Rewinds the collection of sub-`Document`s to the beginning and returns the first one found.
120 *
121 * @return object Returns the first `Document` object instance in the collection.
122 */
123 public function rewind() {
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
124 $data = parent::rewind() ?: $this->_populate();
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
125 $key = key($this->_data);
126
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
127 if (is_object($data)) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
128 return $data;
129 }
130
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
131 if (isset($this->_data[$key])) {
132 return $this->offsetGet($key);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
133 }
134 }
135
136 public function current() {
137 return $this->offsetGet(key($this->_data));
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
138 }
139
140 /**
141 * Returns the next document in the set, and advances the object's internal pointer. If the end
142 * of the set is reached, a new document will be fetched from the data source connection handle
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
143 * If no more documents can be fetched, returns `null`.
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
144 *
c25e039 @Howard3 Cleaning up PHPDoc @return tags with multiple return types defined.
Howard3 authored Jun 12, 2011
145 * @return mixed Returns the next document in the set, or `null`, if no more documents are
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
146 * available.
147 */
148 public function next() {
149 $prev = key($this->_data);
3c2b893 @nateabele Improving cursor validity checks for classes which implement `Iterator`.
nateabele authored May 12, 2011
150 $this->_valid = !(next($this->_data) === false && key($this->_data) === null);
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
151 $cur = key($this->_data);
152
153 if (!$this->_valid && $cur !== $prev && $cur !== null) {
154 $this->_valid = true;
155 }
156 $this->_valid = $this->_valid ?: !is_null($this->_populate());
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
157 return $this->_valid ? $this->offsetGet(key($this->_data)) : null;
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
158 }
159
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored Dec 29, 2010
160 public function export(array $options = array()) {
161 $map = function($doc) use ($options) {
162 return is_array($doc) ? $doc : $doc->export();
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
163 };
164 return array_map($map, $this->_data);
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
165 }
166
167 /**
168 * Lazy-loads a document from a query using a reference to a database adapter and a query
169 * result resource.
170 *
171 * @param array $data
172 * @param mixed $key
173 * @return array
174 */
175 protected function _populate($data = null, $key = null) {
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
176 if ($this->closed() || !($model = $this->_model)) {
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
177 return;
178 }
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored Sep 17, 2010
179 $conn = $model::connection();
180
aeed15c @nateabele Moving database results handling to self-contained iterable result ob…
nateabele authored Oct 13, 2010
181 if (($data = $data ?: $this->_result->next()) === null) {
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
182 return $this->close();
183 }
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored Oct 26, 2010
184 $options = array('exists' => true, 'first' => true, 'pathKey' => $this->_pathKey);
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored Dec 29, 2010
185 return $this->_data[] = $conn->cast($this, array($key => $data), $options);
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
186 }
187
188 /**
189 * Instantiates a new `Document` object as a descendant of the current object, and sets all
190 * default values and internal state.
191 *
192 * @param string $classType The type of class to create, either `'entity'` or `'set'`.
8de98e9 @nateabele Fixing coding standards violations. Closes ticket #143.
nateabele authored Aug 24, 2010
193 * @param string $key The key name to which the related object is assigned.
194 * @param array $data The internal data of the related object.
195 * @param array $options Any other options to pass when instantiating the related object.
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
196 * @return object Returns a new `Document` object instance.
197 */
198 protected function _relation($classType, $key, $data, $options = array()) {
199 $parent = $this;
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
200 $model = $this->_model;
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
201
202 if (is_object($data) && $data instanceof Document) {
203 $data->assignTo($this, compact('model', 'pathKey'));
204 return $data;
205 }
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored Jun 15, 2010
206 $options += compact('model', 'data', 'parent');
27bd48e @nateabele Adding missing files from last commit.
nateabele authored Jun 3, 2010
207 return new $this->_classes[$classType]($options);
208 }
209 }
210
211 ?>
Something went wrong with that request. Please try again.