'isTypeIdentifier', self::isArray => 'isValidArray', self::isString => 'isValidString', self::isVersion => 'isValidVersion', self::isSemVer => 'isValidSemVer' ]; protected static function getValidConstraints(array $requirements, $validationGroup = []) { $validConstraints = []; foreach ($requirements as $field => $constraints) { if (!empty($validationGroup) && !in_array($field, $validationGroup)) { continue; } if (is_array($constraints)) { $validators = $constraints; } else { $validators = explode(',', $constraints); } foreach ($validators as $validator) { if (array_key_exists($validator, self::$customValidators)) { $validConstraints[$field][] = $validator; } else { $validConstraints[$field][] = \tao_helpers_form_FormFactory::getValidator($validator); } } } return $validConstraints; } /** * @param PortableElementObject $object * @param Validatable $validatable * @param array $validationGroup * @return bool * @throws PortableElementInconsistencyModelException * @throws PortableElementInvalidModelException * @throws \common_exception_Error */ public static function validate(PortableElementObject $object, Validatable $validatable, $validationGroup = []) { $constraints = self::getValidConstraints($validatable->getConstraints(), $validationGroup); $errorReport = \common_report_Report::createFailure('Portable element validation has failed.'); foreach ($constraints as $field => $constraint) { foreach ($constraint as $validator) { $getter = 'get' . ucfirst($field); if (! method_exists($object, $getter)) { throw new PortableElementInconsistencyModelException( 'Validator is not correctly set for model ' . get_class($object) ); } $value = $object->$getter(); if ($validator instanceof \tao_helpers_form_Validator) { if (! $validator->evaluate($value)) { $subReport = \common_report_Report::createFailure( __("Unable to validate %s: %s", $field, $validator->getMessage()) ); $errorReport->add($subReport); } continue; } if (is_string($validator)) { if (array_key_exists($validator, self::$customValidators)) { $callable = self::$customValidators[$validator]; try { self::$callable($value); } catch (PortableElementInvalidFieldException $e) { $subReport = \common_report_Report::createFailure( __("Unable to validate %s: %s", $field, $e->getMessage()) ); $errorReport->add($subReport); } } continue; } return false; } } if ($errorReport->containsError()) { $exception = new PortableElementInvalidModelException(); $exception->setReport($errorReport); throw $exception; } return true; } /** * @param $value * @return bool * @throws PortableElementInvalidFieldException */ public static function isValidString($value) { if (! is_string($value)) { throw new PortableElementInvalidFieldException('Unable to validate the given value as valid string.'); } return true; } /** * @param $value * @return bool * @throws PortableElementInvalidFieldException */ public static function isValidArray($value) { if (! is_array($value)) { throw new PortableElementInvalidFieldException('Unable to validate the given value as valid array.'); } return true; } /** * @param $value * @return bool * @throws PortableElementInvalidFieldException */ public static function isValidVersion($value) { $validator = \tao_helpers_form_FormFactory::getValidator(self::Regex, ['format' => '/\d+(?:\.\d+)+/']); if (! is_null($value) && ! $validator->evaluate($value)) { throw new PortableElementInvalidFieldException('Unable to validate the given value as valid version.'); } return true; } /** * @param $value * @return bool * @throws PortableElementInvalidFieldException */ public static function isValidSemVer($value) { try { Regex::matchSemVer($value); } catch (\InvalidArgumentException $exception) { throw new PortableElementInvalidFieldException('Unable to validate the given value as valid SemVer version.'); } return true; } /** * @param $value * @return bool * @throws PortableElementInvalidFieldException */ public static function isTypeIdentifier($value) { //the IMS PCI standard recommends using the URN https://tools.ietf.org/html/rfc4198 $validator = \tao_helpers_form_FormFactory::getValidator(self::Regex, ['format' => '/[a-z0-9-_:]+/i']); if (! is_null($value) && ! $validator->evaluate($value)) { throw new PortableElementInvalidFieldException('Unable to validate the given value as valid type identifier.'); } return true; } }