Passed
Push — master ( 9b561d...08d227 )
by y
01:25
created

AbstractTable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Helix\DB;
4
5
use ArrayAccess;
6
use Closure;
7
use Exception;
8
use Helix\DB;
9
10
/**
11
 * Uses `ArrayAccess` to produce {@link Column} instances.
12
 */
13
abstract class AbstractTable implements ArrayAccess {
14
15
    /**
16
     * Prepared query cache.
17
     *
18
     * @var Statement[]
19
     */
20
    protected $_cache = [];
21
22
    /**
23
     * @var DB
24
     */
25
    protected $db;
26
27
    /**
28
     * @param DB $db
29
     */
30
    public function __construct (DB $db) {
31
        $this->db = $db;
32
    }
33
34
    /**
35
     * Returns the SQL reference qualifier (i.e. the table name)
36
     *
37
     * @return string
38
     */
39
    abstract public function __toString ();
40
41
    /**
42
     * Caches a prepared query.
43
     *
44
     * @param string $key
45
     * @param Closure $prepare `():Statement`
46
     * @return Statement
47
     */
48
    protected function cache (string $key, Closure $prepare): Statement {
49
        return $this->_cache[$key] ?? $this->_cache[$key] = $prepare->__invoke();
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return bool
55
     */
56
    abstract public function offsetExists ($name): bool;
57
58
    /**
59
     * @param string $name
60
     * @return Column
61
     */
62
    abstract public function offsetGet ($name): Column;
63
64
    /**
65
     * Throws.
66
     *
67
     * @param void $name
68
     * @param void $value
69
     * @throws Exception
70
     */
71
    final public function offsetSet ($name, $value): void {
72
        throw new Exception('Tables are immutable.');
73
    }
74
75
    /**
76
     * Throws.
77
     *
78
     * @param void $name
79
     * @throws Exception
80
     */
81
    final public function offsetUnset ($name): void {
82
        $this->offsetSet($name, null);
83
    }
84
}