ソースを参照

Merge pull request #5727 from robertpustulka/3.0-before-marshal-upgrade

3.0 beforeMarshal - Make use of modifiable $options
Mark Story 11 年 前
コミット
f8b1aa65ff
2 ファイル変更16 行追加17 行削除
  1. 12 13
      src/ORM/Marshaller.php
  2. 4 4
      tests/TestCase/ORM/MarshallerTest.php

+ 12 - 13
src/ORM/Marshaller.php

@@ -100,7 +100,8 @@ class Marshaller
      */
     public function one(array $data, array $options = [])
     {
-        $options += ['validate' => true];
+        list($data, $options) = $this->_prepareDataAndOptions($data, $options);
+
         $propertyMap = $this->_buildPropertyMap($options);
 
         $schema = $this->_table->schema();
@@ -114,8 +115,6 @@ class Marshaller
             }
         }
 
-        $data = $this->_prepareData($data, $options);
-
         $errors = $this->_validate($data, $options, true);
         $primaryKey = $schema->primaryKey();
         $properties = [];
@@ -183,25 +182,26 @@ class Marshaller
     }
 
     /**
-     * Returns data prepared to validate and marshall.
+     * Returns data and options prepared to validate and marshall.
      *
      * @param array $data The data to prepare.
      * @param array $options The options passed to this marshaller.
-     * @return array Prepared data.
+     * @return array An array containing prepared data and options.
      */
-    protected function _prepareData($data, $options)
+    protected function _prepareDataAndOptions($data, $options)
     {
-        $tableName = $this->_table->alias();
+        $options += ['validate' => true];
 
+        $tableName = $this->_table->alias();
         if (isset($data[$tableName])) {
             $data = $data[$tableName];
         }
 
-        $dataObject = new \ArrayObject($data);
+        $data = new \ArrayObject($data);
         $options = new \ArrayObject($options);
-        $this->_table->dispatchEvent('Model.beforeMarshal', compact('dataObject', 'options'));
+        $this->_table->dispatchEvent('Model.beforeMarshal', compact('data', 'options'));
 
-        return (array)$dataObject;
+        return [(array)$data, (array)$options];
     }
 
     /**
@@ -343,7 +343,8 @@ class Marshaller
      */
     public function merge(EntityInterface $entity, array $data, array $options = [])
     {
-        $options += ['validate' => true];
+        list($data, $options) = $this->_prepareDataAndOptions($data, $options);
+
         $propertyMap = $this->_buildPropertyMap($options);
         $isNew = $entity->isNew();
         $keys = [];
@@ -352,8 +353,6 @@ class Marshaller
             $keys = $entity->extract((array)$this->_table->primaryKey());
         }
 
-        $data = $this->_prepareData($data, $options);
-
         $errors = $this->_validate($data + $keys, $options, $isNew);
         $schema = $this->_table->schema();
         $properties = [];

+ 4 - 4
tests/TestCase/ORM/MarshallerTest.php

@@ -1764,14 +1764,14 @@ class MarshallerTest extends TestCase
 
         $marshall = new Marshaller($this->articles);
 
-        $this->articles->eventManager()->attach(function ($e, $data) {
+        $this->articles->eventManager()->attach(function ($e, $data, $options) {
             $data['title'] = 'Modified title';
             $data['user']['username'] = 'robert';
+
+            $options['associated'] = ['Users'];
         }, 'Model.beforeMarshal');
 
-        $entity = $marshall->one($data, [
-            'associated' => ['Users']
-        ]);
+        $entity = $marshall->one($data);
 
         $this->assertEquals('Modified title', $entity->title);
         $this->assertEquals('My content', $entity->body);