-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactore DatabaseUtil::createWhereForSerializedBlob()
- Loading branch information
Showing
10 changed files
with
120 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
src/Util/DatabaseUtil/CreateWhereForSerializedBlobResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters