/
DB.class.php
52 lines (42 loc) · 1.13 KB
/
DB.class.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
<?php
class DB
{
private $path;
private $data;
public function __construct($path)
{
$this->path = $path;
$this->data = unserialize(@file_get_contents($this->path));
}
public function getOneUserByUsername($username)
{
$cleanedUsername = trim($username);
if (!$this->data || !array_key_exists($cleanedUsername, $this->data))
{
return null;
}
$userData = $this->data[$cleanedUsername];
return new User($cleanedUsername,
$userData['password'],
$userData['access_token'],
$userData['access_token_secret']);
}
public function addUser(User $user)
{
$this->data[$user->getUsername()] = array(
'password' => $user->getPassword(),
'access_token' => $user->getAccessToken(),
'access_token_secret' => $user->getAccessTokenSecret()
);
return $this;
}
public function save()
{
$retValue = file_put_contents($this->path, serialize($this->data));
if ($retValue === false)
{
throw new DBException('Save failed');
}
return $retValue;
}
}