浏览代码

Rebase into 3.2 and set interface.

Mark Scherer 10 年之前
父节点
当前提交
d212869e3a
共有 4 个文件被更改,包括 44 次插入10 次删除
  1. 36 0
      src/Datasource/InvalidPropertyInterface.php
  2. 5 6
      src/Datasource/RulesChecker.php
  3. 2 1
      src/ORM/Entity.php
  4. 1 3
      src/ORM/Marshaller.php

+ 36 - 0
src/Datasource/InvalidPropertyInterface.php

@@ -0,0 +1,36 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link          http://cakephp.org CakePHP(tm) Project
+ * @since         3.0.0
+ * @license       http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Datasource;
+
+/**
+ * Describes the methods that any class representing a data storage should
+ * comply with.
+ */
+interface InvalidPropertyInterface
+{
+    /**
+     * Sets a field as invalid and not patchable into the entity.
+     *
+     * This is useful for batch operations when one needs to get the original value for an error message after patching.
+     * This value could not be patched into the entity and is simply copied into the _invalid property for debugging purposes
+     * or to be able to log it away.
+     *
+     * @param string|array|null $field
+     * @param string|null $value
+     * @param bool $overwrite
+     * @return $this|mixed
+     */
+    public function invalid($field = null, $value = null, $overwrite = false);
+}

+ 5 - 6
src/Datasource/RulesChecker.php

@@ -328,13 +328,12 @@ class RulesChecker
             }
             $entity->errors($options['errorField'], $message);
 
-            if (method_exists($entity, 'invalid')) {
-                $invalid = null;
-                if (isset($entity->{$options['errorField']})) {
-                    $invalid = $entity->{$options['errorField']};
-                }
-                $entity->invalid($options['errorField'], $invalid);
+            $invalid = null;
+            if (isset($entity->{$options['errorField']})) {
+                $invalid = $entity->{$options['errorField']};
             }
+            $entity->invalid($options['errorField'], $invalid);
+
             return $pass === true;
         };
     }

+ 2 - 1
src/ORM/Entity.php

@@ -15,13 +15,14 @@
 namespace Cake\ORM;
 
 use Cake\Datasource\EntityInterface;
+use Cake\Datasource\InvalidPropertyInterface;
 use Cake\Datasource\EntityTrait;
 
 /**
  * An entity represents a single result row from a repository. It exposes the
  * methods for retrieving and storing properties associated in this row.
  */
-class Entity implements EntityInterface
+class Entity implements EntityInterface, InvalidPropertyInterface
 {
 
     use EntityTrait;

+ 1 - 3
src/ORM/Marshaller.php

@@ -128,9 +128,7 @@ class Marshaller
         $properties = [];
         foreach ($data as $key => $value) {
             if (!empty($errors[$key])) {
-                if (method_exists($entity, 'invalid')) {
-                    $entity->invalid($key, $value);
-                }
+                $entity->invalid($key, $value);
                 continue;
             }
             $columnType = $schema->columnType($key);