1 | <?php |
||
2 | |||
3 | namespace User; |
||
4 | |||
5 | use PDO; |
||
6 | |||
7 | class meta |
||
8 | { |
||
9 | /** |
||
10 | * PDO instance. |
||
11 | * |
||
12 | * @var \DB\pdo |
||
13 | */ |
||
14 | private $pdo; |
||
15 | |||
16 | public function __construct(\DB\pdo $pdo) |
||
17 | { |
||
18 | $this->pdo = $pdo; |
||
19 | $check = $pdo->check_table('usermeta'); |
||
20 | if (!$check) { |
||
21 | $sql = \Filemanager\file::get(__DIR__ . '/meta.sql'); |
||
22 | $this->pdo->query($sql)->exec(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
23 | } |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * Insert new usermeta. |
||
28 | * |
||
29 | * @param mixed $other |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | public function insert(int $userid, string $key, array $value, $other = null) |
||
34 | { |
||
35 | $json = json_encode($value); |
||
36 | if (null !== $other) { |
||
37 | if (\ArrayHelper\helper::is_iterable($other)) { |
||
38 | $other = json_encode($other); |
||
39 | } elseif (is_bool($other)) { |
||
40 | settype($other, 'integer'); |
||
41 | } |
||
42 | } |
||
43 | $sql = "INSERT INTO `usermeta` (`uid`, `key`, `value`, `other`) VALUES ('$userid', '$key', '$json', $other);"; |
||
44 | |||
45 | return $this->pdo->query($sql)->exec(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get user meta. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function get(int $userid, string $key) |
||
54 | { |
||
55 | $get = $this->pdo->select('usermeta')->where(['uid' => $userid, 'key' => $key])->row_array(); |
||
56 | if ($get && !empty($get) && \ArrayHelper\helper::isAssoc($get)) { |
||
57 | if (isset($get['value'])) { |
||
58 | if (is_json($get['value'])) { |
||
59 | $get['value'] = json_decode($get['value'], true); |
||
60 | } |
||
61 | } |
||
62 | if (isset($get['other'])) { |
||
63 | if (is_json($get['other'])) { |
||
64 | $get['other'] = json_decode($get['other'], true); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | return $get; |
||
70 | } |
||
71 | } |
||
72 |