|
<?php
|
|
|
|
namespace Portrino\PxLib\Domain\Validator;
|
|
|
|
/***************************************************************
|
|
* Copyright notice
|
|
*
|
|
* (c) 2017 Andre Wuttig <wuttig@portrino.de>, portrino GmbH
|
|
*
|
|
* All rights reserved
|
|
*
|
|
* This script is part of the TYPO3 project. The TYPO3 project is
|
|
* free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* The GNU General Public License can be found at
|
|
* http://www.gnu.org/copyleft/gpl.html.
|
|
*
|
|
* This script is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* This copyright notice MUST APPEAR in all copies of the script!
|
|
***************************************************************/
|
|
use DateTime;
|
|
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
|
|
use TYPO3\CMS\Extbase\Validation\Error;
|
|
use TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException;
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
|
|
|
|
/**
|
|
* Class PropertyCompareValidator
|
|
*
|
|
* @package Portrino\PxLib\Domain\Validator
|
|
*/
|
|
class PropertyCompareValidator extends AbstractValidator
|
|
{
|
|
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $supportedOptions = [
|
|
'firstProperty' => ['', 'The first property which should be compared with the second', 'string'],
|
|
'secondProperty' => ['', 'The second property which should be compared with the first', 'string'],
|
|
'operator' => ['', 'The second property which should be compared with the first', 'string']
|
|
];
|
|
/**
|
|
* @var array
|
|
*/
|
|
protected $validOperatorMethods = [
|
|
'equal',
|
|
'totallyEqual',
|
|
'notEqual',
|
|
'greaterThan',
|
|
'greaterThanOrEqual',
|
|
'lessThan',
|
|
'lessThanOrEqual'
|
|
];
|
|
|
|
/**
|
|
* Checks if the given first property value is equal to the second property value
|
|
*
|
|
* @param DomainObjectInterface $object
|
|
*
|
|
* @return boolean TRUE, if the firstProperty is equal to the second, FALSE if not
|
|
* @throws InvalidValidationOptionsException
|
|
*/
|
|
public function isValid($object)
|
|
{
|
|
$result = true;
|
|
$firstPropertyName = $this->options['firstProperty'];
|
|
$secondPropertyName = $this->options['secondProperty'];
|
|
$operator = $this->options['operator'];
|
|
|
|
if (empty($object) || $object === null) {
|
|
throw new InvalidValidationOptionsException(
|
|
'No object given.',
|
|
1492089750738
|
|
);
|
|
}
|
|
if (!($object instanceof DomainObjectInterface)) {
|
|
throw new InvalidValidationOptionsException(
|
|
'The domainObject is not instance of "\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface".',
|
|
1492089748475
|
|
);
|
|
}
|
|
if (!property_exists($object, $firstPropertyName)) {
|
|
throw new InvalidValidationOptionsException(
|
|
'The first property: "' . $firstPropertyName . '" doest not exist in domainObject of type: "' . get_class($object) . '".',
|
|
1492089745186
|
|
);
|
|
}
|
|
if (!property_exists($object, $secondPropertyName)) {
|
|
throw new InvalidValidationOptionsException(
|
|
'The second property: "' . $secondPropertyName . '" doest not exist in domainObject of type: "' . get_class($object) . '".',
|
|
1492089742841
|
|
);
|
|
}
|
|
if (!in_array($operator, $this->validOperatorMethods)) {
|
|
throw new InvalidValidationOptionsException(
|
|
'The operator: "' . $operator . '" doest not exist',
|
|
1492090573991
|
|
);
|
|
}
|
|
|
|
$firstValue = ObjectAccess::getProperty($object, $firstPropertyName);
|
|
$secondValue = ObjectAccess::getProperty($object, $secondPropertyName);
|
|
|
|
if ($firstValue instanceof DateTime && $secondValue instanceof DateTime) {
|
|
$firstValue = $firstValue->getTimestamp();
|
|
$secondValue = $secondValue->getTimestamp();
|
|
}
|
|
|
|
if ($firstValue instanceof DomainObjectInterface && $secondValue instanceof DomainObjectInterface) {
|
|
$firstValue = $firstValue->getUid();
|
|
$secondValue = $secondValue->getUid();
|
|
}
|
|
|
|
$result = $this->$operator($firstValue, $secondValue);
|
|
|
|
if ($result === false) {
|
|
$error = new Error($firstPropertyName . ' not ' . $operator . ' ' . $secondPropertyName,
|
|
1492089850097);
|
|
$this->result->forProperty($firstPropertyName)->addError($error);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function equal($firstValue, $secondValue)
|
|
{
|
|
return $firstValue == $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function totallyEqual($firstValue, $secondValue)
|
|
{
|
|
return $firstValue === $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function notEqual($firstValue, $secondValue)
|
|
{
|
|
return $firstValue != $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function greaterThan($firstValue, $secondValue)
|
|
{
|
|
return $firstValue > $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function lessThan($firstValue, $secondValue)
|
|
{
|
|
return $firstValue < $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function greaterThanOrEqual($firstValue, $secondValue)
|
|
{
|
|
return $firstValue >= $secondValue;
|
|
}
|
|
|
|
/**
|
|
* @param mixed $firstValue
|
|
* @param mixed $secondValue
|
|
* @return bool
|
|
*/
|
|
private function lessThanOrEqual($firstValue, $secondValue)
|
|
{
|
|
return $firstValue <= $secondValue;
|
|
}
|
|
}
|