|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaunchDarkly\Impl\Model; |
| 6 | + |
| 7 | +/** |
| 8 | + * An attribute name or path expression identifying a value within an {@link LDContext}. |
| 9 | + * |
| 10 | + * Unlike some other SDKs, this class is not exposed in the public API. Due to the |
| 11 | + * stateless nature of typical PHP applications, there is little advantage in pre-parsing |
| 12 | + * attribute references and storing them internally. This class just encapsulates the |
| 13 | + * parsing logic. |
| 14 | + * |
| 15 | + * @ignore |
| 16 | + * @internal |
| 17 | + */ |
| 18 | +class AttributeReference |
| 19 | +{ |
| 20 | + const ERR_ATTR_EMPTY = 'attribute reference cannot be empty'; |
| 21 | + const ERR_ATTR_EXTRA_SLASH = 'attribute reference contained a double slash or a trailing slash'; |
| 22 | + const ERR_ATTR_INVALID_ESCAPE = |
| 23 | + 'attribute reference contained an escape character (~) that was not followed by 0 or 1'; |
| 24 | + |
| 25 | + private string $_path; |
| 26 | + private ?string $_singleComponent; |
| 27 | + /** @var string[]|null */ |
| 28 | + private ?array $_components; |
| 29 | + private ?string $_error; |
| 30 | + |
| 31 | + private function __construct(string $path, ?string $singleComponent, ?array $components, ?string $error) |
| 32 | + { |
| 33 | + $this->_path = $path; |
| 34 | + $this->_singleComponent = $singleComponent; |
| 35 | + $this->_components = $components; |
| 36 | + $this->_error = $error; |
| 37 | + } |
| 38 | + |
| 39 | + public static function parse(string $refPath): AttributeReference |
| 40 | + { |
| 41 | + if ($refPath === '' || $refPath === '/') { |
| 42 | + return self::failed($refPath, self::ERR_ATTR_EMPTY); |
| 43 | + } |
| 44 | + if (!str_starts_with($refPath, '/')) { |
| 45 | + return new AttributeReference($refPath, $refPath, null, null); |
| 46 | + } |
| 47 | + $components = explode('/', substr($refPath, 1)); |
| 48 | + if (count($components) === 1) { |
| 49 | + $attr = self::unescape($components[0]); |
| 50 | + if ($attr === null) { |
| 51 | + return self::failed($refPath, self::ERR_ATTR_INVALID_ESCAPE); |
| 52 | + } |
| 53 | + return new AttributeReference($refPath, $attr, null, null); |
| 54 | + } |
| 55 | + for ($i = 0; $i < count($components); $i++) { |
| 56 | + $prop = $components[$i]; |
| 57 | + if ($prop === '') { |
| 58 | + return self::failed($refPath, self::ERR_ATTR_EXTRA_SLASH); |
| 59 | + } |
| 60 | + $prop = self::unescape($prop); |
| 61 | + if ($prop === null) { |
| 62 | + return self::failed($refPath, self::ERR_ATTR_INVALID_ESCAPE); |
| 63 | + } |
| 64 | + $components[$i] = $prop; |
| 65 | + } |
| 66 | + return new AttributeReference($refPath, null, $components, null); |
| 67 | + } |
| 68 | + |
| 69 | + private static function failed(string $refPath, string $error): AttributeReference |
| 70 | + { |
| 71 | + return new AttributeReference($refPath, null, null, $error); |
| 72 | + } |
| 73 | + |
| 74 | + public function getPath(): string |
| 75 | + { |
| 76 | + return $this->_path; |
| 77 | + } |
| 78 | + |
| 79 | + public function getError(): ?string |
| 80 | + { |
| 81 | + return $this->_error; |
| 82 | + } |
| 83 | + |
| 84 | + public function getDepth(): int |
| 85 | + { |
| 86 | + return $this->_components === null ? 1 : count($this->_components); |
| 87 | + } |
| 88 | + |
| 89 | + public function getComponent(int $index): string |
| 90 | + { |
| 91 | + if ($this->_components === null) { |
| 92 | + return $index === 0 ? ($this->_singleComponent ?: '') : ''; |
| 93 | + } |
| 94 | + return $index < 0 || $index >= count($this->_components) ? '' : $this->_components[$index]; |
| 95 | + } |
| 96 | + |
| 97 | + private static function unescape(string $s): ?string |
| 98 | + { |
| 99 | + if (preg_match('/(~[^01]|~$)/', $s)) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + return str_replace('~0', '~', str_replace('~1', '/', $s)); |
| 103 | + } |
| 104 | +} |
0 commit comments