-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5015 from mhsdesign/task/4208-remove-magic-unders…
…core-access-for-internal-node-properties !!! TASK: Remove leftovers from magic underscore access for internal node properties
- Loading branch information
Showing
9 changed files
with
246 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
Neos.ContentRepository.NodeAccess/Classes/FlowQueryOperations/SortByTimestampOperation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Neos.Neos package. | ||
* | ||
* (c) Contributors of the Neos Project - www.neos.io | ||
* | ||
* This package is Open Source Software. For the full copyright and license | ||
* information, please view the LICENSE file which was distributed with this | ||
* source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\NodeAccess\FlowQueryOperations; | ||
|
||
use Neos\ContentRepository\Core\Projection\ContentGraph\Node; | ||
use Neos\ContentRepository\Core\Projection\ContentGraph\Timestamps; | ||
use Neos\Eel\FlowQuery\FlowQuery; | ||
use Neos\Eel\FlowQuery\FlowQueryException; | ||
use Neos\Eel\FlowQuery\Operations\AbstractOperation; | ||
|
||
/** | ||
* "sortByTimestamp" operation working on ContentRepository nodes. | ||
* Sorts nodes by specified timestamp. | ||
*/ | ||
class SortByTimestampOperation extends AbstractOperation | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @var string | ||
*/ | ||
protected static $shortName = 'sortByTimestamp'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* We can only handle ContentRepository Nodes. | ||
* | ||
* @param mixed $context | ||
* @return boolean | ||
*/ | ||
public function canEvaluate($context) | ||
{ | ||
return count($context) === 0 || (is_array($context) === true && (current($context) instanceof Node)); | ||
} | ||
|
||
/** | ||
* First argument is the timestamp to sort by like created, lastModified, originalCreated and originalLastModified | ||
* Second argument is the sort direction (ASC or DESC). | ||
* | ||
* sortByTimestamp("created", "ASC") | ||
* sortByTimestamp("lastModified", "DESC") | ||
* | ||
* @see Timestamps for further documentation | ||
* | ||
* @param FlowQuery $flowQuery the FlowQuery object | ||
* @param array<int,mixed> $arguments the arguments for this operation. | ||
* @return void | ||
* @throws FlowQueryException | ||
*/ | ||
public function evaluate(FlowQuery $flowQuery, array $arguments) | ||
{ | ||
/** @var array|Node[] $nodes */ | ||
$nodes = $flowQuery->getContext(); | ||
|
||
$sortedNodes = []; | ||
$sortSequence = []; | ||
$nodesByIdentifier = []; | ||
|
||
// Determine the property value to sort by | ||
foreach ($nodes as $node) { | ||
$timeStamp = match($arguments[0] ?? null) { | ||
'created' => $node->timestamps->created->getTimestamp(), | ||
'lastModified' => $node->timestamps->lastModified?->getTimestamp(), | ||
'originalCreated' => $node->timestamps->originalCreated->getTimestamp(), | ||
'originalLastModified' => $node->timestamps->originalLastModified?->getTimestamp(), | ||
default => throw new FlowQueryException('Please provide a timestamp (created, lastModified, originalLastModified) to sort by.', 1727367726) | ||
}; | ||
|
||
$sortSequence[$node->aggregateId->value] = $timeStamp; | ||
$nodesByIdentifier[$node->aggregateId->value] = $node; | ||
} | ||
|
||
$sortOrder = is_string($arguments[1] ?? null) ? strtoupper($arguments[1]) : null; | ||
if ($sortOrder === 'DESC') { | ||
arsort($sortSequence); | ||
} elseif ($sortOrder === 'ASC') { | ||
asort($sortSequence); | ||
} else { | ||
throw new FlowQueryException('Please provide a valid sort direction (ASC or DESC)', 1727367837); | ||
} | ||
|
||
// Build the sorted context that is returned | ||
foreach ($sortSequence as $nodeIdentifier => $value) { | ||
$sortedNodes[] = $nodesByIdentifier[$nodeIdentifier]; | ||
} | ||
|
||
$flowQuery->setContext($sortedNodes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...tentRepository.NodeAccess/Tests/Unit/FlowQueryOperations/SortByTimestampOperationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\NodeAccess\Tests\Unit\FlowQueryOperations; | ||
|
||
use Neos\ContentRepository\NodeAccess\FlowQueryOperations\SortByTimestampOperation; | ||
use Neos\Eel\FlowQuery\FlowQueryException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* SortOperation test | ||
*/ | ||
class SortByTimestampOperationTest extends TestCase | ||
{ | ||
/** | ||
* @test+ | ||
*/ | ||
public function callWithoutArgumentsCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortByTimestampOperation(); | ||
$operation->evaluate($flowQuery, []); | ||
} | ||
|
||
/** | ||
* @test+ | ||
*/ | ||
public function callWithoutWrongTimeStampArgumentsCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortByTimestampOperation(); | ||
$operation->evaluate($flowQuery, ['erstellt']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function invalidSortDirectionCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortByTimestampOperation(); | ||
$operation->evaluate($flowQuery, ['created', 'FOO']); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Neos.ContentRepository.NodeAccess/Tests/Unit/FlowQueryOperations/SortOperationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neos\ContentRepository\NodeAccess\Tests\Unit\FlowQueryOperations; | ||
|
||
use Neos\ContentRepository\NodeAccess\FlowQueryOperations\SortOperation; | ||
use Neos\Eel\FlowQuery\FlowQueryException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* SortOperation test | ||
*/ | ||
class SortOperationTest extends TestCase | ||
{ | ||
/** | ||
* @test+ | ||
*/ | ||
public function callWithoutArgumentsCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortOperation(); | ||
$operation->evaluate($flowQuery, []); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function invalidSortDirectionCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortOperation(); | ||
$operation->evaluate($flowQuery, ['title', 'FOO']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function invalidSortOptionCausesException() | ||
{ | ||
$this->expectException(FlowQueryException::class); | ||
$flowQuery = new \Neos\Eel\FlowQuery\FlowQuery([]); | ||
$operation = new SortOperation(); | ||
$operation->evaluate($flowQuery, ['title', 'ASC', 'SORT_BAR']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.