Skip to content
Browse files

Fixing array mapping in `Entity` and `Document` classes.

  • Loading branch information...
1 parent 3c2b893 commit 4e7d1861f7c7587a9ed9b5c59d4d109e8630caee @nateabele nateabele committed May 16, 2011
Showing with 15 additions and 18 deletions.
  1. +3 −6 data/Entity.php
  2. +8 −11 data/entity/Document.php
  3. +4 −1 util/Collection.php
View
9 data/Entity.php
@@ -378,15 +378,12 @@ public function export() {
* @return mixed
*/
public function to($format, array $options = array()) {
- $map = function($obj) {
- return $obj->data();
- };
-
switch ($format) {
case 'array':
$data = $this->_updated + $this->_data;
- $data = array_merge($data, array_map($map, $this->_relationships));
- $result = Collection::toArray($data);
+ $rel = array_map(function($obj) { return $obj->data(); }, $this->_relationships);
+ $data = array_merge($data, $rel);
+ $result = Collection::toArray($data, $options);
break;
default:
$result = $this;
View
19 data/entity/Document.php
@@ -377,11 +377,13 @@ public function valid() {
}
public function current() {
- return current($this->_data);
+ $current = current($this->_data);
+ return isset($this->_removed[key($this->_data)]) ? null : $current;
}
public function key() {
- return key($this->_data);
+ $key = key($this->_data);
+ return isset($this->_removed[$key]) ? false : $key;
}
/**
@@ -397,15 +399,7 @@ public function to($format, array $options = array()) {
'MongoDate' => function($value) { return $value->sec; }
));
$options += $defaults;
-
- if ($format == 'array') {
- $map = function($obj) {
- return $obj->data();
- };
- $data = $this->_updated + $this->_data;
- $data = array_merge($data, array_map($map, $this->_relationships));
- return Collection::toArray(array_diff_key($data, $this->_removed), $options);
- }
+ $options['internal'] = false;
return parent::to($format, $options);
}
@@ -422,6 +416,9 @@ public function next() {
$this->_valid = (next($this->_data) !== false);
$cur = key($this->_data);
+ if (isset($this->_removed[$cur])) {
+ return $this->next();
+ }
if (!$this->_valid && $cur !== $prev && $cur !== null) {
$this->_valid = true;
}
View
5 util/Collection.php
@@ -514,6 +514,9 @@ public static function toArray($data, array $options = array()) {
foreach ($data as $key => $item) {
switch (true) {
+ case is_array($item):
+ $result[$key] = static::toArray($item, $options);
+ break;
case (!is_object($item)):
$result[$key] = $item;
break;
@@ -524,7 +527,7 @@ public static function toArray($data, array $options = array()) {
$result[$key] = $item->to('array');
break;
case ($vars = get_object_vars($item)):
- $result[$key] = $vars;
+ $result[$key] = static::toArray($vars, $options);
break;
case (method_exists($item, '__toString')):
$result[$key] = (string) $item;

0 comments on commit 4e7d186

Please sign in to comment.
Something went wrong with that request. Please try again.