Skip to content

Commit

Permalink
Allow to filter UUID column with like and support for format UUID column
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Cyrankowski committed Apr 29, 2020
1 parent 6bcf6df commit 8339e8b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
62 changes: 56 additions & 6 deletions Grid/Column/UuidColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,74 @@

class UuidColumn extends Column
{
protected const FORMAT_SHORT = 'short';
protected const FORMAT_FULL = 'full';

protected static $formats = [
self::FORMAT_FULL,
self::FORMAT_SHORT,
];

protected static $availableOperators = [
self::OPERATOR_EQ,
self::OPERATOR_LIKE,
];

protected $format;

public function __initialize(array $params)
{
parent::__initialize($params);
$this->setFormat($this->getParam('format') ?? self::FORMAT_SHORT);
$this->setDefaultOperator($this->getParam('defaultOperator') ?? self::OPERATOR_LIKE);

$this->setOperators($this->getParam('operators', [
self::OPERATOR_EQ,
self::OPERATOR_LIKE,
]));
}

public function getType()
public function isQueryValid($query)
{
return 'uuid';
return true;
}

public function isQueryValid($query)
public function renderCell($value, $row, $router)
{
$value = $this->getDisplayedValue($value);

if (is_callable($this->callback)) {
return call_user_func($this->callback, $value, $row, $router);
}

return $value;
}

public function getDisplayedValue($value): string
{
$isValid = uuid_is_valid($query);
if ($this->format === self::FORMAT_SHORT) {
return (string) substr($value, 0, 8);
}

return $isValid === true ? true : false;
return (string) $value;
}

public function setFormat($format)
{
if (in_array($format, self::$formats, true)) {
$this->format = $format;
}

return $this;
}

public function getFormat()
{
return $this->format;
}

public function getType()
{
return 'uuid';
}
}
}
4 changes: 4 additions & 0 deletions Grid/Source/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use APY\DataGridBundle\Grid\Column\Column;
use APY\DataGridBundle\Grid\Column\JoinColumn;
use APY\DataGridBundle\Grid\Column\UuidColumn;
use APY\DataGridBundle\Grid\Row;
use APY\DataGridBundle\Grid\Rows;
use Doctrine\ORM\NoResultException;
Expand Down Expand Up @@ -438,6 +439,9 @@ public function execute($columns, $page = 0, $limit = 0, $maxResults = null, $gr
$columnForFilter = (!$column instanceof JoinColumn) ? $column : $columnsById[$filter->getColumnName()];

$fieldName = $this->getFieldName($columnForFilter, false);
if ($columnForFilter instanceof UuidColumn) {
$fieldName = sprintf('CAST(%s as TEXT)', $fieldName);
}
$bindIndexPlaceholder = "?$bindIndex";

if( in_array($filter->getOperator(), array(Column::OPERATOR_LIKE,Column::OPERATOR_RLIKE,Column::OPERATOR_LLIKE,Column::OPERATOR_NLIKE,))){
Expand Down

0 comments on commit 8339e8b

Please sign in to comment.