Skip to content

Commit

Permalink
[7.x] Nextras ORM 4 support, closes #984
Browse files Browse the repository at this point in the history
  • Loading branch information
radimvaculik authored and f3l1x committed Jan 25, 2023
1 parent 806fe8a commit 0139a06
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 95 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"mockery/mockery": "^1.3.3",
"nette/database": "^3.0.2",
"nette/tester": "^2.3.4",
"nextras/dbal": "^3.0.1 || ^4.0",
"nextras/orm": "^3.1.0 || ^4.0",
"nextras/dbal": "^4.0",
"nextras/orm": "^4.0",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-nette": "^1.0.0",
"phpstan/phpstan-strict-rules": "^1.4",
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ parameters:
message: '/^Instanceof between Ublaboo\\DataGrid\\Filter\\Filter and Ublaboo\\DataGrid\\Filter\\FilterDateRange will always evaluate to false\.$/'
count: 1
path: src/DataGrid.php
-
message: '/^Call to an undefined method Nextras\\Orm\\Collection\\ICollection\:\:getQueryBuilder\(\)\.$/'
count: 2
path: src/DataSource/NextrasDataSource.php
-
message: '/^Class Dibi\\Drivers\\MsSqlDriver not found\.$/'
count: 1
Expand Down
112 changes: 23 additions & 89 deletions src/DataSource/NextrasDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Ublaboo\DataGrid\Utils\ArraysHelper;
use Ublaboo\DataGrid\Utils\DateTimeHelper;
use Ublaboo\DataGrid\Utils\Sorting;
use UnexpectedValueException;
use function str_contains;

class NextrasDataSource extends FilterableDataSource implements IDataSource, IAggregatable
Expand All @@ -26,14 +25,8 @@ class NextrasDataSource extends FilterableDataSource implements IDataSource, IAg
/** @var array */
protected array $data = [];

private string $dbalCollectionClass;

public function __construct(protected ICollection $dataSource, protected string $primaryKey)
{
// Support version 4.0 with 3.1 backward compatibility
$this->dbalCollectionClass = class_exists(DbalCollection::class)
? DbalCollection::class
: 'Nextras\Orm\Mapper\Dbal\DbalCollection';
}

public function getCount(): int
Expand Down Expand Up @@ -99,25 +92,14 @@ public function sort(Sorting $sorting): IDataSource
);
}
} else {
if (!$this->dataSource instanceof $this->dbalCollectionClass) {
throw new UnexpectedValueException(
sprintf(
'Expeting %s, got %s',
$this->dbalCollectionClass,
$this->dataSource::class
)
);
}

/**
* Has the statement already a order by clause?
*/
$order = $this->dataSource->getQueryBuilder()->getClause('order');
if ($this->dataSource instanceof DbalCollection) {
$order = $this->dataSource->getQueryBuilder()->getClause('order');

if (ArraysHelper::testEmpty($order)) {
$this->dataSource = $this->dataSource->orderBy(
$this->prepareColumn($this->primaryKey)
);
if (ArraysHelper::testEmpty($order)) {
$this->dataSource = $this->dataSource->orderBy(
$this->prepareColumn($this->primaryKey)
);
}
}
}

Expand Down Expand Up @@ -208,72 +190,29 @@ protected function applyFilterRange(FilterRange $filter): void

protected function applyFilterText(FilterText $filter): void
{
// native handling with LikeFunction in v4
if (class_exists(LikeExpression::class)) {
$conditions = [
ICollection::OR,
];
$conditions = [
ICollection::OR,
];

foreach ($filter->getCondition() as $column => $value) {
$preparedColumn = $this->prepareColumn($column);
foreach ($filter->getCondition() as $column => $value) {
$preparedColumn = $this->prepareColumn($column);

if ($filter->isExactSearch()) {
$conditions[] = [
$preparedColumn => $value,
];
} else {
$words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);

if ($filter->isExactSearch()) {
foreach ($words as $word) {
$conditions[] = [
$preparedColumn => $value,
$preparedColumn . '~' => LikeExpression::contains($word),
];
} else {
$words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);

foreach ($words as $word) {
$conditions[] = [
$preparedColumn . '~' => LikeExpression::contains($word),
];
}
}
}

$this->dataSource = $this->dataSource->findBy($conditions);

return;
}

$condition = $filter->getCondition();
$expr = '(';
$params = [];

foreach ($condition as $column => $value) {
if ($filter->isExactSearch()) {
$expr .= '%column = %s OR ';
$params[] = $column;
$params[] = sprintf('%s', $value);

continue;
}

$words = $filter->hasSplitWordsSearch() === false ? [$value] : explode(' ', $value);

foreach ($words as $word) {
$expr .= '%column LIKE %s OR ';
$params[] = $column;
$params[] = sprintf('%%%s%%', $word);
}
}

$expr = preg_replace('/ OR $/', ')', $expr);

array_unshift($params, $expr);

if (!$this->dataSource instanceof $this->dbalCollectionClass) {
throw new UnexpectedValueException(
sprintf(
'Expeting %s, got %s',
$this->dbalCollectionClass,
$this->dataSource::class
)
);
}

$this->dataSource->getQueryBuilder()->andWhere(...$params);
$this->dataSource = $this->dataSource->findBy($conditions);
}

protected function applyFilterMultiSelect(FilterMultiSelect $filter): void
Expand All @@ -299,12 +238,7 @@ protected function getDataSource(): ICollection
private function prepareColumn(string $column): string
{
if (str_contains($column, '.')) {
// 'this->' is deprecated in v4
$prefix = $this->dbalCollectionClass === DbalCollection::class
? ''
: 'this->';

return $prefix . str_replace('.', '->', $column);
return str_replace('.', '->', $column);
}

return $column;
Expand Down

0 comments on commit 0139a06

Please sign in to comment.