Browse Source

Merge pull request #7416 from skie/3.1-entity-interface

Change method parameters declaration from Entity to EntityInterface.
José Lorenzo Rodríguez 10 years ago
parent
commit
2bacf97f02

+ 1 - 1
src/ORM/Association/BelongsToMany.php

@@ -854,7 +854,7 @@ class BelongsToMany extends Association
     /**
      * Throws an exception should any of the passed entities is not persisted.
      *
-     * @param \Cake\ORM\Entity $sourceEntity the row belonging to the `source` side
+     * @param \Cake\Datasource\EntityInterface $sourceEntity the row belonging to the `source` side
      *   of this association
      * @param array $targetEntities list of entities belonging to the `target` side
      *   of this association

+ 9 - 8
src/ORM/AssociationCollection.php

@@ -15,6 +15,7 @@
 namespace Cake\ORM;
 
 use ArrayIterator;
+use Cake\Datasource\EntityInterface;
 use Cake\ORM\Association;
 use Cake\ORM\AssociationsNormalizerTrait;
 use Cake\ORM\Entity;
@@ -160,13 +161,13 @@ class AssociationCollection implements IteratorAggregate
      * is the owning side.
      *
      * @param \Cake\ORM\Table $table The table entity is for.
-     * @param \Cake\ORM\Entity $entity The entity to save associated data for.
+     * @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
      * @param array $associations The list of associations to save parents from.
      *   associations not in this list will not be saved.
      * @param array $options The options for the save operation.
      * @return bool Success
      */
-    public function saveParents(Table $table, Entity $entity, $associations, array $options = [])
+    public function saveParents(Table $table, EntityInterface $entity, $associations, array $options = [])
     {
         if (empty($associations)) {
             return true;
@@ -181,13 +182,13 @@ class AssociationCollection implements IteratorAggregate
      * is not the owning side.
      *
      * @param \Cake\ORM\Table $table The table entity is for.
-     * @param \Cake\ORM\Entity $entity The entity to save associated data for.
+     * @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
      * @param array $associations The list of associations to save children from.
      *   associations not in this list will not be saved.
      * @param array $options The options for the save operation.
      * @return bool Success
      */
-    public function saveChildren(Table $table, Entity $entity, array $associations, array $options)
+    public function saveChildren(Table $table, EntityInterface $entity, array $associations, array $options)
     {
         if (empty($associations)) {
             return true;
@@ -199,7 +200,7 @@ class AssociationCollection implements IteratorAggregate
      * Helper method for saving an association's data.
      *
      * @param \Cake\ORM\Table $table The table the save is currently operating on
-     * @param \Cake\ORM\Entity $entity The entity to save
+     * @param \Cake\Datasource\EntityInterface $entity The entity to save
      * @param array $associations Array of associations to save.
      * @param array $options Original options
      * @param bool $owningSide Compared with association classes'
@@ -238,7 +239,7 @@ class AssociationCollection implements IteratorAggregate
      * Helper method for saving an association's data.
      *
      * @param \Cake\ORM\Association $association The association object to save with.
-     * @param \Cake\ORM\Entity $entity The entity to save
+     * @param \Cake\Datasource\EntityInterface $entity The entity to save
      * @param array $nested Options for deeper associations
      * @param array $options Original options
      * @return bool Success
@@ -258,11 +259,11 @@ class AssociationCollection implements IteratorAggregate
      * Cascade a delete across the various associations.
      * Cascade first across associations for which cascadeCallbacks is true.
      *
-     * @param \Cake\ORM\Entity $entity The entity to delete associations for.
+     * @param \Cake\Datasource\EntityInterface $entity The entity to delete associations for.
      * @param array $options The options used in the delete operation.
      * @return void
      */
-    public function cascadeDelete(Entity $entity, array $options)
+    public function cascadeDelete(EntityInterface $entity, array $options)
     {
         $noCascade = [];
         foreach ($this->_items as $assoc) {

+ 6 - 6
src/ORM/Behavior.php

@@ -61,26 +61,26 @@ use Cake\Event\EventListenerInterface;
  *   Fired when the rules checking object for the table is being built. You can use this
  *   callback to add more rules to the set.
  *
- * - `beforeRules(Event $event, Entity $entity, ArrayObject $options, $operation)`
+ * - `beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation)`
  *   Fired before an entity is validated using by a rules checker. By stopping this event,
  *   you can return the final value of the rules checking operation.
  *
- * - `afterRules(Event $event, Entity $entity, ArrayObject $options, bool $result, $operation)`
+ * - `afterRules(Event $event, EntityInterface $entity, ArrayObject $options, bool $result, $operation)`
  *   Fired after the rules have been checked on the entity. By stopping this event,
  *   you can return the final value of the rules checking operation.
  *
- * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
+ * - `beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired before each entity is saved. Stopping this event will abort the save
  *   operation. When the event is stopped the result of the event will be returned.
  *
- * - `afterSave(Event $event, Entity $entity, ArrayObject $options)`
+ * - `afterSave(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired after an entity is saved.
  *
- * - `beforeDelete(Event $event, Entity $entity, ArrayObject $options)`
+ * - `beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired before an entity is deleted. By stopping this event you will abort
  *   the delete operation.
  *
- * - `afterDelete(Event $event, Entity $entity, ArrayObject $options)`
+ * - `afterDelete(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired after an entity has been deleted.
  *
  * In addition to the core events, behaviors can respond to any

+ 10 - 9
src/ORM/Behavior/CounterCacheBehavior.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\ORM\Behavior;
 
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Association;
 use Cake\ORM\Behavior;
@@ -64,7 +65,7 @@ use Cake\ORM\Entity;
  * ```
  * [
  *     'Users' => [
- *         'posts_published' => function (Event $event, Entity $entity, Table $table) {
+ *         'posts_published' => function (Event $event, EntityInterface $entity, Table $table) {
  *             $query = $table->find('all')->where([
  *                 'published' => true,
  *                 'user_id' => $entity->get('user_id')
@@ -85,10 +86,10 @@ class CounterCacheBehavior extends Behavior
      * Makes sure to update counter cache when a new record is created or updated.
      *
      * @param \Cake\Event\Event $event The afterSave event that was fired.
-     * @param \Cake\ORM\Entity $entity The entity that was saved.
+     * @param \Cake\Datasource\EntityInterface $entity The entity that was saved.
      * @return void
      */
-    public function afterSave(Event $event, Entity $entity)
+    public function afterSave(Event $event, EntityInterface $entity)
     {
         $this->_processAssociations($event, $entity);
     }
@@ -99,10 +100,10 @@ class CounterCacheBehavior extends Behavior
      * Makes sure to update counter cache when a record is deleted.
      *
      * @param \Cake\Event\Event $event The afterDelete event that was fired.
-     * @param \Cake\ORM\Entity $entity The entity that was deleted.
+     * @param \Cake\Datasource\EntityInterface $entity The entity that was deleted.
      * @return void
      */
-    public function afterDelete(Event $event, Entity $entity)
+    public function afterDelete(Event $event, EntityInterface $entity)
     {
         $this->_processAssociations($event, $entity);
     }
@@ -111,10 +112,10 @@ class CounterCacheBehavior extends Behavior
      * Iterate all associations and update counter caches.
      *
      * @param \Cake\Event\Event $event Event instance.
-     * @param \Cake\ORM\Entity $entity Entity.
+     * @param \Cake\Datasource\EntityInterface $entity Entity.
      * @return void
      */
-    protected function _processAssociations(Event $event, Entity $entity)
+    protected function _processAssociations(Event $event, EntityInterface $entity)
     {
         foreach ($this->_config as $assoc => $settings) {
             $assoc = $this->_table->association($assoc);
@@ -126,12 +127,12 @@ class CounterCacheBehavior extends Behavior
      * Updates counter cache for a single association
      *
      * @param \Cake\Event\Event $event Event instance.
-     * @param \Cake\ORM\Entity $entity Entity
+     * @param \Cake\Datasource\EntityInterface $entity Entity
      * @param Association $assoc The association object
      * @param array $settings The settings for for counter cache for this association
      * @return void
      */
-    protected function _processAssociation(Event $event, Entity $entity, Association $assoc, array $settings)
+    protected function _processAssociation(Event $event, EntityInterface $entity, Association $assoc, array $settings)
     {
         $foreignKeys = (array)$assoc->foreignKey();
         $primaryKeys = (array)$assoc->target()->primaryKey();

+ 7 - 6
src/ORM/Behavior/TimestampBehavior.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\ORM\Behavior;
 
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\I18n\Time;
 use Cake\ORM\Behavior;
@@ -79,12 +80,12 @@ class TimestampBehavior extends Behavior
      * There is only one event handler, it can be configured to be called for any event
      *
      * @param \Cake\Event\Event $event Event instance.
-     * @param \Cake\ORM\Entity $entity Entity instance.
+     * @param \Cake\Datasource\EntityInterface $entity Entity instance.
      * @throws \UnexpectedValueException if a field's when value is misdefined
      * @return true (irrespective of the behavior logic, the save will not be prevented)
      * @throws \UnexpectedValueException When the value for an event is not 'always', 'new' or 'existing'
      */
-    public function handleEvent(Event $event, Entity $entity)
+    public function handleEvent(Event $event, EntityInterface $entity)
     {
         $eventName = $event->name();
         $events = $this->_config['events'];
@@ -153,11 +154,11 @@ class TimestampBehavior extends Behavior
      * "always" or "existing", update the timestamp value. This method will overwrite
      * any pre-existing value.
      *
-     * @param \Cake\ORM\Entity $entity Entity instance.
+     * @param \Cake\Datasource\EntityInterface $entity Entity instance.
      * @param string $eventName Event name.
      * @return bool true if a field is updated, false if no action performed
      */
-    public function touch(Entity $entity, $eventName = 'Model.beforeSave')
+    public function touch(EntityInterface $entity, $eventName = 'Model.beforeSave')
     {
         $events = $this->_config['events'];
         if (empty($events[$eventName])) {
@@ -181,12 +182,12 @@ class TimestampBehavior extends Behavior
     /**
      * Update a field, if it hasn't been updated already
      *
-     * @param \Cake\ORM\Entity $entity Entity instance.
+     * @param \Cake\Datasource\EntityInterface $entity Entity instance.
      * @param string $field Field name
      * @param bool $refreshTimestamp Whether to refresh timestamp.
      * @return void
      */
-    protected function _updateField(Entity $entity, $field, $refreshTimestamp)
+    protected function _updateField($entity, $field, $refreshTimestamp)
     {
         if ($entity->dirty($field)) {
             return;

+ 22 - 22
src/ORM/Behavior/TreeBehavior.php

@@ -80,11 +80,11 @@ class TreeBehavior extends Behavior
      * included in the parameters to be saved.
      *
      * @param \Cake\Event\Event $event The beforeSave event that was fired
-     * @param \Cake\ORM\Entity $entity the entity that is going to be saved
+     * @param \Cake\Datasource\EntityInterface $entity the entity that is going to be saved
      * @return void
      * @throws \RuntimeException if the parent to set for the node is invalid
      */
-    public function beforeSave(Event $event, Entity $entity)
+    public function beforeSave(Event $event, EntityInterface $entity)
     {
         $isNew = $entity->isNew();
         $config = $this->config();
@@ -146,10 +146,10 @@ class TreeBehavior extends Behavior
      * Manages updating level of descendents of currently saved entity.
      *
      * @param \Cake\Event\Event $event The beforeSave event that was fired
-     * @param \Cake\ORM\Entity $entity the entity that is going to be saved
+     * @param \Cake\Datasource\EntityInterface $entity the entity that is going to be saved
      * @return void
      */
-    public function afterSave(Event $event, Entity $entity)
+    public function afterSave(Event $event, EntityInterface $entity)
     {
         if (!$this->_config['level'] || $entity->isNew()) {
             return;
@@ -161,10 +161,10 @@ class TreeBehavior extends Behavior
     /**
      * Set level for descendents.
      *
-     * @param \Cake\ORM\Entity $entity The entity whose descendents need to be updated.
+     * @param \Cake\Datasource\EntityInterface $entity The entity whose descendents need to be updated.
      * @return void
      */
-    protected function _setChildrenLevel(Entity $entity)
+    protected function _setChildrenLevel($entity)
     {
         $config = $this->config();
 
@@ -198,10 +198,10 @@ class TreeBehavior extends Behavior
      * Also deletes the nodes in the subtree of the entity to be delete
      *
      * @param \Cake\Event\Event $event The beforeDelete event that was fired
-     * @param \Cake\ORM\Entity $entity The entity that is going to be saved
+     * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved
      * @return void
      */
-    public function beforeDelete(Event $event, Entity $entity)
+    public function beforeDelete(Event $event, EntityInterface $entity)
     {
         $config = $this->config();
         $this->_ensureFields($entity);
@@ -224,7 +224,7 @@ class TreeBehavior extends Behavior
      * updated to a new parent. It also makes the hole in the tree so the node
      * move can be done without corrupting the structure.
      *
-     * @param \Cake\ORM\Entity $entity The entity to re-parent
+     * @param \Cake\Datasource\EntityInterface $entity The entity to re-parent
      * @param mixed $parent the id of the parent to set
      * @return void
      * @throws \RuntimeException if the parent to set to the entity is not valid
@@ -286,7 +286,7 @@ class TreeBehavior extends Behavior
      * a new root in the tree. It also modifies the ordering in the rest of the tree
      * so the structure remains valid
      *
-     * @param \Cake\ORM\Entity $entity The entity to set as a new root
+     * @param \Cake\Datasource\EntityInterface $entity The entity to set as a new root
      * @return void
      */
     protected function _setAsRoot($entity)
@@ -369,12 +369,12 @@ class TreeBehavior extends Behavior
     /**
      * Get the number of children nodes.
      *
-     * @param \Cake\ORM\Entity $node The entity to count children for
+     * @param \Cake\Datasource\EntityInterface $node The entity to count children for
      * @param bool $direct whether to count all nodes in the subtree or just
      * direct children
      * @return int Number of children nodes.
      */
-    public function childCount(Entity $node, $direct = false)
+    public function childCount(EntityInterface $node, $direct = false)
     {
         $config = $this->config();
         $parent = $this->_table->aliasField($config['parent']);
@@ -503,11 +503,11 @@ class TreeBehavior extends Behavior
      * Note that the node will not be deleted just moved away from its current position
      * without moving its children with it.
      *
-     * @param \Cake\ORM\Entity $node The node to remove from the tree
+     * @param \Cake\Datasource\EntityInterface $node The node to remove from the tree
      * @return \Cake\ORM\Entity|false the node after being removed from the tree or
      * false on error
      */
-    public function removeFromTree(Entity $node)
+    public function removeFromTree(EntityInterface $node)
     {
         return $this->_table->connection()->transactional(function () use ($node) {
             $this->_ensureFields($node);
@@ -518,7 +518,7 @@ class TreeBehavior extends Behavior
     /**
      * Helper function containing the actual code for removeFromTree
      *
-     * @param \Cake\ORM\Entity $node The node to remove from the tree
+     * @param \Cake\Datasource\EntityInterface $node The node to remove from the tree
      * @return \Cake\ORM\Entity|false the node after being removed from the tree or
      * false on error
      */
@@ -561,12 +561,12 @@ class TreeBehavior extends Behavior
      * If the node is the first child, or is a top level node with no previous node
      * this method will return false
      *
-     * @param \Cake\ORM\Entity $node The node to move
+     * @param \Cake\Datasource\EntityInterface $node The node to move
      * @param int|bool $number How many places to move the node, or true to move to first position
      * @throws \Cake\Datasource\Exception\RecordNotFoundException When node was not found
      * @return \Cake\ORM\Entity|bool $node The node after being moved or false on failure
      */
-    public function moveUp(Entity $node, $number = 1)
+    public function moveUp(EntityInterface $node, $number = 1)
     {
         if ($number < 1) {
             return false;
@@ -581,7 +581,7 @@ class TreeBehavior extends Behavior
     /**
      * Helper function used with the actual code for moveUp
      *
-     * @param \Cake\ORM\Entity $node The node to move
+     * @param \Cake\Datasource\EntityInterface $node The node to move
      * @param int|bool $number How many places to move the node, or true to move to first position
      * @throws \Cake\Datasource\Exception\RecordNotFoundException When node was not found
      * @return \Cake\ORM\Entity|bool $node The node after being moved or false on failure
@@ -642,12 +642,12 @@ class TreeBehavior extends Behavior
      * If the node is the last child, or is a top level node with no subsequent node
      * this method will return false
      *
-     * @param \Cake\ORM\Entity $node The node to move
+     * @param \Cake\Datasource\EntityInterface $node The node to move
      * @param int|bool $number How many places to move the node or true to move to last position
      * @throws \Cake\Datasource\Exception\RecordNotFoundException When node was not found
      * @return \Cake\ORM\Entity|bool the entity after being moved or false on failure
      */
-    public function moveDown(Entity $node, $number = 1)
+    public function moveDown(EntityInterface $node, $number = 1)
     {
         if ($number < 1) {
             return false;
@@ -662,7 +662,7 @@ class TreeBehavior extends Behavior
     /**
      * Helper function used with the actual code for moveDown
      *
-     * @param \Cake\ORM\Entity $node The node to move
+     * @param \Cake\Datasource\EntityInterface $node The node to move
      * @param int|bool $number How many places to move the node, or true to move to last position
      * @throws \Cake\Datasource\Exception\RecordNotFoundException When node was not found
      * @return \Cake\ORM\Entity|bool $node The node after being moved or false on failure
@@ -879,7 +879,7 @@ class TreeBehavior extends Behavior
      * Ensures that the provided entity contains non-empty values for the left and
      * right fields
      *
-     * @param \Cake\ORM\Entity $entity The entity to ensure fields for
+     * @param \Cake\Datasource\EntityInterface $entity The entity to ensure fields for
      * @return void
      */
     protected function _ensureFields($entity)

+ 6 - 6
src/ORM/Table.php

@@ -95,26 +95,26 @@ use RuntimeException;
  * - `buildRules(Event $event, RulesChecker $rules)`
  *   Allows listeners to modify the rules checker by adding more rules.
  *
- * - `beforeRules(Event $event, Entity $entity, ArrayObject $options, string $operation)`
+ * - `beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, string $operation)`
  *   Fired before an entity is validated using the rules checker. By stopping this event,
  *   you can return the final value of the rules checking operation.
  *
- * - `afterRules(Event $event, Entity $entity, ArrayObject $options, bool $result, string $operation)`
+ * - `afterRules(Event $event, EntityInterface $entity, ArrayObject $options, bool $result, string $operation)`
  *   Fired after the rules have been checked on the entity. By stopping this event,
  *   you can return the final value of the rules checking operation.
  *
- * - `beforeSave(Event $event, Entity $entity, ArrayObject $options)`
+ * - `beforeSave(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired before each entity is saved. Stopping this event will abort the save
  *   operation. When the event is stopped the result of the event will be returned.
  *
- * - `afterSave(Event $event, Entity $entity, ArrayObject $options)`
+ * - `afterSave(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired after an entity is saved.
  *
- * - `beforeDelete(Event $event, Entity $entity, ArrayObject $options)`
+ * - `beforeDelete(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired before an entity is deleted. By stopping this event you will abort
  *   the delete operation.
  *
- * - `afterDelete(Event $event, Entity $entity, ArrayObject $options)`
+ * - `afterDelete(Event $event, EntityInterface $entity, ArrayObject $options)`
  *   Fired after an entity has been deleted.
  *
  * @see \Cake\Event\EventManager for reference on the events system.

+ 4 - 3
tests/TestCase/ORM/Behavior/CounterCacheBehaviorTest.php

@@ -16,6 +16,7 @@ namespace Cake\Test\TestCase\ORM\Behavior;
 
 use Cake\Database\Query;
 use Cake\Datasource\ConnectionManager;
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior\CounterCacheBehavior;
 use Cake\ORM\Entity;
@@ -250,7 +251,7 @@ class CounterCacheBehaviorTest extends TestCase
 
         $this->post->addBehavior('CounterCache', [
             'Users' => [
-                'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable) use ($entity, $table) {
+                'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable) use ($entity, $table) {
                     $this->assertSame($orgTable, $table);
                     $this->assertSame($orgEntity, $entity);
 
@@ -281,7 +282,7 @@ class CounterCacheBehaviorTest extends TestCase
 
         $this->post->addBehavior('CounterCache', [
             'Users' => [
-                'posts_published' => function (Event $orgEvent, Entity $orgEntity, Table $orgTable, $original) use ($entity, $table) {
+                'posts_published' => function (Event $orgEvent, EntityInterface $orgEntity, Table $orgTable, $original) use ($entity, $table) {
                     $this->assertSame($orgTable, $table);
                     $this->assertSame($orgEntity, $entity);
 
@@ -317,7 +318,7 @@ class CounterCacheBehaviorTest extends TestCase
 
         $this->post->addBehavior('CounterCache', [
             'Users' => [
-                'posts_published' => function (Event $event, Entity $entity, Table $table) {
+                'posts_published' => function (Event $event, EntityInterface $entity, Table $table) {
                     $query = new Query($this->connection);
                     return $query->select(4);
                 }