From 278a5778cbce5b4296cf8b345b28daa1d9ae5ab6 Mon Sep 17 00:00:00 2001 From: Tom H Anderson Date: Mon, 25 Jun 2018 15:50:06 -0600 Subject: [PATCH] Allow fields with underscores --- src/AbstractAbstractFactory.php | 33 ++++++++++++++++++++ src/Resolve/EntityResolveAbstractFactory.php | 9 +++--- src/Type/EntityTypeAbstractFactory.php | 22 +++++++------ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/AbstractAbstractFactory.php b/src/AbstractAbstractFactory.php index 1357da6..84ef4bd 100644 --- a/src/AbstractAbstractFactory.php +++ b/src/AbstractAbstractFactory.php @@ -90,4 +90,37 @@ protected function mapFieldType(string $fieldType) return $graphQLType; } + + /** + * In order to support fields with underscores we need to know + * if the possible filter name we found as the last _part of the + * filter field name is indeed a filter else it could be a field + * e.g. id_name filter resolves to 'name' and is not a filter + * e.g. id_eq filter resolves to 'eq' and is a filter + */ + public function isFilter($filterName) + { + switch (strtolower($filterName)) { + case 'eq': + case 'neq': + case 'gt': + case 'lt': + case 'gte': + case 'lte': + case 'in': + case 'notin': + case 'between': + case 'contains': + case 'startswith': + case 'endswith': + case 'memberof': + case 'isnull': + case 'sort': + case 'distinct': + return true; + default: + } + + return false; + } } diff --git a/src/Resolve/EntityResolveAbstractFactory.php b/src/Resolve/EntityResolveAbstractFactory.php index f398f79..44152eb 100644 --- a/src/Resolve/EntityResolveAbstractFactory.php +++ b/src/Resolve/EntityResolveAbstractFactory.php @@ -165,16 +165,17 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o } // Handle most fields as $field_$type: $value - if (! strstr($field, '_')) { + // Get right-most _text + $filter = substr($field, strrpos($field, '_') + 1); + if (strpos($field, '_') === false || ! $this->isFilter($filter)) { // Handle field:value $filterArray[] = [ 'type' => 'eq', 'field' => $field, 'value' => $value, ]; - } else { - $field = strtok($field, '_'); - $filter = strtok('_'); + } elseif (strpos($field, '_') !== false && $this->isFilter($filter)) { + $field = substr($field, 0, strrpos($field, '_')); switch ($filter) { case 'sort': diff --git a/src/Type/EntityTypeAbstractFactory.php b/src/Type/EntityTypeAbstractFactory.php index 27887a9..799fc51 100644 --- a/src/Type/EntityTypeAbstractFactory.php +++ b/src/Type/EntityTypeAbstractFactory.php @@ -156,15 +156,19 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o continue; } - if (! strstr($field, '_')) { - $filterArray[] = [ - 'type' => 'eq', - 'field' => $field, - 'value' => $value, - ]; - } else { - $field = strtok($field, '_'); - $filter = strtok('_'); + + // Handle most fields as $field_$type: $value + // Get right-most _text + $filter = substr($field, strrpos($field, '_') + 1); + if (strpos($field, '_') === false || ! $this->isFilter($filter)) { + // Handle field:value + $filterArray[] = [ + 'type' => 'eq', + 'field' => $field, + 'value' => $value, + ]; + } elseif (strpos($field, '_') !== false && $this->isFilter($filter)) { + $field = substr($field, 0, strrpos($field, '_')); switch ($filter) { case 'sort':