forked from etsy/411
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDBArray.php
More file actions
66 lines (59 loc) · 1.71 KB
/
Copy pathDBArray.php
File metadata and controls
66 lines (59 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace FOO;
/**
* Class DBArray
* Array values backed by the DB.
* @package FOO
*/
class DBArray implements \ArrayAccess {
/** @var string Database table name for this class. */
public static $TABLE = '';
/** @var int Site ID of this array. */
protected $site_id = 0;
/**
* Constructor
*/
public function __construct() {
$this->site_id = SiteFinder::getCurrentId();
}
/**
* ArrayAccess interface
* @param mixed $key
* @return mixed
* @throws DBException
*/
public function offsetExists($key) {
return DB::query(sprintf('SELECT COUNT(*) FROM `%s` WHERE `site_id` = ? AND `key` = ?', static::$TABLE), [$this->site_id, $key], DB::VAL);
}
/**
* ArrayAccess interface
* @param mixed $key
* @return int|mixed
* @throws DBException
*/
public function offsetGet($key) {
$value = DB::query(sprintf('SELECT `value` FROM `%s` WHERE `site_id` = ? AND `key` = ?', static::$TABLE), [$this->site_id, $key], DB::VAL);
if(ctype_digit($value)) {
$value = (int) $value;
}
return $value;
}
/**
* ArrayAccess interface
* @param mixed $key
* @param mixed $value
* @return mixed
* @throws DBException
*/
public function offsetSet($key, $value) {
return DB::query(sprintf('REPLACE INTO `%s` VALUES (?, ?, ?)', static::$TABLE), [$this->site_id, $key, $value], DB::CNT);
}
/**
* ArrayAccess interface
* @param mixed $key
* @throws DBException
*/
public function offsetUnset($key) {
DB::query(sprintf('DELETE FROM `%s` WHERE `site_id` = ? AND `key` = ?', static::$TABLE), [$this->site_id, $key]);
}
}