Skip to content
Permalink
Browse files

Enable EntityContext to read from ArrayAccess properties

Traverse into ArrayAccess properties as custom type mappings should be
readable if they quack like arrays.

Refs #10853
  • Loading branch information...
markstory committed Jul 5, 2017
1 parent 4bc6a54 commit ef239d0d84d52a58af3b4cdacc15f61826399ab1
Showing with 9 additions and 6 deletions.
  1. +2 −1 src/View/Form/EntityContext.php
  2. +7 −5 tests/TestCase/View/Form/EntityContextTest.php
@@ -19,6 +19,7 @@
use Cake\Http\ServerRequest;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use ArrayAccess;
use RuntimeException;
use Traversable;
@@ -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 ef239d0

Please sign in to comment.
You can’t perform that action at this time.