Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: flow query directy field access without get 0 #3399

Draft
wants to merge 2 commits into
base: 9.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Neos.Eel/Classes/FlowQuery/FlowQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Neos\Eel\Exception;
use Neos\Eel\ProtectedContextAwareInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Utility\Exception\PropertyNotAccessibleException;
use Neos\Utility\ObjectAccess;

/**
* FlowQuery is jQuery for PHP, a selector and traversal engine for object sets.
Expand Down Expand Up @@ -179,6 +181,19 @@ public function __call($operationName, array $arguments)
}
}

public function __get($name): mixed
{
$value = $this->__call('get', [0]);
if (is_array($value) || is_object($value)) {
try {
return ObjectAccess::getProperty($value, $name);
} catch (PropertyNotAccessibleException $exception) {
return null;
}
}
return null;
}

/**
* Implementation of the countable() interface, which is mapped to the "count" operation.
*
Expand Down
14 changes: 14 additions & 0 deletions Neos.Eel/Tests/Unit/FlowQuery/FlowQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function constructWithFlowQueryIsIdempotent()
self::assertEquals($flowQuery->getContext(), $wrappedQuery->getContext());
}

/**
* @test
*/
public function accessPropertiesDirectly()
{
$myObject = new \stdClass();
$myObject->stringProperty = 'abc';
$myObject2 = new \stdClass();
$myObject2->stringProperty = 'def';

$query = $this->createFlowQuery([$myObject, $myObject2]);
self::assertSame('abc', $query->stringProperty);
}

/**
* @test
*/
Expand Down
4 changes: 4 additions & 0 deletions Neos.Utility.ObjectHandling/Classes/ObjectAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ protected static function getPropertyInternal($subject, string $propertyName, bo
return null;
}

if (method_exists($subject, '__get')) {
return $subject->__get($propertyName);
}

$cacheIdentifier = $className . '|' . $propertyName;
self::initializePropertyGetterCache($cacheIdentifier, $subject, $propertyName);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Neos\Utility\ObjectHandling\Tests\Unit\Fixture;

class DummyClassWithCall
{
public function __call($name, $arguments)
{
return sprintf('__call %s %s', $name, json_encode($arguments));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Neos\Utility\ObjectHandling\Tests\Unit\Fixture;

class DummyClassWithCallAndGet
{
public function __call($name, $arguments)
{
throw new \RuntimeException('should not be invoked');
}

public function __get($name)
{
return sprintf('__get %s', $name);
}
}
26 changes: 25 additions & 1 deletion Neos.Utility.ObjectHandling/Tests/Unit/ObjectAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
use Neos\Utility\Exception\PropertyNotAccessibleException;
use Neos\Utility\ObjectAccess;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\ArrayAccessClass;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\DummyClassWithCall;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\DummyClassWithCallAndGet;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\DummyClassWithGettersAndSetters;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\ProxiedClassWithPrivateProperty;
use Neos\Utility\ObjectHandling\Tests\Unit\Fixture\Model\EntityWithDoctrineProxy;
use Neos\Utility\TypeHandling;

require_once('Fixture/DummyClassWithGettersAndSetters.php');
require_once('Fixture/ArrayAccessClass.php');
require_once('Fixture/DummyClassWithCallAndGet.php');
require_once('Fixture/DummyClassWithCall.php');
require_once('Fixture/Model/EntityWithDoctrineProxy.php');
require_once('Fixture/ProxiedClassWithPrivateProperty.php');

Expand Down Expand Up @@ -274,6 +278,26 @@ public function getPropertyDoesNotTryArrayAccessOnSplObjectStorageSubject()
ObjectAccess::getProperty($splObjectStorage, 'something');
}

/**
* @test
*/
public function getPropertyInvokesMagicGetMethodIfExistent()
{
$dummyClassWithCallAndGet = new DummyClassWithCallAndGet();
$actualResult = ObjectAccess::getProperty($dummyClassWithCallAndGet, 'key');
self::assertEquals('__get key', $actualResult);
}

/**
* @test
*/
public function getPropertyInvokesMagicCallMethod()
{
$classWithCall = new DummyClassWithCall();
$actualResult = ObjectAccess::getProperty($classWithCall, 'key');
self::assertEquals('__call getKey []', $actualResult);
}

/**
* @test
*/
Expand Down Expand Up @@ -560,7 +584,7 @@ public function getPropertyUsingDirectAccessWorksOnPrivatePropertyOfProxyParent(
public function setPropertyUsingDirectAccessWorksOnPrivatePropertyOfProxyParent()
{
$proxyObject = new ProxiedClassWithPrivateProperty();

ObjectAccess::setProperty($proxyObject, 'property', 'changed', true);
self::assertEquals('changed', $proxyObject->getProperty());
}
Expand Down
Loading