Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Commit

Permalink
Move blockStringValue into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
crisu83 committed Feb 26, 2018
1 parent 33c480e commit 3584762
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 68 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"autoload": {
"files": [
"./src/Language/blockStringValue.php",
"./src/Language/helpers.php",
"./src/Type/definition.php",
"./src/Type/directives.php",
Expand All @@ -29,6 +30,7 @@
},
"autoload-dev": {
"files": [
"./src/Language/blockStringValue.php",
"./src/Language/helpers.php",
"./src/Type/definition.php",
"./src/Type/directives.php",
Expand Down
14 changes: 14 additions & 0 deletions src/Language/AST/Visitor/AbstractVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: chris
* Date: 26/02/2018
* Time: 14.17
*/

namespace Digia\GraphQL\Language\AST\Visitor;

class AbstractVisitor
{

}
14 changes: 14 additions & 0 deletions src/Language/AST/Visitor/Behavior/AcceptVisitorTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: chris
* Date: 26/02/2018
* Time: 14.20
*/

namespace Digia\GraphQL\Language\AST\Visitor;

trait AcceptVisitorTrait
{

}
14 changes: 14 additions & 0 deletions src/Language/AST/Visitor/Contract/VisitorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: chris
* Date: 26/02/2018
* Time: 14.13
*/

namespace Digia\GraphQL\Language\AST\Visitor;

interface VisitorInterface
{

}
8 changes: 8 additions & 0 deletions src/Language/Contract/PrinterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Digia\GraphQL\Language;

interface PrinterInterface
{

}
14 changes: 14 additions & 0 deletions src/Language/Printer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Created by PhpStorm.
* User: chris
* Date: 26/02/2018
* Time: 14.07
*/

namespace Digia\GraphQL\Language;

class Printer
{

}
71 changes: 71 additions & 0 deletions src/Language/blockStringValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Digia\GraphQL\Language;

/**
* Produces the value of a block string from its parsed raw value, similar to
* Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc.
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*
* @param string $rawString
* @return string
*/
function blockStringValue(string $rawString): string
{
$lines = preg_split("/\r\n|[\n\r]/", $rawString);
$lineCount = count($lines);

$commonIndent = null;

for ($i = 1; $i < $lineCount; $i++) {
$line = $lines[$i];
$indent = leadingWhitespace($line);

if ($indent < mb_strlen($line) && ($commonIndent === null || $indent < $commonIndent)) {
$commonIndent = $indent;

if ($commonIndent === 0) {
break;
}
}
}

if ($commonIndent > 0) {
for ($i = 1; $i < $lineCount; $i++) {
$lines[$i] = sliceString($lines[$i], $commonIndent);
}
}

while (count($lines) > 0 && isBlank($lines[0])) {
array_shift($lines);
}

while (($lineCount = count($lines)) > 0 && isBlank($lines[$lineCount - 1])) {
array_pop($lines);
}

return implode("\n", $lines);
}

/**
* @param string $string
* @return int
*/
function leadingWhitespace(string $string): int
{
$i = 0;
$length = mb_strlen($string);
while ($i < $length && ($string[$i] === ' ' || $string[$i] === "\t")) {
$i++;
}
return $i;
}

/**
* @param string $string
* @return bool
*/
function isBlank(string $string): bool
{
return leadingWhitespace($string) === mb_strlen($string);
}
68 changes: 0 additions & 68 deletions src/Language/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,74 +121,6 @@ function uniCharCode(string $a, string $b, string $c, string $d): string
return (dechex(ordUTF8($a)) << 12) | (dechex(ordUTF8($b)) << 8) | (dechex(ordUTF8($c)) << 4) | dechex(ordUTF8($d));
}

/**
* Produces the value of a block string from its parsed raw value, similar to
* Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc.
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*
* @param string $rawString
* @return string
*/
function blockStringValue(string $rawString): string
{
$lines = preg_split("/\r\n|[\n\r]/", $rawString);
$lineCount = count($lines);

$commonIndent = null;

for ($i = 1; $i < $lineCount; $i++) {
$line = $lines[$i];
$indent = leadingWhitespace($line);

if ($indent < mb_strlen($line) && ($commonIndent === null || $indent < $commonIndent)) {
$commonIndent = $indent;

if ($commonIndent === 0) {
break;
}
}
}

if ($commonIndent > 0) {
for ($i = 1; $i < $lineCount; $i++) {
$lines[$i] = sliceString($lines[$i], $commonIndent);
}
}

while (count($lines) > 0 && isBlank($lines[0])) {
array_shift($lines);
}

while (($lineCount = count($lines)) > 0 && isBlank($lines[$lineCount - 1])) {
array_pop($lines);
}

return implode("\n", $lines);
}

/**
* @param string $string
* @return int
*/
function leadingWhitespace(string $string): int
{
$i = 0;
$length = mb_strlen($string);
while ($i < $length && ($string[$i] === ' ' || $string[$i] === "\t")) {
$i++;
}
return $i;
}

/**
* @param string $string
* @return bool
*/
function isBlank(string $string): bool
{
return leadingWhitespace($string) === mb_strlen($string);
}

/**
* @param string $value
* @return bool
Expand Down

0 comments on commit 3584762

Please sign in to comment.