Skip to content

Commit

Permalink
refactore DatabaseUtil::createWhereForSerializedBlob()
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Oct 20, 2023
1 parent f376c09 commit 5fa1bef
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/EntityFinder/EntityFinderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public function __construct(
*/
public function findModulesByTypeAndSerializedValue(string $type, string $field, array $values): ?Collection
{
['columns' => $columns, 'values' => $values] = $this->utils->database()
->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
$blobQuery = $this->utils->database()->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
$columns = [$blobQuery->createOrWhere()];
$values = $blobQuery->values;

$columns = [$columns];
$columns[] = ModuleModel::getTable().'.type=?';
$values[] = $type;

Expand Down
55 changes: 0 additions & 55 deletions src/Util/Database/DatabaseUtil.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Util/DatabaseUtil/CreateWhereForSerializedBlobResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\DatabaseUtil;

class CreateWhereForSerializedBlobResult
{
public function __construct(
public readonly string $field,
public readonly array $values
) {
}

/**
* Return the where query with AND operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)').
*/
public function createInlineAndWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', $this->getValueList()).'))';
}

/**
* Return the where query with OR operation for each value. Values are inlined in the query ('REGEXP (':"3"')' instead of 'REGEXP (?)').
*/
public function createInlineOrWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', $this->getValueList()).'))';
}

/**
* Return the where query with AND operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property.
*/
public function createAndWhere(): string
{

return '('.$this->field.' REGEXP ('.implode(") AND ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))';
}

/**
* Return the where query with OR operation and placeholder for each value ('REGEXP (?)'). Values can be obtained from the values property.
*/
public function createOrWhere(): string
{
return '('.$this->field.' REGEXP ('.implode(") OR ".$this->field.' REGEXP (', array_fill(0, count($this->getValueList()), '?')).'))';
}

private function getValueList(): array
{
$returnValues = [];

foreach ($this->values as $val) {
$returnValues[] = ":\"$val\"";
}

return $returnValues;
}

}
17 changes: 17 additions & 0 deletions src/Util/DatabaseUtil/DatabaseUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace HeimrichHannot\UtilsBundle\Util\DatabaseUtil;

class DatabaseUtil
{
/**
* Create a where condition for a field that contains a serialized blob.
*
* @param string $field A field containing a serialized array.
* @param array $values The values that should be searched for in the field.
*/
public function createWhereForSerializedBlob(string $field, array $values): CreateWhereForSerializedBlobResult
{
return new CreateWhereForSerializedBlobResult($field, $values);
}
}
7 changes: 4 additions & 3 deletions src/Util/User/UserUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Contao\Model\Collection;
use Contao\StringUtil;
use Contao\UserModel;
use HeimrichHannot\UtilsBundle\Util\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Model\ModelUtil;

class UserUtil
Expand Down Expand Up @@ -65,8 +65,9 @@ public function findActiveUsersByGroup(array $groups, string $type = self::TYPE_
$columns = ["($table.start='' OR $table.start<='$time') AND ($table.stop='' OR $table.stop>'".($time + 60)."') AND $table.disable=''"];
$columns[] = '';

[$columns[], $tmpValues] = $this->databaseUtil->createWhereForSerializedBlob('groups', $groups);
$values = array_merge(array_values($values), array_values($tmpValues));
$blobResult = $this->databaseUtil->createWhereForSerializedBlob('groups', $groups);
$columns[] = $blobResult->createOrWhere();
$values = array_merge(array_values($values), array_values($blobResult->values));

return $adapter->findBy($columns, $values, $options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\Data\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\File\FileUtil;
use HeimrichHannot\UtilsBundle\Util\Html\HtmlUtil;
Expand Down
7 changes: 5 additions & 2 deletions tests/EntityFinder/EntityFinderHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Contao\ModuleModel;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\EntityFinder\EntityFinderHelper;
use HeimrichHannot\UtilsBundle\Util\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\CreateWhereForSerializedBlobResult;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Utils;

class EntityFinderHelperTest extends ContaoTestCase
Expand All @@ -19,7 +20,9 @@ public function testFindModulesByTypeAndSerializedValue()
]);

$databaseUtilMock = $this->createMock(DatabaseUtil::class);
$databaseUtilMock->method('createWhereForSerializedBlob')->willReturn(['columns' => '', 'values' => []]);
$databaseUtilMock->method('createWhereForSerializedBlob')->willReturn(
new CreateWhereForSerializedBlobResult('field', [])
);
$utils = $this->createMock(Utils::class);
$utils->method('database')->willReturn($databaseUtilMock);

Expand Down
29 changes: 29 additions & 0 deletions tests/Util/DatabaseUtil/DatabaseUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace HeimrichHannot\UtilsBundle\tests\Util\DatabaseUtil;

use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\CreateWhereForSerializedBlobResult;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use PHPUnit\Framework\MockObject\MockBuilder;

class DatabaseUtilTest extends AbstractUtilsTestCase
{
public function getTestInstance(array $parameters = [], ?MockBuilder $mockBuilder = null)
{
return new DatabaseUtil();
}

public function testCreateWhereForSerializedBlob()
{
$result = $this->getTestInstance()->createWhereForSerializedBlob('elements', ['texts', 'headline']);
static::assertInstanceOf(CreateWhereForSerializedBlobResult::class, $result);
static::assertSame('(elements REGEXP (?) OR elements REGEXP (?))', $result->createOrWhere());
static::assertSame('(elements REGEXP (?) AND elements REGEXP (?))', $result->createAndWhere());
static::assertSame('(elements REGEXP (:"texts") OR elements REGEXP (:"headline"))', $result->createInlineOrWhere());
static::assertSame('(elements REGEXP (:"texts") AND elements REGEXP (:"headline"))', $result->createInlineAndWhere());
static::assertCount(2, $result->values);
}


}
5 changes: 3 additions & 2 deletions tests/Util/User/UserUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use Contao\UserModel;
use HeimrichHannot\TestUtilitiesBundle\Mock\ModelMockTrait;
use HeimrichHannot\UtilsBundle\Tests\AbstractUtilsTestCase;
use HeimrichHannot\UtilsBundle\Util\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\CreateWhereForSerializedBlobResult;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Model\ModelUtil;
use HeimrichHannot\UtilsBundle\Util\User\UserUtil;
use PHPUnit\Framework\MockObject\MockBuilder;
Expand Down Expand Up @@ -219,7 +220,7 @@ public function testFindActiveUsersByGroup()
]);
$parameters['databaseUtil'] = $this->createMock(DatabaseUtil::class);
$parameters['databaseUtil']->method('createWhereForSerializedBlob')->willReturnCallback(function (string $field, array $values) {
return [$field, $values];
return new CreateWhereForSerializedBlobResult($field, $values);
});

$instance = $this->getTestInstance($parameters);
Expand Down
2 changes: 1 addition & 1 deletion tests/Util/UtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\Util\Container\ContainerUtil;
use HeimrichHannot\UtilsBundle\Util\Data\AnonymizeUtil;
use HeimrichHannot\UtilsBundle\Util\Database\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\DatabaseUtil;
use HeimrichHannot\UtilsBundle\Util\Dca\DcaUtil;
use HeimrichHannot\UtilsBundle\Util\Html\HtmlUtil;
use HeimrichHannot\UtilsBundle\Util\Locale\LocaleUtil;
Expand Down

0 comments on commit 5fa1bef

Please sign in to comment.