/** * {@inheritdoc} * @throws ValidationException when validation errors occur */ public function validate(Collection $config, Inspector $inspector = null) { $inspector = $inspector ?: Inspector::getInstance(); $typeValidation = $inspector->getTypeValidation(); $errors = array(); foreach ($this->params as $name => $arg) { $currentValue = $config->get($name); $configValue = $arg->getValue($currentValue); // Inject configuration information into the config value if ($configValue && is_string($configValue)) { $configValue = $config->inject($configValue); } // Ensure that required arguments are set if ($arg->getRequired() && ($configValue === null || $configValue === '')) { $errors[] = 'Requires that the ' . $name . ' argument be supplied.' . ($arg->getDoc() ? ' (' . $arg->getDoc() . ').' : ''); continue; } // Ensure that the correct data type is being used if ($typeValidation && $configValue !== null && ($argType = $arg->getType())) { $validation = $inspector->validateConstraint($argType, $configValue, $arg->getTypeArgs()); if ($validation !== true) { $errors[] = $name . ': ' . $validation; $config->set($name, $configValue); continue; } } $configValue = $arg->filter($configValue); // Update the config value if it changed if (!$configValue !== $currentValue) { $config->set($name, $configValue); } // Check the length values if validating data $argMinLength = $arg->getMinLength(); if ($argMinLength && strlen($configValue) < $argMinLength) { $errors[] = 'Requires that the ' . $name . ' argument be >= ' . $arg->getMinLength() . ' characters.'; } $argMaxLength = $arg->getMaxLength(); if ($argMaxLength && strlen($configValue) > $argMaxLength) { $errors[] = 'Requires that the ' . $name . ' argument be <= ' . $arg->getMaxLength() . ' characters.'; } } if (!empty($errors)) { $e = new ValidationException('Validation errors: ' . implode("\n", $errors)); $e->setErrors($errors); throw $e; } }
/** * @dataProvider dataProvider */ public function testInjectsConfigData($output, $input, $config) { $collection = new Collection($config); $this->assertEquals($output, $collection->inject($input)); }
/** * Validates that all required args are included in a config object, * and if not, throws an InvalidArgumentException with a helpful error message. Adds * default args to the passed config object if the parameter was not * set in the config object. * * @param array $params Params to validate * @param Collection $config Configuration settings * @param bool $strict Set to FALSE to allow missing required fields * @param bool $validate Set to TRUE or FALSE to validate data. * Set to false when you only need to add * default values and statics. * * @return array|bool Returns an array of errors or TRUE on success * * @throws InvalidArgumentException if any args are missing and $strict is TRUE */ public function validateConfig(array $params, Collection $config, $strict = true, $validate = true) { $errors = array(); foreach ($params as $name => $arg) { // Set the default or static value if it is not set $configValue = $arg->getValue($config->get($name)); // Inject configuration information into the config value if ($configValue && is_string($configValue)) { $configValue = $config->inject($configValue); } // Ensure that required arguments are set if ($validate && $arg->getRequired() && ($configValue === null || $configValue === '')) { $errors[] = 'Requires that the ' . $name . ' argument be supplied.' . ($arg->getDoc() ? ' (' . $arg->getDoc() . ').' : ''); continue; } // Ensure that the correct data type is being used if ($validate && $this->typeValidation && $configValue !== null && ($argType = $arg->getType())) { $validation = $this->validateConstraint($argType, $configValue); if ($validation !== true) { $errors[] = $name . ': ' . $validation; continue; } } // Run the value through attached filters $configValue = $arg->filter($configValue); $config->set($name, $configValue); // Check the length values if validating data if ($validate) { $argMinLength = $arg->getMinLength(); if ($argMinLength && strlen($configValue) < $argMinLength) { $errors[] = 'Requires that the ' . $name . ' argument be >= ' . $arg->getMinLength() . ' characters.'; } $argMaxLength = $arg->getMaxLength(); if ($argMaxLength && strlen($configValue) > $argMaxLength) { $errors[] = 'Requires that the ' . $name . ' argument be <= ' . $arg->getMaxLength() . ' characters.'; } } } if (empty($errors)) { return true; } elseif ($strict) { throw new ValidationException('Validation errors: ' . implode("\n", $errors)); } return $errors; }