googleanalytics
/
lib
/
analytics-admin
/
vendor
/
google
/
gax
/
src
/
ResourceTemplate
/
Parser.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
Parser.php
228 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 | * Collection of methods for parsing Segments. |
| 39 | * |
| 40 | * @internal |
| 41 | */ |
| 42 | class Parser |
| 43 | { |
| 44 | /** |
| 45 | * Parses a path into an array of segments. |
| 46 | * |
| 47 | * @param string|null $path |
| 48 | * @return array |
| 49 | * @throws ValidationException |
| 50 | */ |
| 51 | public static function parseSegments(string $path = null) |
| 52 | { |
| 53 | if (empty($path)) { |
| 54 | throw new ValidationException("Cannot parse empty path"); |
| 55 | } |
| 56 | $segments = []; |
| 57 | $index = 0; |
| 58 | $nextLiteral = ''; |
| 59 | $segments[] = self::parseSegmentFromPath($path, $nextLiteral, $index); |
| 60 | while ($index < strlen($path)) { |
| 61 | self::parseLiteralFromPath($nextLiteral, $path, $index); |
| 62 | $segments[] = self::parseSegmentFromPath($path, $nextLiteral, $index); |
| 63 | } |
| 64 | return $segments; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Given a path and an index, reads a Segment from the path and updates |
| 69 | * the index. |
| 70 | * |
| 71 | * @param string $path |
| 72 | * @param string $nextLiteral |
| 73 | * @param int $index |
| 74 | * @return Segment |
| 75 | * @throws ValidationException |
| 76 | */ |
| 77 | private static function parseSegmentFromPath(string $path, string &$nextLiteral, int &$index) |
| 78 | { |
| 79 | if ($index >= strlen($path)) { |
| 80 | // A trailing '/' has caused the index to exceed the bounds |
| 81 | // of the string - provide a helpful error message. |
| 82 | throw self::parseError($path, strlen($path) - 1, "invalid trailing '/'"); |
| 83 | } |
| 84 | if ($path[$index] === '{') { |
| 85 | // Validate that the { has a matching } |
| 86 | $closingBraceIndex = strpos($path, '}', $index); |
| 87 | if ($closingBraceIndex === false) { |
| 88 | throw self::parseError( |
| 89 | $path, |
| 90 | strlen($path), |
| 91 | "Expected '}' to match '{' at index $index, got end of string" |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | $segmentStringLengthWithoutBraces = $closingBraceIndex - $index - 1; |
| 96 | $segmentStringWithoutBraces = substr($path, $index + 1, $segmentStringLengthWithoutBraces); |
| 97 | $index = $closingBraceIndex + 1; |
| 98 | |
| 99 | $nextLiteral = '/'; |
| 100 | $remainingPath = substr($path, $index); |
| 101 | if (!empty($remainingPath)) { |
| 102 | // Find the firstnon-slash separator seen, if any. |
| 103 | $nextSlashIndex = strpos($remainingPath, '/', 0); |
| 104 | $nonSlashSeparators = ['-', '_', '~', '.']; |
| 105 | foreach ($nonSlashSeparators as $nonSlashSeparator) { |
| 106 | $nonSlashSeparatorIndex = strpos($remainingPath, $nonSlashSeparator, 0); |
| 107 | $nextOpenBraceIndex = strpos($remainingPath, '{', 0); |
| 108 | if ($nonSlashSeparatorIndex !== false && $nonSlashSeparatorIndex === $nextOpenBraceIndex - 1) { |
| 109 | $index += $nonSlashSeparatorIndex; |
| 110 | $nextLiteral = $nonSlashSeparator; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return self::parseVariableSegment($segmentStringWithoutBraces, $nextLiteral); |
| 117 | } else { |
| 118 | $nextSlash = strpos($path, '/', $index); |
| 119 | if ($nextSlash === false) { |
| 120 | $nextSlash = strlen($path); |
| 121 | } |
| 122 | $segmentString = substr($path, $index, $nextSlash - $index); |
| 123 | $nextLiteral = '/'; |
| 124 | $index = $nextSlash; |
| 125 | return self::parse($segmentString, $path, $index); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param string $segmentString |
| 131 | * @param string $path |
| 132 | * @param int $index |
| 133 | * @return Segment |
| 134 | * @throws ValidationException |
| 135 | */ |
| 136 | private static function parse(string $segmentString, string $path, int $index) |
| 137 | { |
| 138 | if ($segmentString === '*') { |
| 139 | return new Segment(Segment::WILDCARD_SEGMENT); |
| 140 | } elseif ($segmentString === '**') { |
| 141 | return new Segment(Segment::DOUBLE_WILDCARD_SEGMENT); |
| 142 | } else { |
| 143 | if (!self::isValidLiteral($segmentString)) { |
| 144 | if (empty($segmentString)) { |
| 145 | // Create user friendly message in case of empty segment |
| 146 | throw self::parseError($path, $index, "Unexpected empty segment (consecutive '/'s are invalid)"); |
| 147 | } else { |
| 148 | throw self::parseError($path, $index, "Unexpected characters in literal segment $segmentString"); |
| 149 | } |
| 150 | } |
| 151 | return new Segment(Segment::LITERAL_SEGMENT, $segmentString); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param string $segmentStringWithoutBraces |
| 157 | * @param string $separatorLiteral |
| 158 | * @return Segment |
| 159 | * @throws ValidationException |
| 160 | */ |
| 161 | private static function parseVariableSegment(string $segmentStringWithoutBraces, string $separatorLiteral) |
| 162 | { |
| 163 | // Validate there are no nested braces |
| 164 | $nestedOpenBracket = strpos($segmentStringWithoutBraces, '{'); |
| 165 | if ($nestedOpenBracket !== false) { |
| 166 | throw new ValidationException( |
| 167 | "Unexpected '{' parsing segment $segmentStringWithoutBraces at index $nestedOpenBracket" |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | $equalsIndex = strpos($segmentStringWithoutBraces, '='); |
| 172 | if ($equalsIndex === false) { |
| 173 | // If the variable does not contain '=', we assume the pattern is '*' as per google.rpc.Http |
| 174 | $variableKey = $segmentStringWithoutBraces; |
| 175 | $nestedResource = new RelativeResourceTemplate("*"); |
| 176 | } else { |
| 177 | $variableKey = substr($segmentStringWithoutBraces, 0, $equalsIndex); |
| 178 | $nestedResourceString = substr($segmentStringWithoutBraces, $equalsIndex + 1); |
| 179 | $nestedResource = new RelativeResourceTemplate($nestedResourceString); |
| 180 | } |
| 181 | |
| 182 | if (!self::isValidLiteral($variableKey)) { |
| 183 | throw new ValidationException( |
| 184 | "Unexpected characters in variable name $variableKey" |
| 185 | ); |
| 186 | } |
| 187 | return new Segment(Segment::VARIABLE_SEGMENT, null, $variableKey, $nestedResource, $separatorLiteral); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * @param string $literal |
| 192 | * @param string $path |
| 193 | * @param int $index |
| 194 | * @return string |
| 195 | * @throws ValidationException |
| 196 | */ |
| 197 | private static function parseLiteralFromPath(string $literal, string $path, int &$index) |
| 198 | { |
| 199 | $literalLength = strlen($literal); |
| 200 | if (strlen($path) < ($index + $literalLength)) { |
| 201 | throw self::parseError($path, $index, "expected '$literal'"); |
| 202 | } |
| 203 | $consumedLiteral = substr($path, $index, $literalLength); |
| 204 | if ($consumedLiteral !== $literal) { |
| 205 | throw self::parseError($path, $index, "expected '$literal'"); |
| 206 | } |
| 207 | $index += $literalLength; |
| 208 | return $consumedLiteral; |
| 209 | } |
| 210 | |
| 211 | private static function parseError(string $path, int $index, string $reason) |
| 212 | { |
| 213 | return new ValidationException("Error parsing '$path' at index $index: $reason"); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Check if $literal is a valid segment literal. Segment literals may only contain numbers, |
| 218 | * letters, and any of the following: .-~_ |
| 219 | * |
| 220 | * @param string $literal |
| 221 | * @return bool |
| 222 | */ |
| 223 | private static function isValidLiteral(string $literal) |
| 224 | { |
| 225 | return preg_match("/^[0-9a-zA-Z\\.\\-~_]+$/", $literal) === 1; |
| 226 | } |
| 227 | } |
| 228 |