| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2016 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\Google\ApiCore; |
| 34 |
|
| 35 |
use Dudlewebs\WPMCS\Google\ApiCore\ResourceTemplate\AbsoluteResourceTemplate; |
| 36 |
use Dudlewebs\WPMCS\Google\ApiCore\ResourceTemplate\RelativeResourceTemplate; |
| 37 |
use Dudlewebs\WPMCS\Google\ApiCore\ResourceTemplate\ResourceTemplateInterface; |
| 38 |
/** |
| 39 |
* Represents a path template. |
| 40 |
* |
| 41 |
* Templates use the syntax of the API platform; see the protobuf of HttpRule for |
| 42 |
* details. A template consists of a sequence of literals, wildcards, and variable bindings, |
| 43 |
* where each binding can have a sub-path. A string representation can be parsed into an |
| 44 |
* instance of PathTemplate, which can then be used to perform matching and instantiation. |
| 45 |
*/ |
| 46 |
class PathTemplate implements ResourceTemplateInterface |
| 47 |
{ |
| 48 |
private $resourceTemplate; |
| 49 |
/** |
| 50 |
* PathTemplate constructor. |
| 51 |
* |
| 52 |
* @param string $path A path template string |
| 53 |
* @throws ValidationException When $path cannot be parsed into a valid PathTemplate |
| 54 |
*/ |
| 55 |
public function __construct(string $path = null) |
| 56 |
{ |
| 57 |
if (empty($path)) { |
| 58 |
throw new ValidationException('Cannot construct PathTemplate from empty string'); |
| 59 |
} |
| 60 |
if ($path[0] === '/') { |
| 61 |
$this->resourceTemplate = new AbsoluteResourceTemplate($path); |
| 62 |
} else { |
| 63 |
$this->resourceTemplate = new RelativeResourceTemplate($path); |
| 64 |
} |
| 65 |
} |
| 66 |
/** |
| 67 |
* @return string A string representation of the path template |
| 68 |
*/ |
| 69 |
public function __toString() |
| 70 |
{ |
| 71 |
return $this->resourceTemplate->__toString(); |
| 72 |
} |
| 73 |
/** |
| 74 |
* Renders a path template using the provided bindings. |
| 75 |
* |
| 76 |
* @param array $bindings An array matching var names to binding strings. |
| 77 |
* @throws ValidationException if a key isn't provided or if a sub-template |
| 78 |
* can't be parsed. |
| 79 |
* @return string A rendered representation of this path template. |
| 80 |
*/ |
| 81 |
public function render(array $bindings) |
| 82 |
{ |
| 83 |
return $this->resourceTemplate->render($bindings); |
| 84 |
} |
| 85 |
/** |
| 86 |
* Check if $path matches a resource string. |
| 87 |
* |
| 88 |
* @param string $path A resource string. |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
public function matches(string $path) |
| 92 |
{ |
| 93 |
return $this->resourceTemplate->matches($path); |
| 94 |
} |
| 95 |
/** |
| 96 |
* Matches a fully qualified path template string. |
| 97 |
* |
| 98 |
* @param string $path A fully qualified path template string. |
| 99 |
* @throws ValidationException if path can't be matched to the template. |
| 100 |
* @return array Array matching var names to binding values. |
| 101 |
*/ |
| 102 |
public function match(string $path) |
| 103 |
{ |
| 104 |
return $this->resourceTemplate->match($path); |
| 105 |
} |
| 106 |
} |
| 107 |
|