Permalink
Browse files

Merge pull request #10859 from cakephp/issue-10853

Enable EntityContext to read from ArrayAccess properties
  • Loading branch information...
markstory committed Jul 7, 2017
2 parents 8de9cc2 + 53bde12 commit 4ffc76f575ab0043379fa94beadef49153ac8e8c
Showing with 9 additions and 6 deletions.
  1. +2 −1 src/View/Form/EntityContext.php
  2. +7 −5 tests/TestCase/View/Form/EntityContextTest.php
@@ -14,6 +14,7 @@
*/
namespace Cake\View\Form;
use ArrayAccess;
use Cake\Collection\Collection;
use Cake\Datasource\EntityInterface;
use Cake\Http\ServerRequest;
@@ -259,7 +260,7 @@ public function val($field, $options = [])
return $this->_schemaDefault($part, $entity);
}
if (is_array($entity)) {
if (is_array($entity) || $entity instanceof ArrayAccess) {
$key = array_pop($parts);
return isset($entity[$key]) ? $entity[$key] : null;
@@ -461,7 +461,8 @@ public function testValGetArrayValue()
'name' => 'Test tag',
],
'author' => new Entity([
'roles' => ['admin', 'publisher']
'roles' => ['admin', 'publisher'],
'aliases' => new ArrayObject(['dave', 'david']),
])
]);
$context = new EntityContext($this->request, [
@@ -477,11 +478,12 @@ public function testValGetArrayValue()
$result = $context->val('tag.name');
$this->assertEquals($row->tag['name'], $result);
$result = $context->val('tag.nope');
$this->assertNull($result);
$result = $context->val('author.aliases.0');
$this->assertEquals($row->author->aliases[0], $result, 'ArrayAccess can be read');
$result = $context->val('author.roles.3');
$this->assertNull($result);
$this->assertNull($context->val('author.aliases.3'));
$this->assertNull($context->val('tag.nope'));
$this->assertNull($context->val('author.roles.3'));
}
/**

0 comments on commit 4ffc76f

Please sign in to comment.