本文整理汇总了PHP中Guzzle\Common\Collection::inject方法的典型用法代码示例。如果您正苦于以下问题:PHP Collection::inject方法的具体用法?PHP Collection::inject怎么用?PHP Collection::inject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Common\Collection
的用法示例。
在下文中一共展示了Collection::inject方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
/**
* {@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;
}
}
示例2: testInjectsConfigData
/**
* @dataProvider dataProvider
*/
public function testInjectsConfigData($output, $input, $config)
{
$collection = new Collection($config);
$this->assertEquals($output, $collection->inject($input));
}
示例3: validateConfig
/**
* 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;
}