1 | <?php |
||
13 | class Literal { |
||
14 | /// Properties /// |
||
15 | |||
16 | /** |
||
17 | * @var array An array that maps driver names to literals. |
||
18 | */ |
||
19 | protected $driverValues = []; |
||
20 | |||
21 | /// Methods /// |
||
22 | |||
23 | /** |
||
24 | * Initialize an instance of the {@link Literal} class. |
||
25 | * |
||
26 | * @param string|array $value Either a string default value or an array in the form |
||
27 | * `['driver' => 'literal']` to specify different literals for different database drivers. |
||
28 | */ |
||
29 | 44 | public function __construct($value) { |
|
30 | 44 | if (is_string($value)) { |
|
31 | 44 | $this->driverValues['default'] = $value; |
|
32 | 44 | } elseif (is_array($value)) { |
|
33 | foreach ($value as $key => $value) { |
||
34 | $this->driverValues[$this->driverKey($key)] = $value; |
||
35 | } |
||
36 | } |
||
37 | 44 | } |
|
38 | |||
39 | /** |
||
40 | * Get the literal value. |
||
41 | * |
||
42 | * @param Db $db The database driver getting the value. |
||
43 | * @param array ...$args Arguments to pass into the literal. This uses **sprintf()** so arguments must already be escaped. |
||
44 | * @return string Returns the value for the specific driver, the default literal, or "null" if there is no default. |
||
45 | */ |
||
46 | 44 | public function getValue(Db $db, ...$args) { |
|
57 | |||
58 | /** |
||
59 | * Set the literal value. |
||
60 | * |
||
61 | * @param string $value The new value to set. |
||
62 | * @param string|object $driver The name of the database driver to set the value for. |
||
63 | * @return Literal Returns $this for fluent calls. |
||
64 | */ |
||
65 | public function setValue($value, $driver = 'default') { |
||
70 | |||
71 | /** |
||
72 | * Normalize the driver name for the drivers array. |
||
73 | * |
||
74 | * @param string|object $key The name of the driver or an instance of a database driver. |
||
75 | * @return string Returns the driver name normalized. |
||
76 | */ |
||
77 | 44 | protected function driverKey($key) { |
|
78 | 44 | if (is_object($key)) { |
|
79 | 44 | $key = get_class($key); |
|
80 | 44 | } |
|
81 | 44 | $key = strtolower(basename($key)); |
|
82 | |||
83 | 44 | if (preg_match('`([az]+)(Db)?$`', $key, $m)) { |
|
84 | $key = $m; |
||
85 | } |
||
86 | 44 | return $key; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Create and return a {@link Literal} object. |
||
91 | * |
||
92 | * @param string|array $value The literal value(s) as passed to {@link Literal::__construct()}. |
||
93 | * @return Literal Thew new literal value. |
||
94 | */ |
||
95 | public static function value($value) { |
||
99 | |||
100 | /** |
||
101 | * Create and return a {@link Literal} object that will query the current unix timesatmp. |
||
102 | * |
||
103 | * @return Literal Returns the timestamp expression. |
||
104 | */ |
||
105 | public static function timestamp() { |
||
114 | } |
||
115 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.