forked from tommyrot/superseriousstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.php
65 lines (57 loc) · 1.38 KB
/
common.php
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
<?php declare(strict_types=1);
/**
* Copyright (c) 2010-2022, Jos de Ruijter <jos@dutnie.nl>
*/
/**
* Trait with common functions.
*/
trait common
{
public function add_int(string $var, int $value): void
{
$this->$var += $value;
}
/**
* Retrieve and apply given variables from $table after casting their values to
* the appropriate type. $table can be either "settings" or "parse_state".
*/
private function apply_vars(string $table, array $vars): void
{
foreach ($vars as $var) {
if (is_null($value = db::query_single_col('SELECT value FROM '.$table.' WHERE var = \''.$var.'\''))) {
continue;
}
if (is_string($this->$var)) {
$this->$var = $value;
} elseif (is_int($this->$var)) {
if (preg_match('/^\d+$/', $value)) {
$this->$var = (int) $value;
}
} elseif (is_bool($this->$var)) {
if (preg_match('/^true$/i', $value)) {
$this->$var = true;
} elseif (preg_match('/^false$/i', $value)) {
$this->$var = false;
}
} elseif (is_array($this->$var)) {
$this->$var = unserialize($value);
}
}
}
public function get_int(string $var): int
{
return $this->$var;
}
public function get_string(string $var): string
{
return $this->$var;
}
public function set_int(string $var, int $value): void
{
$this->$var = $value;
}
public function set_string(string $var, string $value): void
{
$this->$var = $value;
}
}