AST
2 months ago
BlockString.php
2 months ago
DirectiveLocation.php
2 months ago
Lexer.php
2 months ago
Parser.php
2 months ago
Printer.php
2 months ago
Source.php
2 months ago
SourceLocation.php
2 months ago
Token.php
2 months ago
Visitor.php
2 months ago
VisitorOperation.php
2 months ago
VisitorRemoveNode.php
2 months ago
VisitorSkipNode.php
2 months ago
VisitorStop.php
2 months ago
Source.php
53 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL\Language; |
| 4 | |
| 5 | class Source |
| 6 | { |
| 7 | public string $body; |
| 8 | |
| 9 | public int $length; |
| 10 | |
| 11 | public string $name; |
| 12 | |
| 13 | public SourceLocation $locationOffset; |
| 14 | |
| 15 | /** |
| 16 | * A representation of source input to GraphQL. |
| 17 | * |
| 18 | * `name` and `locationOffset` are optional. They are useful for clients who |
| 19 | * store Automattic\WooCommerce\Vendor\GraphQL documents in source files; for example, if the Automattic\WooCommerce\Vendor\GraphQL input |
| 20 | * starts at line 40 in a file named Foo.graphql, it might be useful for name to |
| 21 | * be "Foo.graphql" and location to be `{ line: 40, column: 0 }`. |
| 22 | * line and column in locationOffset are 1-indexed |
| 23 | */ |
| 24 | public function __construct(string $body, ?string $name = null, ?SourceLocation $location = null) |
| 25 | { |
| 26 | $this->body = $body; |
| 27 | $this->length = mb_strlen($body, 'UTF-8'); |
| 28 | $this->name = $name === '' || $name === null |
| 29 | ? 'Automattic\WooCommerce\Vendor\GraphQL request' |
| 30 | : $name; |
| 31 | $this->locationOffset = $location ?? new SourceLocation(1, 1); |
| 32 | } |
| 33 | |
| 34 | public function getLocation(int $position): SourceLocation |
| 35 | { |
| 36 | $line = 1; |
| 37 | $column = $position + 1; |
| 38 | |
| 39 | $utfChars = json_decode('"\u2028\u2029"'); |
| 40 | $lineRegexp = '/\r\n|[\n\r' . $utfChars . ']/su'; |
| 41 | $matches = []; |
| 42 | preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, \PREG_OFFSET_CAPTURE); |
| 43 | |
| 44 | foreach ($matches[0] as $match) { |
| 45 | ++$line; |
| 46 | |
| 47 | $column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8')); |
| 48 | } |
| 49 | |
| 50 | return new SourceLocation($line, $column); |
| 51 | } |
| 52 | } |
| 53 |