summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Behavior/Binary.php
blob: 98a4bf49e0d39ba4a6641eae25d4fd7090d36fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

namespace ipl\Orm\Behavior;

use ipl\Orm\Contract\PropertyBehavior;
use ipl\Orm\Contract\QueryAwareBehavior;
use ipl\Orm\Contract\RewriteFilterBehavior;
use ipl\Orm\Query;
use ipl\Sql\Adapter\Pgsql;
use ipl\Stdlib\Filter\Condition;
use UnexpectedValueException;

use function ipl\Stdlib\get_php_type;

/**
 * Support hex filters for binary columns and PHP resource (in) / bytea hex format (out) transformation for PostgreSQL
 */
class Binary extends PropertyBehavior implements QueryAwareBehavior, RewriteFilterBehavior
{
    /**
     * Properties for {@see rewriteCondition()} to support hex filters for each adapter
     *
     * Set in {@see setQuery()} from the {@see $properties} array because the latter is reset for adapters other than
     * {@see Pgsql}, so {@see fromDb()} and {@see toDb()} don't run for them.
     *
     * @var array
     */
    protected $rewriteSubjects;

    public function fromDb($value, $key, $_)
    {
        if ($value !== null) {
            if (is_resource($value)) {
                return stream_get_contents($value);
            }

            return $value;
        }

        return null;
    }

    /**
     * @throws UnexpectedValueException If value is a resource
     */
    public function toDb($value, $key, $_)
    {
        if (is_resource($value)) {
            throw new UnexpectedValueException(sprintf('Unexpected resource for %s', $key));
        }

        if ($value === '*') {
            /**
             * Support IS (NOT) NULL filter transformation.
             * {@see \ipl\Sql\Compat\FilterProcessor::assemblePredicate()}
             */
            return $value;
        }

        /**
         * TODO(lippserd): If the filter is moved to a subquery, the value has already been processed.
         * This is because our filter processor is unfortunately doing the transformation twice at the moment:
         *
         * {@link https://github.com/Icinga/ipl-orm/issues/48}
         *
         * {@see \ipl\Orm\Compat\FilterProcessor::requireAndResolveFilterColumns()}
         */
        if (substr($value, 0, 2) === '\\x') {
            return $value;
        }

        return sprintf('\\x%s', bin2hex($value));
    }

    public function setQuery(Query $query)
    {
        $this->rewriteSubjects = $this->properties;

        if (! $query->getDb()->getAdapter() instanceof Pgsql) {
            // Only process properties if the adapter is PostgreSQL.
            $this->properties = [];
        }
    }

    public function rewriteCondition(Condition $condition, $relation = null)
    {
        /**
         * TODO(lippserd): Duplicate code because {@see RewriteFilterBehavior}s come after {@see PropertyBehavior}s.
         * {@see \ipl\Orm\Compat\FilterProcessor::requireAndResolveFilterColumns()}
         */
        $column = $condition->metaData()->get('columnName');
        if (isset($this->rewriteSubjects[$column])) {
            $value = $condition->getValue();

            if (empty($this->properties) && is_resource($value)) {
                // Only for PostgreSQL.
                throw new UnexpectedValueException(sprintf('Unexpected resource for %s', $column));
            }

            // ctype_xdigit expects strings.
            $value = (string) $value;
            /**
             * Although this code path is also affected by the duplicate behavior evaluation stated in {@see toDb()},
             * no further adjustments are needed as ctype_xdigit returns false for binary and bytea hex strings.
             */
            if (ctype_xdigit($value)) {
                if (empty($this->properties) && substr($value, 0, 2) !== '\\x') {
                    // Only for PostgreSQL.
                    $condition->setValue(sprintf('\\x%s', $value));
                } else {
                    $condition->setValue(hex2bin($value));
                }
            }
        }
    }
}