Skip to content
Browse files

Cleanup and documentation for misc. `\data` classes. Refactored `Docu…

…ment::__set()` and `Exporter::cast()` to reduce call count.
  • Loading branch information...
1 parent 8b132d5 commit ad3ccf393acc981ffd2342932e66bbfcf36cb3e2 @nateabele nateabele committed May 12, 2011
Showing with 73 additions and 52 deletions.
  1. +3 −1 data/Entity.php
  2. +8 −7 data/Source.php
  3. +13 −0 data/collection/DocumentArray.php
  4. +10 −11 data/entity/Document.php
  5. +5 −1 data/source/MongoDb.php
  6. +34 −32 data/source/mongo_db/Exporter.php
View
4 data/Entity.php
@@ -288,8 +288,10 @@ public function exists() {
/**
* Called after an `Entity` is saved. Updates the object's internal state to reflect the
* corresponding database entity, and sets the `Entity` object's key, if this is a newly-created
- * object.
+ * object. **Do not** call this method if you intend to update the database's copy of the
+ * entity. Instead, see `Model::save()`.
*
+ * @see lithium\data\Model::save()
* @param mixed $id The ID to assign, where applicable.
* @param array $data Any additional generated data assigned to the object by the database.
* @return void
View
15 data/Source.php
@@ -251,12 +251,10 @@ public function methods() {
* dependencies.
*/
public function configureClass($class) {
- return array(
- 'meta' => array(
- 'key' => 'id',
- 'locked' => true
- )
- );
+ return array('meta' => array(
+ 'key' => 'id',
+ 'locked' => true
+ ));
}
/**
@@ -273,7 +271,10 @@ public function configureClass($class) {
public function item($model, array $data = array(), array $options = array()) {
$defaults = array('class' => 'entity');
$options += $defaults;
- return $this->_instance($options['class'], compact('model', 'data') + $options);
+
+ $class = $options['class'];
+ unset($options['class']);
+ return $this->_instance($class, compact('model', 'data') + $options);
}
}
View
13 data/collection/DocumentArray.php
@@ -12,9 +12,22 @@
class DocumentArray extends \lithium\data\Collection {
+ /**
+ * Indicates whether this array was part of a document loaded from a data source, or is part of
+ * a new document, or is in newly-added field of an existing document.
+ *
+ * @var boolean
+ */
protected $_exists = false;
/**
+ * Contains an array that is matched against .
+ *
+ * @var array
+ */
+ protected $_updated = array();
+
+ /**
* Holds an array of values that should be processed on initialization.
*
* @var array
View
21 data/entity/Document.php
@@ -220,22 +220,21 @@ protected function &_getNested($name) {
* @return void
*/
public function __set($name, $value = null) {
- if (is_array($name)) {
- foreach ($name as $key => $val) {
- $this->__set($key, $val);
+ $data = is_array($name) ? $name : array($name => $value);
+
+ foreach ($data as $key => $val) {
+ if (strpos($key, '.')) {
+ $this->_setNested($key, $val);
+ unset($data[$key]);
}
- return;
- }
- if (is_string($name) && strpos($name, '.')) {
- return $this->_setNested($name, $value);
+ unset($this->_increment[$key], $this->_removed[$key]);
}
+
if ($model = $this->_model) {
$pathKey = $this->_pathKey;
- $options = compact('pathKey') + array('first' => true);
- $value = $model::connection()->cast($this, array($name => $value), $options);
+ $data = $model::connection()->cast($this, $data, compact('pathKey'));
}
- $this->_updated[$name] = $value;
- unset($this->_increment[$name], $this->_removed[$name]);
+ $this->_updated = $data + $this->_updated;
}
protected function _setNested($name, $value) {
View
6 data/source/MongoDb.php
@@ -120,6 +120,7 @@ class MongoDb extends \lithium\data\Source {
*
* @see lithium\data\Connections::add()
* @see lithium\data\source\MongoDb::$_schema
+ * @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
* @param array $config All information required to connect to the database, including:
* - `'database'` _string_: The name of the database to connect to. Defaults to `null`.
* - `'host'` _string_: The IP or machine name where Mongo is running, followed by a
@@ -232,8 +233,11 @@ public function configureClass($class) {
}
/**
- * Connects to the Mongo server.
+ * Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo
+ * database connection.
*
+ * @see lithium\data\source\MongoDb::__construct()
+ * @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
* @return boolean Returns `true` the connection attempt was successful, otherwise `false`.
*/
public function connect() {
View
66 data/source/mongo_db/Exporter.php
@@ -37,46 +37,48 @@ public static function cast($data, $schema, $database, array $options = array())
'pathKey' => null, 'handlers' => array(), 'model' => null, 'arrays' => true
);
$options += $defaults;
- $typeMap = static::$_map;
foreach ($data as $key => $value) {
- if (is_object($value)) {
- continue;
- }
- $path = $options['pathKey'] ? "{$options['pathKey']}.{$key}" : $key;
- $field = (isset($schema[$path]) ? $schema[$path] : array());
+ $pathKey = $options['pathKey'] ? "{$options['pathKey']}.{$key}" : $key;
+ $field = (isset($schema[$pathKey]) ? $schema[$pathKey] : array());
$field += array('type' => null, 'array' => null);
- $type = isset($typeMap[$field['type']]) ? $typeMap[$field['type']] : $field['type'];
+ $data[$key] = static::_cast($value, $field, $database, $options + compact('pathKey'));
+ }
+ return $data;
+ }
- $isObject = ($type == 'object');
- $isArray = (is_array($value) && $field['array'] !== false && !$isObject);
- $isArray = $field['array'] || $isArray;
+ protected static function _cast($value, $def, $database, $options) {
+ if (is_object($value)) {
+ return $value;
+ }
+ $pathKey = $options['pathKey'];
+ $typeMap = static::$_map;
+ $type = isset($typeMap[$def['type']]) ? $typeMap[$def['type']] : $def['type'];
- if (isset($options['handlers'][$type]) && $handler = $options['handlers'][$type]) {
- $value = $isArray ? array_map($handler, (array) $value) : $handler($value);
- }
- if (!$options['arrays']) {
- $data[$key] = $value;
- continue;
- }
- $pathKey = $path;
+ $isObject = ($type == 'object');
+ $isArray = (is_array($value) && $def['array'] !== false && !$isObject);
+ $isArray = $def['array'] || $isArray;
- if (!is_array($value) && !$field['array']) {
- $data[$key] = $value;
- continue;
- }
+ if (isset($options['handlers'][$type]) && $handler = $options['handlers'][$type]) {
+ $value = $isArray ? array_map($handler, (array) $value) : $handler($value);
+ }
+ if (!$options['arrays']) {
+ return $value;
+ }
- if ($field['array']) {
- $opts = array('class' => 'array') + $options;
- $value = ($value === null) ? array() : $value;
- $value = is_array($value) ? $value : array($value);
- } elseif (is_array($value)) {
- $arrayType = !$isObject && (array_keys($value) === range(0, count($value) - 1));
- $opts = $arrayType ? array('class' => 'array') + $options : $options;
- }
- $data[$key] = $database->item($options['model'], $value, compact('pathKey') + $opts);
+ if (!is_array($value) && !$def['array']) {
+ return $value;
}
- return $data;
+
+ if ($def['array']) {
+ $opts = array('class' => 'array') + $options;
+ $value = ($value === null) ? array() : $value;
+ $value = is_array($value) ? $value : array($value);
+ } elseif (is_array($value)) {
+ $arrayType = !$isObject && (array_keys($value) === range(0, count($value) - 1));
+ $opts = $arrayType ? array('class' => 'array') + $options : $options;
+ }
+ return $database->item($options['model'], $value, compact('pathKey') + $opts);
}
public static function toCommand($changes) {

0 comments on commit ad3ccf3

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