mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-29 00:25:17 +03:00
50 lines
1.0 KiB
PHP
50 lines
1.0 KiB
PHP
|
<?php
|
||
|
namespace GraphQL\Language;
|
||
|
|
||
|
class Source
|
||
|
{
|
||
|
/**
|
||
|
* @var string
|
||
|
*/
|
||
|
public $body;
|
||
|
|
||
|
/**
|
||
|
* @var int
|
||
|
*/
|
||
|
public $length;
|
||
|
|
||
|
/**
|
||
|
* @var string
|
||
|
*/
|
||
|
public $name;
|
||
|
|
||
|
public function __construct($body, $name = null)
|
||
|
{
|
||
|
$this->body = $body;
|
||
|
$this->length = mb_strlen($body, 'UTF-8');
|
||
|
$this->name = $name ?: 'GraphQL';
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param $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 = array();
|
||
|
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);
|
||
|
}
|
||
|
}
|