Last active
August 31, 2018 13:45
-
-
Save kumirska/ffcf4f26db8b939838194ec2b8f4d6f1 to your computer and use it in GitHub Desktop.
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
<?php | |
namespace AppBundle\Helpers; | |
/** | |
* Example | |
* Add to property's phpdoc `* RegexPattern(pattern="/[^\\]+/s", message="Unsupported symbol")` | |
*/ | |
trait ObjectFieldsValidationTrait | |
{ | |
protected $_propertiesClass; | |
protected $_validatorsPropertiesClass; | |
protected $_filledPropertiesClass = false; | |
protected $_propertyClassInvalidFillDefaultMessage = 'Field is incorrect'; | |
protected $_propertiesClassInvalidMessages = []; | |
/** | |
* @return bool | |
*/ | |
final public function isClassPropertiesValidFilled() | |
{ | |
if (!$this->_filledPropertiesClass) { | |
$this->_fillPropertiesClass(); | |
} | |
$isValid = true; | |
foreach ($this->_validatorsPropertiesClass as $propName => $params) { | |
if (isset($params['pattern']) && array_key_exists($propName, $this->_propertiesClass)) { | |
$message = isset($params['message']) ? $params['message'] : $this->_propertyClassInvalidFillDefaultMessage; | |
if (preg_match($params['pattern'], $this->_propertiesClass[$propName])) { | |
$this->_propertiesClassInvalidMessages[$propName] = $message; | |
$isValid = false; | |
} | |
} | |
} | |
return $isValid; | |
} | |
/** | |
* @throws \ReflectionException | |
*/ | |
final protected function _fillPropertiesClass() | |
{ | |
$class = new \ReflectionObject($this); | |
foreach ($class->getProperties() as $property) { | |
$p = $class->getProperty($property->getName()); | |
$p->setAccessible(true); | |
$this->_propertiesClass[$p->getName()] = $p->getValue($this); | |
preg_match('/\n\s+?\*\s+?RegexPattern\((.+?)\)\n/is', $p->getDocComment(), $matched); | |
if (isset($matched[1])) { | |
$gatherParams = []; | |
$params = array_map('trim', explode(',', $matched[1])); | |
foreach ($params as $param) { | |
$param = array_map('trim', explode('=', $param)); | |
$gatherParams[$param[0]] = trim($param[1], '"'); | |
} | |
if ($gatherParams) { | |
$this->_validatorsPropertiesClass[$property->getName()] = $gatherParams; | |
} | |
} | |
} | |
$this->_filledPropertiesClass = true; | |
} | |
final public function setClassPropertyInvalidDefaultMessage($message) | |
{ | |
$this->_propertyClassInvalidFillDefaultMessage = $message; | |
} | |
/** | |
* @return array | |
*/ | |
final public function getPropertiesClassInvalidMessages() | |
{ | |
return $this->_propertiesClassInvalidMessages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Вынести парсер док-в в отдельный класс (абстрактный) и наследоваться
в дочернем классе передавать лишь идентификатор пхпдока аля "RegexPattern" и описать служебные методы (валидация, гет/сет)