Passed
Pull Request — master (#15)
by Todd
02:41
created

Identifier   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 63.64%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 35
rs 10
c 1
b 0
f 0
ccs 7
cts 11
cp 0.6364
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 2 1
A __construct() 0 8 4
A escape() 0 2 1
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 6
    public function __construct(...$identifier) {
25 6
        if (empty($identifier) || empty($identifier[0])) {
26
            throw new \InvalidArgumentException("The identifier is empty.", 500);
27
        }
28 6
        if (count($identifier) === 1) {
29 6
            $this->parts = explode('.', $identifier[0]);
30
        } else {
31
            $this->parts = $identifier;
32
        }
33 6
    }
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 6
    public function escape(Db $db) {
51 6
        return implode('.', array_map([$db, 'escape'], $this->parts));
52
    }
53
}
54