Identifier::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
/**
3
 * @author Todd Burry <[email protected]>
4
 * @copyright 2009-2017 Vanilla Forums Inc.
5
 * @license MIT
6
 */
7
8
namespace Garden\Db;
9
10
/**
11
 * Contains a database identifier string such as a table name or a column name.
12
 *
13
 * The database object is meant to take plain database strings for tables. However, when it takes
14
 * such a string it will always prepend the database prefix to the table name. If you want to query a
15
 * table without the prefix then wrap it in this class.
16
 */
17
class Identifier {
18
    private $parts;
19
20
    /**
21
     * Identifier constructor.
22
     * @param string ...$identifier
23
     */
24 15
    public function __construct(...$identifier) {
25 15
        if (empty($identifier) || empty($identifier[0])) {
26
            throw new \InvalidArgumentException("The identifier is empty.", 500);
27
        }
28 15
        if (count($identifier) === 1) {
29 6
            $this->parts = explode('.', $identifier[0]);
30
        } else {
31 9
            $this->parts = $identifier;
32
        }
33 15
    }
34
35
    /**
36
     * Convert the identifier to a simple string.
37
     *
38
     * @return string Returns the identifier as a string.
39
     */
40
    public function __toString() {
41
        return implode('.', $this->parts);
42
    }
43
44
    /**
45
     * Escape the identifier.
46
     *
47
     * @param Db $db The database used to escape the identifier.
48
     * @return string Returns the full escaped identifier.
49
     */
50 15
    public function escape(Db $db) {
51 15
        return implode('.', array_map([$db, 'escape'], $this->parts));
52
    }
53
}
54