GraphQL\Language\Source::getLocation PHP Method

getLocation() public method

public getLocation ( $position ) : SourceLocation
$position
return SourceLocation
    public function getLocation($position)
    {
        $line = 1;
        $column = $position + 1;
        $utfChars = json_decode('"\\u2028\\u2029"');
        $lineRegexp = '/\\r\\n|[\\n\\r' . $utfChars . ']/su';
        $matches = [];
        preg_match_all($lineRegexp, mb_substr($this->body, 0, $position, 'UTF-8'), $matches, PREG_OFFSET_CAPTURE);
        foreach ($matches[0] as $index => $match) {
            $line += 1;
            $column = $position + 1 - ($match[1] + mb_strlen($match[0], 'UTF-8'));
        }
        return new SourceLocation($line, $column);
    }

Usage Example

Example #1
0
 /**
  * @param Source $source
  * @param int $position
  * @param string $description
  */
 public function __construct(Source $source, $position, $description)
 {
     $location = $source->getLocation($position);
     $syntaxError = "Syntax Error {$source->name} ({$location->line}:{$location->column}) {$description}\n\n" . self::highlightSourceAtLocation($source, $location);
     parent::__construct($syntaxError, null, $source, [$position]);
 }