Literal::value()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2014 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Db;
9
10
/**
11
 * Represents an string that won't be escaped in queries.
12
 */
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
    public function __construct($value) {
30
        if (is_string($value)) {
31
            $this->driverValues['default'] = $value;
32
        } elseif (is_array($value)) {
33
            foreach ($value as $key => $value) {
34
                $this->driverValues[$this->normalizeKey($key)] = $value;
35
            }
36
        }
37
    }
38
39
    /**
40
     * Get the literal value.
41
     *
42
     * @param string $driver The name of the database driver to fetch the literal for.
43
     * @return string Returns the value for the specific driver, the default literal, or "null" if there is no default.
44
     */
45
    public function getValue($driver = 'default') {
46
        $driver = $this->normalizeKey($driver);
47
48
        if (isset($this->driverValues[$driver])) {
49
            return $this->driverValues[$driver];
50
        } elseif (isset($this->driverValues['default'])) {
51
            return $this->driverValues['default'];
52
        } else {
53
            return 'null';
54
        }
55
    }
56
57
    /**
58
     * Set the literal value.
59
     *
60
     * @param string $value The new value to set.
61
     * @param string $driver The name of the database driver to set the value for.
62
     * @return Literal Returns $this for fluent calls.
63
     */
64
    public function setValue($value, $driver = 'default') {
65
        $driver = $this->normalizeKey($driver);
66
        $this->driverValues[$driver] = $value;
67
        return $this;
68
    }
69
70
    /**
71
     * Normalize the driver name for the drivers array.
72
     *
73
     * @param string $key The name of the driver.
74
     * @return string Returns the driver name normalized.
75
     */
76
    protected function normalizeKey($key) {
77
        return rtrim_substr(strtolower(basename($key)), 'db');
78
    }
79
80
    /**
81
     * Create and return a {@link Literal} object.
82
     *
83
     * @param string|array $value The literal value(s) as passed to {@link Literal::__construct()}.
84
     * @return Literal Thew new literal value.
85
     */
86
    public static function value($value) {
87
        $literal = new Literal($value);
88
        return $literal;
89
    }
90
91
    /**
92
     * Creat and return a {@link Literal} object that will query the current unix timesatmp.
93
     *
94
     * @return Literal Returns the timestamp expression.
95
     */
96
    public static function timestamp() {
97
        $literal = new Literal([
98
            'mysql' => 'unix_timestamp()',
99
            'sqlite' => "date('now', 'unixepoch')",
100
            'posgresql' => 'extract(epoch from now())',
101
            'default' => time()
102
        ]);
103
        return $literal;
104
    }
105
}
106