eZ\Publish\Core\REST\Common\RequestParser\Pattern::generate PHP Method

generate() public method

Generate a URL of the given type from the specified values.
public generate ( string $type, array $values = [] ) : string
$type string
$values array
return string
    public function generate($type, array $values = array())
    {
        if (!isset($this->map[$type])) {
            throw new Exceptions\InvalidArgumentException("No URL for type '{$type}' available.");
        }
        $url = $this->map[$type];
        preg_match_all('(' . self::STANDARD_VARIABLE_REGEX . '|' . self::SLASHES_VARIABLE_REGEX . ')', $url, $matches, PREG_SET_ORDER);
        foreach ($matches as $matchSet) {
            $variableName = empty($matchSet[1]) ? $matchSet[2] : $matchSet[1];
            if (!isset($values[$variableName])) {
                throw new Exceptions\InvalidArgumentException("No value provided for '{$variableName}'.");
            }
            $url = str_replace($matchSet[0], $values[$variableName], $url);
            unset($values[$variableName]);
        }
        if (count($values)) {
            throw new Exceptions\InvalidArgumentException("Unused values in values array: '" . implode("', '", array_keys($values)) . "'.");
        }
        return $url;
    }

Usage Example

 /**
  * Test generating URL with extra value
  *
  * @expectedException \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
  * @expectedExceptionMessage Unused values in values array: 'bar'.
  */
 public function testGenerateSuperfluousValue()
 {
     $urlHandler = new Common\RequestParser\Pattern(array('pattern' => '/foo/{foo}'));
     $urlHandler->generate('pattern', array('foo' => 23, 'bar' => 42));
 }