PathTemplate.php 4.1 KB

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