eZ\Publish\Core\Repository\URLWildcardService::create PHP Method

create() public method

Creates a new url wildcard.
public create ( string $sourceUrl, string $destinationUrl, boolean $forward = false ) : eZ\Publish\API\Repository\Values\Content\UrlWildcard
$sourceUrl string
$destinationUrl string
$forward boolean
return eZ\Publish\API\Repository\Values\Content\UrlWildcard
    public function create($sourceUrl, $destinationUrl, $forward = false)
    {
        if ($this->repository->hasAccess('content', 'urltranslator') !== true) {
            throw new UnauthorizedException('content', 'urltranslator');
        }
        $sourceUrl = $this->cleanUrl($sourceUrl);
        $destinationUrl = $this->cleanUrl($destinationUrl);
        $spiUrlWildcards = $this->urlWildcardHandler->loadAll();
        foreach ($spiUrlWildcards as $wildcard) {
            if ($wildcard->sourceUrl === $sourceUrl) {
                throw new InvalidArgumentException('$sourceUrl', 'Pattern already exists');
            }
        }
        preg_match_all('(\\*)', $sourceUrl, $patterns);
        preg_match_all('(\\{(\\d+)\\})', $destinationUrl, $placeholders);
        $patterns = array_map('intval', $patterns[0]);
        $placeholders = array_map('intval', $placeholders[1]);
        if (!empty($placeholders) && max($placeholders) > count($patterns)) {
            throw new ContentValidationException('Placeholders are not matching with wildcards.');
        }
        $this->repository->beginTransaction();
        try {
            $spiUrlWildcard = $this->urlWildcardHandler->create($sourceUrl, $destinationUrl, $forward);
            $this->repository->commit();
        } catch (Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
        return $this->buildUrlWildcardDomainObject($spiUrlWildcard);
    }