|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Source\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Source\Core\Model; |
|
6
|
|
|
use Source\Core\Connect; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @package Source\Models |
|
10
|
|
|
*/ |
|
11
|
|
|
class Phone extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Phone constructor. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct("phone", ["id"], ["user_id", "phone_type_id", "number"]); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param int $user_id |
|
23
|
|
|
* @param int $phone_type_id |
|
24
|
|
|
* @param int $phone_type_id |
|
25
|
|
|
* @return Phone |
|
26
|
|
|
*/ |
|
27
|
|
|
public function bootstrap(int $user_id, int $phone_type_id, int $number): Phone |
|
28
|
|
|
{ |
|
29
|
|
|
$this->user_id = $user_id; |
|
|
|
|
|
|
30
|
|
|
$this->phone_type_id = $phone_type_id; |
|
|
|
|
|
|
31
|
|
|
$this->number = $number; |
|
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $userId |
|
37
|
|
|
* @return null|Phone |
|
38
|
|
|
*/ |
|
39
|
|
|
public function findByUserId(string $userId) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
$stmt = Connect::getInstance()->prepare("SELECT phone.id, phone.number, phone_type.name FROM redstore.phone INNER JOIN redstore.phone_type ON phone_type.id = phone.phone_type_id WHERE phone.user_id = :userId;"); |
|
43
|
|
|
$stmt->bindValue(":userId", $userId, \PDO::PARAM_INT); |
|
44
|
|
|
$stmt->execute(); |
|
45
|
|
|
|
|
46
|
|
|
if (!$stmt->rowCount()) { |
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $stmt->fetchAll(); |
|
51
|
|
|
} catch (\PDOException $exception) { |
|
52
|
|
|
$this->fail = $exception; |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public function save(): bool |
|
61
|
|
|
{ |
|
62
|
|
|
if (!$this->required()) { |
|
63
|
|
|
$this->error = "Faltam campos obrigatórios!"; |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** Phone Update */ |
|
68
|
|
|
if (!empty($this->id)) { |
|
|
|
|
|
|
69
|
|
|
$phoneId = $this->id; |
|
70
|
|
|
|
|
71
|
|
|
$this->update($this->safe(), "id = :id", "id={$phoneId}"); |
|
72
|
|
|
if ($this->fail()) { |
|
73
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** Phone Create */ |
|
79
|
|
|
if (empty($this->id)) { |
|
80
|
|
|
$phoneId = $this->create($this->safe()); |
|
81
|
|
|
if ($this->fail()) { |
|
82
|
|
|
$this->error = "Erro ao cadastrar, verifique os dados"; |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->data = ($this->findById($phoneId))->data(); |
|
|
|
|
|
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|