-
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.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 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 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,19 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Util; | ||
|
||
use HeimrichHannot\UtilsBundle\Util\DatabaseUtil\CreateWhereForSerializedBlobResult; | ||
|
||
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); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
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,68 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Util\DatabaseUtil; | ||
|
||
class CreateWhereForSerializedBlobResult | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public $field; | ||
/** | ||
* @var array | ||
*/ | ||
public $values; | ||
|
||
public function __construct( | ||
string $field, | ||
array $values | ||
) { | ||
$this->field = $field; | ||
$this->values = $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