/** * Populate $resource with values for parameters witch the implementations is interested in. * * @param ResourceInterface $resource * @return mixed */ public function populate(ResourceInterface $resource) { if (!count($this)) { return; } $self = $this; $url = preg_replace_callback(self::PARAM_REGEXP, function ($matches) use($self) { $name = $matches['name']; return $self->has($name) ? $self->get($name)->getValue() : null; }, $resource->getUrl()); // update resource with $url values $resource->setUrl($url); }
/** * Populate $resource with values for parameters witch the implementations is interested in. * * @param ResourceInterface $resource * @return mixed */ public function populate(ResourceInterface $resource) { if (!count($this)) { return; } $url = $resource->getUrl(); $query = parse_url($url, PHP_URL_QUERY); if (!$query) { return; } $url = str_replace($query, null, $url); $query = array(); foreach ($this as $param) { $query[$param->getName()] = $param->getValue(); } $url = trim($url); $url = rtrim($url, '?') . '?'; $url .= http_build_query($query); // update resource with $url values $resource->setUrl($url); }
protected function parseDefinition($definition, ResourceInterface $resource) { $definition = trim($definition); if (false === strpos($definition, ':')) { $message = 'Definition must contains HTTP Method & URL separator ":"'; throw new Exception\InvalidArgumentException($message); } list($method, $url) = (array) explode(':', $definition, 2); $method = trim($method); $method = strtoupper($method); if (!isset($this->availableMethods[$method])) { $message = 'HTTP Method is not supported %s. Method must be one of those: %s'; $message = sprintf($message, $method, implode(array_keys($this->availableMethods))); throw new Exception\InvalidArgumentException($message); } $url = trim($url); $url = ltrim($url, '/'); $url = '/' . $url; $parts = parse_url($url); if (!array_key_exists('path', $parts)) { $message = 'URL path must be defined. Given URL %s'; $message = sprintf($message, $url); throw new Exception\InvalidArgumentException($message); } $resource->setMethod($method); $resource->setUrl($url); }
public function toArrayResource(ResourceInterface $resource) { return array('method' => $resource->getMethod(), 'url' => $resource->getUrl(), 'description' => $resource->getDescription(), 'urlParams' => $this->toArrayParamSet($resource->getUrlParams()), 'queryParams' => $this->toArrayParamSet($resource->getQueryParams())); }