Created
December 8, 2015 08:07
-
-
Save smichaelsen/0778705d6615b5ee4ab2 to your computer and use it in GitHub Desktop.
ValidationService for Extbase
This file contains hidden or 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
<?php | |
namespace Smichaelsen\Gist\Service; | |
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface; | |
use TYPO3\CMS\Extbase\Error\Result; | |
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator; | |
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface; | |
/** | |
* Provides stand alone validation for domain objects. | |
* | |
* Additionally it adds the possibility to define validators via object class phpdoc annotations | |
*/ | |
class ValidationService | |
{ | |
/** | |
* @var \TYPO3\CMS\Extbase\Reflection\ReflectionService | |
* @inject | |
*/ | |
protected $reflectionService; | |
/** | |
* @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver | |
* @inject | |
*/ | |
protected $validatorResolver; | |
/** | |
* @param DomainObjectInterface $object | |
* @return Result | |
*/ | |
public function validate(DomainObjectInterface $object) { | |
$validator = $this->validatorResolver->getBaseValidatorConjunction(get_class($object)); | |
$validator->addValidator($this->getCustomObjectValidator($object)); | |
return $validator->validate($object); | |
} | |
/** | |
* @param DomainObjectInterface $object | |
* @return ValidatorInterface | |
*/ | |
protected function getCustomObjectValidator(DomainObjectInterface $object) { | |
$conjunctionValidator = new ConjunctionValidator(); | |
foreach ($this->reflectionService->getClassTagValues(get_class($object), 'validate') as $validatorClassName) { | |
$conjunctionValidator->addValidator($this->validatorResolver->createValidator($validatorClassName)); | |
} | |
return $conjunctionValidator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment