Permalink
Browse files

Use Collection class to simplify entity handling.

Mucking about with the various iterator classes isn't necesary when we
use the Collection clas to pre-process it.
  • Loading branch information...
1 parent 9d3b5e6 commit 1ce283a9a41fc69992a91221696be9e226d1ebe0 @markstory markstory committed Feb 9, 2014
Showing with 5 additions and 11 deletions.
  1. +3 −10 src/View/Form/EntityContext.php
  2. +2 −1 tests/TestCase/View/Form/EntityContextTest.php
@@ -14,13 +14,12 @@
*/
namespace Cake\View\Form;
+use Cake\Collection\Collection;
use Cake\Network\Request;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Validation\Validator;
-use IteratorAggregate;
-use IteratorIterator;
use Traversable;
/**
@@ -109,14 +108,8 @@ protected function _prepare() {
$table = $this->_context['table'];
if (empty($table)) {
$entity = $this->_context['entity'];
- if ($entity instanceof IteratorAggregate) {
- $entity = $entity->getIterator()->current();
- } elseif ($entity instanceof IteratorIterator) {
- $entity = $entity->getInnerIterator()->current();
- } elseif ($entity instanceof Traversable) {
- $entity = $entity->current();
- } elseif (is_array($entity)) {
- $entity = current($entity);
+ if (is_array($entity) || $entity instanceof Traversable) {
+ $entity = (new Collection($entity))->first();
}
if ($entity instanceof Entity) {
list($ns, $entityClass) = namespaceSplit(get_class($entity));
@@ -83,7 +83,8 @@ public function testOperationsNoEntity() {
$this->assertEquals([], $context->error('title'));
$this->assertEquals(
['length' => null, 'precision' => null],
- $context->attributes('title'));
+ $context->attributes('title')
+ );
}
/**

0 comments on commit 1ce283a

Please sign in to comment.