Skip to content

Commit

Permalink
Allow fields with underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Jun 25, 2018
1 parent d65956b commit 278a577
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
33 changes: 33 additions & 0 deletions src/AbstractAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
9 changes: 5 additions & 4 deletions src/Resolve/EntityResolveAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
22 changes: 13 additions & 9 deletions src/Type/EntityTypeAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down

0 comments on commit 278a577

Please sign in to comment.