AST.php
2 months ago
ASTDefinitionBuilder.php
2 months ago
BreakingChangesFinder.php
2 months ago
BuildClientSchema.php
2 months ago
BuildSchema.php
2 months ago
InterfaceImplementations.php
2 months ago
LazyException.php
2 months ago
LexicalDistance.php
2 months ago
MixedStore.php
2 months ago
PairSet.php
2 months ago
PhpDoc.php
2 months ago
SchemaExtender.php
2 months ago
SchemaPrinter.php
2 months ago
TypeComparators.php
2 months ago
TypeInfo.php
2 months ago
Utils.php
2 months ago
Value.php
2 months ago
Utils.php
295 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Utils; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Warning; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\Node; |
| 8 | |
| 9 | class Utils |
| 10 | { |
| 11 | public static function undefined(): \stdClass |
| 12 | { |
| 13 | static $undefined; |
| 14 | |
| 15 | return $undefined ??= new \stdClass(); |
| 16 | } |
| 17 | |
| 18 | /** @param array<string, mixed> $vars */ |
| 19 | public static function assign(object $obj, array $vars): object |
| 20 | { |
| 21 | foreach ($vars as $key => $value) { |
| 22 | if (! property_exists($obj, $key)) { |
| 23 | $cls = get_class($obj); |
| 24 | Warning::warn( |
| 25 | "Trying to set non-existing property '{$key}' on class '{$cls}'", |
| 26 | Warning::WARNING_ASSIGN |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | $obj->{$key} = $value; |
| 31 | } |
| 32 | |
| 33 | return $obj; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Print a value that came from JSON for debugging purposes. |
| 38 | * |
| 39 | * @param mixed $value |
| 40 | */ |
| 41 | public static function printSafeJson($value): string |
| 42 | { |
| 43 | if ($value instanceof \stdClass) { |
| 44 | return static::jsonEncodeOrSerialize($value); |
| 45 | } |
| 46 | |
| 47 | return static::printSafeInternal($value); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Print a value that came from PHP for debugging purposes. |
| 52 | * |
| 53 | * @param mixed $value |
| 54 | */ |
| 55 | public static function printSafe($value): string |
| 56 | { |
| 57 | if (is_object($value)) { |
| 58 | if (method_exists($value, '__toString')) { |
| 59 | return $value->__toString(); |
| 60 | } |
| 61 | |
| 62 | return 'instance of ' . get_class($value); |
| 63 | } |
| 64 | |
| 65 | return static::printSafeInternal($value); |
| 66 | } |
| 67 | |
| 68 | /** @param \stdClass|array<mixed> $value */ |
| 69 | protected static function jsonEncodeOrSerialize($value): string |
| 70 | { |
| 71 | try { |
| 72 | return json_encode($value, JSON_THROW_ON_ERROR); |
| 73 | } catch (\JsonException $jsonException) { |
| 74 | return serialize($value); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** @param mixed $value */ |
| 79 | protected static function printSafeInternal($value): string |
| 80 | { |
| 81 | if (is_array($value)) { |
| 82 | return static::jsonEncodeOrSerialize($value); |
| 83 | } |
| 84 | |
| 85 | if ($value === '') { |
| 86 | return '(empty string)'; |
| 87 | } |
| 88 | |
| 89 | if ($value === null) { |
| 90 | return 'null'; |
| 91 | } |
| 92 | |
| 93 | if ($value === false) { |
| 94 | return 'false'; |
| 95 | } |
| 96 | |
| 97 | if ($value === true) { |
| 98 | return 'true'; |
| 99 | } |
| 100 | |
| 101 | if (is_string($value)) { |
| 102 | return "\"{$value}\""; |
| 103 | } |
| 104 | |
| 105 | if (is_scalar($value)) { |
| 106 | return (string) $value; |
| 107 | } |
| 108 | |
| 109 | return gettype($value); |
| 110 | } |
| 111 | |
| 112 | /** UTF-8 compatible chr(). */ |
| 113 | public static function chr(int $ord, string $encoding = 'UTF-8'): string |
| 114 | { |
| 115 | if ($encoding === 'UCS-4BE') { |
| 116 | return pack('N', $ord); |
| 117 | } |
| 118 | |
| 119 | return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE'); |
| 120 | } |
| 121 | |
| 122 | /** UTF-8 compatible ord(). */ |
| 123 | public static function ord(string $char, string $encoding = 'UTF-8'): int |
| 124 | { |
| 125 | if (! isset($char[1])) { |
| 126 | return ord($char); |
| 127 | } |
| 128 | |
| 129 | if ($encoding !== 'UCS-4BE') { |
| 130 | $char = mb_convert_encoding($char, 'UCS-4BE', $encoding); |
| 131 | assert(is_string($char), 'format string is statically known to be correct'); |
| 132 | } |
| 133 | |
| 134 | $unpacked = unpack('N', $char); |
| 135 | assert(is_array($unpacked), 'format string is statically known to be correct'); |
| 136 | |
| 137 | return $unpacked[1]; |
| 138 | } |
| 139 | |
| 140 | /** Returns UTF-8 char code at given $positing of the $string. */ |
| 141 | public static function charCodeAt(string $string, int $position): int |
| 142 | { |
| 143 | $char = mb_substr($string, $position, 1, 'UTF-8'); |
| 144 | |
| 145 | return self::ord($char); |
| 146 | } |
| 147 | |
| 148 | /** @throws \JsonException */ |
| 149 | public static function printCharCode(?int $code): string |
| 150 | { |
| 151 | if ($code === null) { |
| 152 | return '<EOF>'; |
| 153 | } |
| 154 | |
| 155 | return $code < 0x007F |
| 156 | // Trust JSON for ASCII |
| 157 | ? json_encode(self::chr($code), JSON_THROW_ON_ERROR) |
| 158 | // Otherwise, print the escaped form |
| 159 | : '"\\u' . dechex($code) . '"'; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Upholds the spec rules about naming. |
| 164 | * |
| 165 | * @throws Error |
| 166 | */ |
| 167 | public static function assertValidName(string $name): void |
| 168 | { |
| 169 | $error = self::isValidNameError($name); |
| 170 | if ($error !== null) { |
| 171 | throw $error; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** Returns an Error if a name is invalid. */ |
| 176 | public static function isValidNameError(string $name, ?Node $node = null): ?Error |
| 177 | { |
| 178 | if (isset($name[1]) && $name[0] === '_' && $name[1] === '_') { |
| 179 | return new Error( |
| 180 | "Name \"{$name}\" must not begin with \"__\", which is reserved by Automattic\WooCommerce\Vendor\GraphQL introspection.", |
| 181 | $node |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | if (preg_match('/^[_a-zA-Z][_a-zA-Z0-9]*$/', $name) !== 1) { |
| 186 | return new Error( |
| 187 | "Names must match /^[_a-zA-Z][_a-zA-Z0-9]*\$/ but \"{$name}\" does not.", |
| 188 | $node |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | return null; |
| 193 | } |
| 194 | |
| 195 | /** @param array<string> $items */ |
| 196 | public static function quotedOrList(array $items): string |
| 197 | { |
| 198 | $quoted = array_map( |
| 199 | static fn (string $item): string => "\"{$item}\"", |
| 200 | $items |
| 201 | ); |
| 202 | |
| 203 | return self::orList($quoted); |
| 204 | } |
| 205 | |
| 206 | /** @param array<string> $items */ |
| 207 | public static function orList(array $items): string |
| 208 | { |
| 209 | if ($items === []) { |
| 210 | return ''; |
| 211 | } |
| 212 | |
| 213 | $selected = array_slice($items, 0, 5); |
| 214 | $selectedLength = count($selected); |
| 215 | $firstSelected = $selected[0]; |
| 216 | |
| 217 | if ($selectedLength === 1) { |
| 218 | return $firstSelected; |
| 219 | } |
| 220 | |
| 221 | return array_reduce( |
| 222 | range(1, $selectedLength - 1), |
| 223 | static fn ($list, $index): string => $list |
| 224 | . ($selectedLength > 2 ? ', ' : ' ') |
| 225 | . ($index === $selectedLength - 1 ? 'or ' : '') |
| 226 | . $selected[$index], |
| 227 | $firstSelected |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Given an invalid input string and a list of valid options, returns a filtered |
| 233 | * list of valid options sorted based on their similarity with the input. |
| 234 | * |
| 235 | * @param array<string> $options |
| 236 | * |
| 237 | * @return array<int, string> |
| 238 | */ |
| 239 | public static function suggestionList(string $input, array $options): array |
| 240 | { |
| 241 | /** @var array<string, int> $optionsByDistance */ |
| 242 | $optionsByDistance = []; |
| 243 | $lexicalDistance = new LexicalDistance($input); |
| 244 | $threshold = mb_strlen($input) * 0.4 + 1; |
| 245 | foreach ($options as $option) { |
| 246 | $distance = $lexicalDistance->measure($option, $threshold); |
| 247 | |
| 248 | if ($distance !== null) { |
| 249 | $optionsByDistance[$option] = $distance; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | uksort($optionsByDistance, static function (string $a, string $b) use ($optionsByDistance) { |
| 254 | $distanceDiff = $optionsByDistance[$a] - $optionsByDistance[$b]; |
| 255 | |
| 256 | return $distanceDiff !== 0 ? $distanceDiff : strnatcmp($a, $b); |
| 257 | }); |
| 258 | |
| 259 | return array_map('strval', array_keys($optionsByDistance)); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Try to extract the value for a key from an object like value. |
| 264 | * |
| 265 | * @param mixed $objectLikeValue |
| 266 | * |
| 267 | * @return mixed |
| 268 | */ |
| 269 | public static function extractKey($objectLikeValue, string $key) |
| 270 | { |
| 271 | if (is_array($objectLikeValue) || $objectLikeValue instanceof \ArrayAccess) { |
| 272 | return $objectLikeValue[$key] ?? null; |
| 273 | } |
| 274 | |
| 275 | if (is_object($objectLikeValue)) { |
| 276 | return $objectLikeValue->{$key} ?? null; |
| 277 | } |
| 278 | |
| 279 | return null; |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Split a string that has either Unix, Windows or Mac style newlines into lines. |
| 284 | * |
| 285 | * @return list<string> |
| 286 | */ |
| 287 | public static function splitLines(string $value): array |
| 288 | { |
| 289 | $lines = preg_split("/\r\n|\r|\n/", $value); |
| 290 | assert(is_array($lines), 'given the regex is valid'); |
| 291 | |
| 292 | return $lines; |
| 293 | } |
| 294 | } |
| 295 |