|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Win\Authentication; |
|
4
|
|
|
|
|
5
|
|
|
use Win\DAO\DAO; |
|
6
|
|
|
use Win\Authentication\User; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* User DAO |
|
10
|
|
|
*/ |
|
11
|
|
|
class UserDAO extends DAO implements UserDAOInterface { |
|
12
|
|
|
|
|
13
|
|
|
const TABLE = 'user'; |
|
14
|
|
|
const ALIAS = 'Usuário'; |
|
15
|
|
|
|
|
16
|
|
|
/** @var User */ |
|
17
|
|
|
protected $obj; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param array $row |
|
21
|
|
|
* @return User |
|
22
|
|
|
*/ |
|
23
|
|
|
protected function mapObject($row) { |
|
24
|
|
|
$obj = new User(); |
|
25
|
|
|
$obj->setId($row['id']); |
|
26
|
|
|
$obj->setActive($row['is_active']); |
|
27
|
|
|
$obj->setAccessLevel($row['access_level']); |
|
28
|
|
|
$obj->setGroupId($row['group_id']); |
|
29
|
|
|
$obj->setName($row['name']); |
|
30
|
|
|
$obj->setEmail($row['email']); |
|
31
|
|
|
$obj->setPasswordHash($row['password_hash']); |
|
32
|
|
|
$obj->setRecoreryHash($row['recovery_hash']); |
|
33
|
|
|
$obj->setImage($row['image']); |
|
34
|
|
|
$obj->setLastLogin($row['last_login']); |
|
35
|
|
|
return $obj; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param User $obj |
|
40
|
|
|
* @return mixed[] |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function mapRow($obj) { |
|
43
|
|
|
$row['id'] = $obj->getId(); |
|
|
|
|
|
|
44
|
|
|
$row['is_active'] = $obj->isActive(); |
|
45
|
|
|
$row['access_level'] = $obj->getAccessLevel(); |
|
46
|
|
|
$row['group_id'] = $obj->getGroupId(); |
|
47
|
|
|
$row['name'] = $obj->getName(); |
|
48
|
|
|
$row['email'] = $obj->getEmail(); |
|
49
|
|
|
$row['password_hash'] = $obj->getPasswordHash(); |
|
50
|
|
|
$row['recovery_hash'] = $obj->getRecoreryHash(); |
|
|
|
|
|
|
51
|
|
|
$row['image'] = $obj->getImage(); |
|
|
|
|
|
|
52
|
|
|
$row['last_login'] = $obj->getLastLogin(); |
|
|
|
|
|
|
53
|
|
|
return $row; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Insere o primeiro usuario |
|
58
|
|
|
* @param string $email |
|
59
|
|
|
* @param string $password |
|
60
|
|
|
*/ |
|
61
|
|
|
public function insertFirst($email, $password) { |
|
62
|
|
|
$totalUser = $this->numRows(); |
|
63
|
|
|
|
|
64
|
|
|
if ($totalUser == 0) { |
|
65
|
|
|
$user = new User(); |
|
66
|
|
|
$user->setName('Administrador'); |
|
67
|
|
|
$user->setEmail($email); |
|
68
|
|
|
$user->setPassword($password); |
|
69
|
|
|
$user->setActive(true); |
|
70
|
|
|
$user->setAccessLevel(User::ACCESS_ADMIN); |
|
71
|
|
|
$this->save($user); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.