Skip to content

Instantly share code, notes, and snippets.

@pumatertion
Last active August 29, 2015 14:02
Show Gist options
  • Save pumatertion/4ba96bd07df13a03acb6 to your computer and use it in GitHub Desktop.
Save pumatertion/4ba96bd07df13a03acb6 to your computer and use it in GitHub Desktop.
FilterExpiredOperation
<?php
/* *
* This script belongs to the TYPO3 Flow framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
namespace BLEICKER\Strodtbeck\FlowQuery\Operations;
use Doctrine\Common\Collections\ArrayCollection;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Eel\FlowQuery\Operations\AbstractOperation;
use TYPO3\Eel\FlowQuery\FlowQuery;
use TYPO3\TYPO3CR\Domain\Model\NodeInterface;
/**
* Class FilterExpiredOperation
*
* @package BLEICKER\Strodtbeck\FlowQuery\Operations
*/
class FilterExpiredOperation extends AbstractOperation {
/**
* {@inheritdoc}
*
* @var string
*/
static protected $shortName = 'filterExpired';
/**
* {@inheritdoc}
*
* @param FlowQuery $flowQuery the FlowQuery object
* @param array $arguments the context index to fetch from
* @return void
*/
public function evaluate(FlowQuery $flowQuery, array $arguments) {
$context = $flowQuery->getContext();
$contextCollection = new ArrayCollection($context);
$currentDateTime = new \DateTime();
$propertyPaths = $arguments;
$isValidFilter = function (NodeInterface $node) use ($currentDateTime, $propertyPaths) {
$isValid = TRUE;
foreach($propertyPaths as $propertyPath){
$propertyValue = $node->getProperty($propertyPath);
if($propertyValue instanceof \DateTime){
$isValid = $currentDateTime < $propertyValue;
if($isValid === FALSE){
break;
}
}
}
return $isValid;
};
$filteredContext = $contextCollection->filter($isValidFilter)->toArray();
$flowQuery->setContext($filteredContext);
}
}
@pumatertion
Copy link
Author

Example usage:
events = ${q(node).children().filterExpired('startDate','endDate')}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment