Skip to content

Commit

Permalink
Added UUID filtering; ModelSchema integration
Browse files Browse the repository at this point in the history
  • Loading branch information
RoccoHoward committed Feb 26, 2018
1 parent b0ca540 commit 1e3374c
Showing 1 changed file with 85 additions and 7 deletions.
92 changes: 85 additions & 7 deletions src/ModelSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ class ModelSearch
'listLookup',
];

/**
* UUID operators.
*
* @var array
*/
protected static $uuid_operators = [
'=' => ['value' => '=', 'name' => 'Equals', 'inline' => 'is'],
'!=' => ['value' => '!=', 'name' => 'Not equal', 'inline' => 'is not'],
'IN' => ['value' => 'IN', 'name' => 'In...', 'inline' => 'in', 'helper' => 'Separated by semi-colon'],
'NOT_IN' => ['value' => 'NOT_IN', 'name' => 'Not in...', 'inline' => 'not in', 'helper' => 'Separated by semi-colon'],
'NULL' => ['value' => 'NULL', 'name' => 'NULL', 'inline' => 'is null'],
'NOT_NULL' => ['value' => 'NOT_NULL', 'name' => 'Not NULL', 'inline' => 'is not null'],
];

/**
* String operators.
*
Expand Down Expand Up @@ -264,7 +278,7 @@ private static function getRelation($relation)
break;
case 'HasOne':
$parent_key = $table.'.'.$relation->getParentKey();
$foreign_key = $table.'.'.$relation->getForeignKey();
$foreign_key = $table.'.'.$relation->getForeignKeyName();
break;
}

Expand Down Expand Up @@ -311,6 +325,27 @@ private static function buildCastedAttributes($model, &$result, $method = null)
$name_append = $method.'.';
}

// ModelSchema implementation gives us better data.
if (class_exists('HnhDigital\ModelSchema\Model')
&& $model instanceOf \HnhDigital\ModelSchema\Model) {

// Build attributes off the schema.
foreach ($model->getSchema() as $name => $config) {
$result[$name_append.$name] = [
'name' => $name,
'title' => array_get($config, 'title', $name),
'attributes' => [sprintf('%s.%s', $model->getTable(), $name)],
'filter' => self::convertCast(array_get($config, 'cast')),
'model' => &$model,
'model_name' => $model_name,
'source_model' => array_get($config, 'model'),
'source_model_name' => array_get($config, 'model_name', 'display_name'),
];
}

return;
}

// Build attributes off the specified casts.
foreach ($model->getCasts() as $name => $cast) {
$result[$name_append.$name] = [
Expand Down Expand Up @@ -407,7 +442,7 @@ private static function validateAttributes($model, $name, &$attributes)
// Check each of the attribute values.
// Convert any prepended with a curly to an expression.
foreach ($attributes as &$value) {
if (substr($value, 0, 1) === '{') {
if (substr($value, 0, 1) === '#' || substr($value, 0, 1) === '{') {
$value = new Expression(substr($value, 1));
}
}
Expand Down Expand Up @@ -561,6 +596,8 @@ private static function validateFilterItem($filter, $settings)
public static function getDefaultOperator($filter, $operator)
{
switch ($filter) {
case 'uuid':
return 'IN';
case 'string':
return '*=*';
case 'number':
Expand Down Expand Up @@ -594,11 +631,6 @@ private static function applyWildAll(&$operator, &$value)
*/
public static function parseInlineOperator($text)
{
// Convert string to filter array.
if (!is_array($text)) {
$text = ['', $text];
}

$operator_name = 'contains';
$operator = array_get($text, 0, '');
$value = array_get($text, 1, false);
Expand Down Expand Up @@ -681,6 +713,52 @@ public static function checkEmptyOperator(&$operator, &$value)
}
}

/**
* Filter by UUID.
*
* @return void
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public static function filterByUuid($filter)
{
$operator = array_get($filter, 'operator');
$method = array_get($filter, 'method');
$arguments = array_get($filter, 'arguments');
$value_one = array_get($filter, 'value_one');
$value_two = array_get($filter, 'value_two');
$settings = array_get($filter, 'settings');
$positive = array_get($filter, 'positive');

switch ($operator) {
case 'IN':
$method = 'whereIn';
$arguments = [static::getListFromString($value_one)];
break;
case 'NOT_IN':
$method = 'whereNotIn';
$arguments = [static::getListFromString($value_one)];
break;
case 'NULL':
$method = 'whereNull';
break;
case 'NOT_NULL':
$method = 'whereNotNull';
break;
}

return [
'operator' => $operator,
'method' => $method,
'arguments' => $arguments,
'value_one' => $value_one,
'value_two' => $value_two,
'settings' => $settings,
'positive' => $positive,
];
}

/**
* Filter by string.
*
Expand Down

0 comments on commit 1e3374c

Please sign in to comment.