| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm\Behavior; |
| 4 |
|
| 5 |
use ipl\Orm\Contract\PropertyBehavior; |
| 6 |
use ipl\Orm\Contract\QueryAwareBehavior; |
| 7 |
use ipl\Orm\Contract\RewriteFilterBehavior; |
| 8 |
use ipl\Orm\Exception\ValueConversionException; |
| 9 |
use ipl\Orm\Query; |
| 10 |
use ipl\Sql\Adapter\Pgsql; |
| 11 |
use ipl\Stdlib\Filter\Condition; |
| 12 |
use UnexpectedValueException; |
| 13 |
|
| 14 |
/** |
| 15 |
* Support hex filters for binary columns and PHP resource (in) / bytea hex format (out) transformation for PostgreSQL |
| 16 |
*/ |
| 17 |
class Binary extends PropertyBehavior implements QueryAwareBehavior, RewriteFilterBehavior |
| 18 |
{ |
| 19 |
/** @var bool Whether the query is using a pgsql adapter */ |
| 20 |
protected bool $isPostgres = true; |
| 21 |
|
| 22 |
public function fromDb($value, $key, $_) |
| 23 |
{ |
| 24 |
if (! $this->isPostgres) { |
| 25 |
return $value; |
| 26 |
} |
| 27 |
|
| 28 |
if ($value !== null) { |
| 29 |
if (is_resource($value)) { |
| 30 |
$content = stream_get_contents($value); |
| 31 |
rewind($value); |
| 32 |
|
| 33 |
return $content; |
| 34 |
} |
| 35 |
|
| 36 |
return $value; |
| 37 |
} |
| 38 |
|
| 39 |
return null; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @throws ValueConversionException If value is a resource |
| 44 |
*/ |
| 45 |
public function toDb($value, $key, $_) |
| 46 |
{ |
| 47 |
if (! $this->isPostgres) { |
| 48 |
return $value; |
| 49 |
} |
| 50 |
|
| 51 |
if (is_resource($value)) { |
| 52 |
throw new ValueConversionException(sprintf('Unexpected resource for %s', $key)); |
| 53 |
} |
| 54 |
|
| 55 |
if ($value === '*') { |
| 56 |
/** |
| 57 |
* Support IS (NOT) NULL filter transformation. |
| 58 |
* {@see \ipl\Sql\Compat\FilterProcessor::assemblePredicate()} |
| 59 |
*/ |
| 60 |
return $value; |
| 61 |
} |
| 62 |
|
| 63 |
return sprintf('\\x%s', bin2hex($value)); |
| 64 |
} |
| 65 |
|
| 66 |
public function setQuery(Query $query) |
| 67 |
{ |
| 68 |
$this->isPostgres = $query->getDb()->getAdapter() instanceof Pgsql; |
| 69 |
|
| 70 |
return $this; |
| 71 |
} |
| 72 |
|
| 73 |
public function rewriteCondition(Condition $condition, $relation = null) |
| 74 |
{ |
| 75 |
/** |
| 76 |
* TODO(lippserd): Duplicate code because {@see RewriteFilterBehavior}s come after {@see PropertyBehavior}s. |
| 77 |
* {@see \ipl\Orm\Compat\FilterProcessor::requireAndResolveFilterColumns()} |
| 78 |
*/ |
| 79 |
$column = $condition->metaData()->get('columnName'); |
| 80 |
if ($column !== null && isset($this->properties[$column])) { |
| 81 |
$value = $condition->metaData()->get('originalValue'); |
| 82 |
|
| 83 |
if ($this->isPostgres && is_resource($value)) { |
| 84 |
throw new UnexpectedValueException(sprintf('Unexpected resource for %s', $column)); |
| 85 |
} |
| 86 |
|
| 87 |
// ctype_xdigit expects strings. |
| 88 |
$value = (string) $value; |
| 89 |
/** |
| 90 |
* Although this code path is also affected by the duplicate behavior evaluation stated in {@see toDb()}, |
| 91 |
* no further adjustments are needed as ctype_xdigit returns false for binary and bytea hex strings. |
| 92 |
*/ |
| 93 |
if (ctype_xdigit($value)) { |
| 94 |
if (! $this->isPostgres) { |
| 95 |
$condition->setValue(hex2bin($value)); |
| 96 |
} elseif (substr($value, 0, 2) !== '\\x') { |
| 97 |
$condition->setValue(sprintf('\\x%s', $value)); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |