Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fields with underscores #13

Merged
Merged
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
Allow fields with underscores
  • Loading branch information
TomHAnderson committed Jun 25, 2018
commit 278a5778cbce5b4296cf8b345b28daa1d9ae5ab6
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