PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / trunk
ShareThis Dashboard for Google Analytics vtrunk
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / google / gax / src / ResourceTemplate / AbsoluteResourceTemplate.php
googleanalytics / lib / analytics-admin / vendor / google / gax / src / ResourceTemplate Last commit date
AbsoluteResourceTemplate.php 3 years ago Parser.php 3 years ago RelativeResourceTemplate.php 3 years ago ResourceTemplateInterface.php 3 years ago Segment.php 3 years ago
AbsoluteResourceTemplate.php
149 lines
1 <?php
2 /*
3 * Copyright 2018 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
33 namespace Google\ApiCore\ResourceTemplate;
34
35 use Google\ApiCore\ValidationException;
36
37 /**
38 * Represents an absolute resource template, meaning that it will always contain a leading slash,
39 * and may contain a trailing verb (":<verb>").
40 *
41 * Examples:
42 * /projects
43 * /projects/{project}
44 * /foo/{bar=**}/fizz/*:action
45 *
46 * Templates use the syntax of the API platform; see
47 * https://github.com/googleapis/api-common-protos/blob/master/google/api/http.proto
48 * for details. A template consists of a sequence of literals, wildcards, and variable bindings,
49 * where each binding can have a sub-path. A string representation can be parsed into an
50 * instance of AbsoluteResourceTemplate, which can then be used to perform matching and instantiation.
51 *
52 * @internal
53 */
54 class AbsoluteResourceTemplate implements ResourceTemplateInterface
55 {
56 /** @var RelativeResourceTemplate */
57 private $resourceTemplate;
58
59 /** @var string */
60 private $verb;
61
62 /**
63 * AbsoluteResourceTemplate constructor.
64 * @param string $path
65 * @throws ValidationException
66 */
67 public function __construct(string $path)
68 {
69 if (empty($path)) {
70 throw new ValidationException('Cannot construct AbsoluteResourceTemplate from empty string');
71 }
72 if ($path[0] !== '/') {
73 throw new ValidationException(
74 "Could not construct AbsoluteResourceTemplate from '$path': must begin with '/'"
75 );
76 }
77 $verbSeparatorPos = $this->verbSeparatorPos($path);
78 $this->resourceTemplate = new RelativeResourceTemplate(substr($path, 1, $verbSeparatorPos - 1));
79 $this->verb = substr($path, $verbSeparatorPos + 1);
80 }
81
82 /**
83 * @inheritdoc
84 */
85 public function __toString()
86 {
87 return sprintf("/%s%s", $this->resourceTemplate, $this->renderVerb());
88 }
89
90 /**
91 * @inheritdoc
92 */
93 public function render(array $bindings)
94 {
95 return sprintf("/%s%s", $this->resourceTemplate->render($bindings), $this->renderVerb());
96 }
97
98 /**
99 * @inheritdoc
100 */
101 public function matches(string $path)
102 {
103 try {
104 $this->match($path);
105 return true;
106 } catch (ValidationException $ex) {
107 return false;
108 }
109 }
110
111 /**
112 * @inheritdoc
113 */
114 public function match(string $path)
115 {
116 if (empty($path)) {
117 throw $this->matchException($path, "path cannot be empty");
118 }
119 if ($path[0] !== '/') {
120 throw $this->matchException($path, "missing leading '/'");
121 }
122 $verbSeparatorPos = $this->verbSeparatorPos($path);
123 if (substr($path, $verbSeparatorPos + 1) !== $this->verb) {
124 throw $this->matchException($path, "trailing verb did not match '{$this->verb}'");
125 }
126 return $this->resourceTemplate->match(substr($path, 1, $verbSeparatorPos - 1));
127 }
128
129 private function matchException(string $path, string $reason)
130 {
131 return new ValidationException("Could not match path '$path' to template '$this': $reason");
132 }
133
134 private function renderVerb()
135 {
136 return $this->verb ? ':' . $this->verb : '';
137 }
138
139 private function verbSeparatorPos(string $path)
140 {
141 $finalSeparatorPos = strrpos($path, '/');
142 $verbSeparatorPos = strrpos($path, ':', $finalSeparatorPos);
143 if ($verbSeparatorPos === false) {
144 $verbSeparatorPos = strlen($path);
145 }
146 return $verbSeparatorPos;
147 }
148 }
149