/** * prepares a request for execution **/ protected function build() { $method = $this->get('method'); // ensure the appropriate method is being used if (null === constant('\\Guzzle\\Http\\Message\\RequestInterface::' . strtoupper($method))) { throw new \InvalidArgumentException('You must use a valid REST method.'); } //end if $url = $this->get('service'); // convert passed arguments into a collection if ($args = $this->get('args')) { $args = new \Guzzle\Common\Collection((array) $args); $url = \Guzzle\Guzzle::inject($url, $args); } //end if // set up the request object $this->request = $this->client->{$method}($url); // assign query data if passed if ($query = $this->get('query')) { $this->request->getQuery()->merge($query); } //end if }
/** * 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 (optional) Set to FALSE to allow missing required fields * @param bool $validate (optional) 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 (is_string($configValue)) { $configValue = Guzzle::inject($configValue, $config); } // 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[] = $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.'; } } $config->set($name, $configValue); } if (empty($errors)) { return true; } else { if ($strict) { throw new ValidationException('Validation errors: ' . implode("\n", $errors)); } } return $errors; }
/** * @covers Guzzle\Guzzle::inject * @dataProvider dataProvider */ public function testInjectsConfigData($output, $input, $config) { $this->assertEquals($output, Guzzle::inject($input, new Collection($config))); }