Issues (590)

src/Query/Compiler/AbstractCompiler.php (1 issue)

1
<?php
2
3
namespace Bdf\Prime\Query\Compiler;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Exception\PrimeException;
7
use Bdf\Prime\Platform\PlatformInterface;
8
9
/**
10
 * Base class for create compilers
11
 *
12
 * - Implements doCompile* methods for doing compilation, without take care of side effects
13
 * - Check $this->isCompiling() on reset() method
14
 * - Use preprocessor->field() for compiling update and insert values, or aggregation projection, group, order columns
15
 * - For compile projection (SELECT columns), use preprocessor->root() for select all columns
16
 * - preprocessor->table() for register new tables / relations (FROM & JOIN)
17
 * - Use preprocessor->expression() for compile filter (WHERE, ON, HAVING) expression
18
 *
19
 * @template Q as \Bdf\Prime\Query\CompilableClause&\Bdf\Prime\Query\Contract\Compilable
20
 * @template C as ConnectionInterface
21
 *
22
 * @implements CompilerInterface<Q>
23
 */
24
abstract class AbstractCompiler implements CompilerInterface
25
{
26
    /** @use InsertCompilerTrait<Q> */
27
    use InsertCompilerTrait;
28
29
    /** @use UpdateCompilerTrait<Q> */
30
    use UpdateCompilerTrait;
31
32
    /** @use DeleteCompilerTrait<Q> */
33
    use DeleteCompilerTrait;
34
35
    /** @use SelectCompilerTrait<Q> */
36
    use SelectCompilerTrait;
37
38
    /**
39
     * The connection platform
40
     *
41
     * @var C
42
     */
43
    protected ConnectionInterface $connection;
44
45
46
    /**
47
     * AbstractCompiler constructor.
48
     *
49
     * @param C $connection
50
     */
51 501
    public function __construct(ConnectionInterface $connection)
52
    {
53 501
        $this->connection = $connection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $connection of type Bdf\Prime\Connection\ConnectionInterface is incompatible with the declared type Bdf\Prime\Query\Compiler\C of property $connection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 951
    public function platform(): PlatformInterface
60
    {
61 951
        return $this->connection->platform();
62
    }
63
64
    /**
65
     * Try to resolve type and auto convert value
66
     *
67
     * @param mixed $value
68
     *
69
     * @return mixed
70
     * @throws PrimeException
71
     */
72 3
    protected function autoConvertValue($value)
73
    {
74 3
        if ($value === null) {
75 1
            return null;
76
        }
77
78 3
        return $this->platform()->types()->toDatabase($value);
79
    }
80
81
    /**
82
     * Try to resolve type and auto convert values.
83
     *
84
     * @param mixed $values If is array, convert each values, else convert the value
85
     *
86
     * @return mixed
87
     * @throws PrimeException
88
     *
89
     * @see AbstractCompiler::autoConvertValue()
90
     */
91
    protected function autoConvertValues($values)
92
    {
93
        if (!is_array($values)) {
94
            return $this->autoConvertValue($values);
95
        }
96
97
        foreach ($values as &$e) {
98
            $e = $this->autoConvertValue($e);
99
        }
100
101
        return $values;
102
    }
103
}
104