Path.php 4.09 KiB
<?php declare(strict_types=1);
/*
 * This file is part of "irstea/ng-model-generator-bundle".
 * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
 * Copyright (C) 2018 IRSTEA
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
namespace Irstea\NgModelGeneratorBundle\Models\Types\Operations;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
 * Class Path.
final class Path
    /**
     * @var PathPart[]
    private $parts;
    /**
     * Path constructor.
     * @param PathPart[] $parts
    public function __construct(array $parts)
        $this->parts = $parts;
    /**
     * @return string
    public function getUsage(): string
        return TypescriptHelper::quoteString(
            $this->getTemplate(),
            $this->hasParameters() ? '`' : "'"
    /**
     * @return string
    public function getTemplate(): string
        return implode(
            '',
            array_map(
                function (PathPart $part): string {
                    return $part->asTemplate();
                $this->parts
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
} /** * @return string */ public function getTestPattern(): string { return implode( '', array_map( function (PathPart $part): string { return $part->asTestPattern(); }, $this->parts ) ); } /** * @return string */ public function getCapturePattern(): string { return implode( '', array_map( function (PathPart $part): string { return $part->asCapturePattern(); }, $this->parts ) ); } /** * @return bool */ public function hasParameters(): bool { foreach ($this->parts as $part) { if ($part->getParameter() !== null) { return true; } } return false; } /** * @return Parameter[] */ public function getParameters(): array { return \array_filter( array_map( function (PathPart $part): ?Parameter { return $part->getParameter(); }, $this->parts ) ); } /** * @param Path $other * @param Parameter $otherParameter * * @return Path */ public function buildUsing(Path $other, Parameter $otherParameter): Path
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
{ if (\count($other->parts) > \count($this->parts)) { return $this; } foreach ($other->parts as $index => $part) { if (!$this->parts[$index]->isEqual($part)) { return $this; } } if (\count($other->parts) === \count($this->parts)) { return $other; } return new Path( \array_merge( [new ParameterPathPart($otherParameter, $other->getTestPattern())], \array_slice($this->parts, \count($other->parts)) ) ); } /** * @return \Generator */ public function getIterator() { yield from $this->parts; } /** * @return array */ public function jsonSerialize() { return \get_object_vars($this); } }