Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/Flow/JSONPath/AccessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,64 @@ class AccessHelper
{
public static function collectionKeys($collection)
{
if (is_object($collection)) {
return array_keys(get_object_vars($collection));
if (\is_object($collection)) {
return \array_keys(\get_object_vars($collection));
} else {
return array_keys($collection);
return \array_keys($collection);
}
}

public static function isCollectionType($collection)
{
return is_array($collection) || is_object($collection);
return \is_array($collection) || \is_object($collection);
}

public static function keyExists($collection, $key, $magicIsAllowed = false)
{
if ($magicIsAllowed && is_object($collection) && method_exists($collection, '__get')) {
if ($magicIsAllowed && \is_object($collection) && \method_exists($collection, '__get')) {
return true;
}

if (is_int($key) && $key < 0) {
$key = abs((int)$key);
if (\is_int($key) && $key < 0) {
$key = \abs((int)$key);
}

if (is_array($collection) || $collection instanceof \ArrayAccess) {
return array_key_exists($key, $collection);
} else if (is_object($collection)) {
return property_exists($collection, $key);
if (\is_array($collection) || $collection instanceof \ArrayAccess) {
return \array_key_exists($key, $collection);
} else if (\is_object($collection)) {
return \property_exists($collection, $key);
}

return false;
}

public static function getValue($collection, $key, $magicIsAllowed = false)
{
if ($magicIsAllowed && is_object($collection) && method_exists($collection, '__get') && !$collection instanceof \ArrayAccess) {
if ($magicIsAllowed && \is_object($collection) && \method_exists($collection, '__get') && !$collection instanceof \ArrayAccess) {
return $collection->__get($key);
}

if (is_object($collection) && !$collection instanceof \ArrayAccess) {
if (\is_object($collection) && !$collection instanceof \ArrayAccess) {
return $collection->$key;
}

if (is_array($collection)) {
if (is_int($key)) {
return array_slice($collection, $key, 1, false)[0];
if (\is_array($collection)) {
if (\is_int($key)) {
return \array_slice($collection, $key, 1, false)[0];
} else {
return $collection[$key];
}
}

if (is_object($collection) && !$collection instanceof \ArrayAccess) {
if (\is_object($collection) && !$collection instanceof \ArrayAccess) {
return $collection->$key;
}

/*
* Find item in php collection by index
* Written this way to handle instances ArrayAccess or Traversable objects
*/
if (is_int($key)) {
if (\is_int($key)) {
$i = 0;
foreach ($collection as $val) {
if ($i === $key) {
Expand All @@ -88,7 +88,7 @@ public static function getValue($collection, $key, $magicIsAllowed = false)

public static function setValue(&$collection, $key, $value)
{
if (is_object($collection) && ! $collection instanceof \ArrayAccess) {
if (\is_object($collection) && ! $collection instanceof \ArrayAccess) {
return $collection->$key = $value;
} else {
return $collection[$key] = $value;
Expand All @@ -97,7 +97,7 @@ public static function setValue(&$collection, $key, $value)

public static function unsetValue(&$collection, $key)
{
if (is_object($collection) && ! $collection instanceof \ArrayAccess) {
if (\is_object($collection) && ! $collection instanceof \ArrayAccess) {
unset($collection->$key);
} else {
unset($collection[$key]);
Expand All @@ -106,10 +106,10 @@ public static function unsetValue(&$collection, $key)

public static function arrayValues($collection)
{
if (is_array($collection)) {
return array_values($collection);
} else if (is_object($collection)) {
return array_values((array) $collection);
if (\is_array($collection)) {
return \array_values($collection);
} else if (\is_object($collection)) {
return \array_values((array) $collection);
}

throw new JSONPathException("Invalid variable type for arrayValues");
Expand Down
12 changes: 6 additions & 6 deletions src/Flow/JSONPath/Filters/QueryMatchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function filter($collection)
{
$return = [];

preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);
\preg_match('/^' . static::MATCH_QUERY_OPERATORS . '$/x', $this->token->value, $matches);

if (!isset($matches[1])) {
throw new \Exception("Malformed filter query");
Expand All @@ -34,18 +34,18 @@ public function filter($collection)
$operator = isset($matches['operator']) ? $matches['operator'] : null;
$comparisonValue = isset($matches['comparisonValue']) ? $matches['comparisonValue'] : null;

if (strtolower($comparisonValue) === "false") {
if (\strtolower($comparisonValue) === "false") {
$comparisonValue = false;
}
if (strtolower($comparisonValue) === "true") {
if (\strtolower($comparisonValue) === "true") {
$comparisonValue = true;
}
if (strtolower($comparisonValue) === "null") {
if (\strtolower($comparisonValue) === "null") {
$comparisonValue = null;
}

$comparisonValue = preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = preg_replace('/[\'"]$/', '', $comparisonValue);
$comparisonValue = \preg_replace('/^[\'"]/', '', $comparisonValue);
$comparisonValue = \preg_replace('/[\'"]$/', '', $comparisonValue);

foreach ($collection as $value) {
if (AccessHelper::keyExists($value, $key, $this->magicIsAllowed)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Flow/JSONPath/Filters/QueryResultFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public function filter($collection)
{
$result = [];

preg_match('/@\.(?<key>\w+)\s*(?<operator>-|\+|\*|\/)\s*(?<numeric>\d+)/', $this->token->value, $matches);
\preg_match('/@\.(?<key>\w+)\s*(?<operator>-|\+|\*|\/)\s*(?<numeric>\d+)/', $this->token->value, $matches);

$matchKey = $matches['key'];

if (AccessHelper::keyExists($collection, $matchKey, $this->magicIsAllowed)) {
$value = AccessHelper::getValue($collection, $matchKey, $this->magicIsAllowed);
} else {
if ($matches['key'] === 'length') {
$value = count($collection);
$value = \count($collection);
} else {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Flow/JSONPath/Filters/SliceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function filter($collection)
{
$result = [];

$length = count($collection);
$length = \count($collection);
$start = $this->token->value['start'];
$end = $this->token->value['end'];
$step = $this->token->value['step'] ?: 1;
Expand Down
22 changes: 11 additions & 11 deletions src/Flow/JSONPath/JSONPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function find($expression)
foreach ($collectionData as $value) {
if (AccessHelper::isCollectionType($value)) {
$filteredValue = $filter->filter($value);
$filteredData = array_merge($filteredData, $filteredValue);
$filteredData = \array_merge($filteredData, $filteredValue);
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public function last()
return null;
}

$value = $this->data[end($keys)] ? $this->data[end($keys)] : null;
$value = $this->data[\end($keys)] ? $this->data[\end($keys)] : null;

return AccessHelper::isCollectionType($value) ? new static($value, $this->options) : $value;
}
Expand Down Expand Up @@ -115,11 +115,11 @@ public function lastKey()
{
$keys = AccessHelper::collectionKeys($this->data);

if (empty($keys) || end($keys) === false) {
if (empty($keys) || \end($keys) === false) {
return null;
}

return end($keys);
return \end($keys);
}

/**
Expand All @@ -129,7 +129,7 @@ public function lastKey()
*/
public function parseTokens($expression)
{
$cacheKey = md5($expression);
$cacheKey = \md5($expression);

if (isset(static::$tokenCache[$cacheKey])) {
return static::$tokenCache[$cacheKey];
Expand Down Expand Up @@ -190,7 +190,7 @@ public function jsonSerialize()
*/
public function current()
{
$value = current($this->data);
$value = \current($this->data);

return AccessHelper::isCollectionType($value) ? new static($value, $this->options) : $value;
}
Expand All @@ -200,31 +200,31 @@ public function current()
*/
public function next()
{
next($this->data);
\next($this->data);
}

/**
* Return the key of the current element
*/
public function key()
{
return key($this->data);
return \key($this->data);
}

/**
* Checks if current position is valid
*/
public function valid()
{
return key($this->data) !== null;
return \key($this->data) !== null;
}

/**
* Rewind the Iterator to the first element
*/
public function rewind()
{
reset($this->data);
\reset($this->data);
}

/**
Expand All @@ -241,6 +241,6 @@ public function __get($key)
*/
public function count()
{
return count($this->data);
return \count($this->data);
}
}
36 changes: 18 additions & 18 deletions src/Flow/JSONPath/JSONPathLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class JSONPathLexer

public function __construct($expression)
{
$expression = trim($expression);
$expression = \trim($expression);

$len = strlen($expression);
$len = \strlen($expression);
if (!$len) {
return;
}
Expand All @@ -44,7 +44,7 @@ public function __construct($expression)
if ($len === 1) {
return;
}
$expression = substr($expression, 1);
$expression = \substr($expression, 1);
$len--;
}

Expand Down Expand Up @@ -164,25 +164,25 @@ public function parseExpression()
*/
protected function createToken($value)
{
if (preg_match('/^(' . static::MATCH_INDEX . ')$/xu', $value, $matches)) {
if (preg_match('/^-?\d+$/', $value)) {
if (\preg_match('/^(' . static::MATCH_INDEX . ')$/xu', $value, $matches)) {
if (\preg_match('/^-?\d+$/', $value)) {
$value = (int)$value;
}
return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}

if (preg_match('/^' . static::MATCH_INDEXES . '$/xu', $value, $matches)) {
$value = explode(',', trim($value, ','));
if (\preg_match('/^' . static::MATCH_INDEXES . '$/xu', $value, $matches)) {
$value = \explode(',', \trim($value, ','));

foreach ($value as $i => $v) {
$value[$i] = (int) trim($v);
$value[$i] = (int) \trim($v);
}

return new JSONPathToken(JSONPathToken::T_INDEXES, $value);
}

if (preg_match('/^' . static::MATCH_SLICE . '$/xu', $value, $matches)) {
$parts = explode(':', $value);
if (\preg_match('/^' . static::MATCH_SLICE . '$/xu', $value, $matches)) {
$parts = \explode(':', $value);

$value = [
'start' => isset($parts[0]) && $parts[0] !== "" ? (int) $parts[0] : null,
Expand All @@ -193,28 +193,28 @@ protected function createToken($value)
return new JSONPathToken(JSONPathToken::T_SLICE, $value);
}

if (preg_match('/^' . static::MATCH_QUERY_RESULT . '$/xu', $value)) {
$value = substr($value, 1, -1);
if (\preg_match('/^' . static::MATCH_QUERY_RESULT . '$/xu', $value)) {
$value = \substr($value, 1, -1);

return new JSONPathToken(JSONPathToken::T_QUERY_RESULT, $value);
}

if (preg_match('/^' . static::MATCH_QUERY_MATCH . '$/xu', $value)) {
$value = substr($value, 2, -1);
if (\preg_match('/^' . static::MATCH_QUERY_MATCH . '$/xu', $value)) {
$value = \substr($value, 2, -1);

return new JSONPathToken(JSONPathToken::T_QUERY_MATCH, $value);
}

if (preg_match('/^' . static::MATCH_INDEX_IN_SINGLE_QUOTES . '$/xu', $value, $matches)) {
if (\preg_match('/^' . static::MATCH_INDEX_IN_SINGLE_QUOTES . '$/xu', $value, $matches)) {
$value = $matches[1];
$value = trim($value);
$value = \trim($value);

return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}

if (preg_match('/^' . static::MATCH_INDEX_IN_DOUBLE_QUOTES . '$/xu', $value, $matches)) {
if (\preg_match('/^' . static::MATCH_INDEX_IN_DOUBLE_QUOTES . '$/xu', $value, $matches)) {
$value = $matches[1];
$value = trim($value);
$value = \trim($value);

return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Flow/JSONPath/JSONPathToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct($type, $value)

public function validateType($type)
{
if (!in_array($type, static::getTypes(), true)) {
if (!\in_array($type, static::getTypes(), true)) {
throw new JSONPathException('Invalid token: ' . $type);
}
}
Expand All @@ -59,9 +59,9 @@ public static function getTypes()
*/
public function buildFilter($options)
{
$filterClass = 'Flow\\JSONPath\\Filters\\' . ucfirst($this->type) . 'Filter';
$filterClass = 'Flow\\JSONPath\\Filters\\' . \ucfirst($this->type) . 'Filter';

if (! class_exists($filterClass)) {
if (! \class_exists($filterClass)) {
throw new JSONPathException("No filter class exists for token [{$this->type}]");
}

Expand Down
Loading