|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Source\Models\Address; |
|
4
|
|
|
|
|
5
|
|
|
use Source\Core\Model; |
|
6
|
|
|
use Source\Core\Connect; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @package Source\Models |
|
10
|
|
|
*/ |
|
11
|
|
|
class Address extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Address constructor. |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::__construct('address', ['id'], ['city_id', 'street', 'cep', 'number']); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param int $city_id |
|
23
|
|
|
* @param string $street |
|
24
|
|
|
* @param string $cep |
|
25
|
|
|
* @param int $number |
|
26
|
|
|
* @return Address |
|
27
|
|
|
*/ |
|
28
|
|
|
public function bootstrap(int $city_id, string $street, string $cep, int $number): Address |
|
29
|
|
|
{ |
|
30
|
|
|
$this->city_id = $city_id; |
|
|
|
|
|
|
31
|
|
|
$this->street = $street; |
|
|
|
|
|
|
32
|
|
|
$this->cep = $cep; |
|
|
|
|
|
|
33
|
|
|
$this->number = $number; |
|
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function bindAddress(int $userId, int $addressId) |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
$stmt = Connect::getInstance()->prepare("INSERT INTO redstore.user_address (user_id, address_id, created_by, created_at, updated_at) VALUES (:userId, :addressId, :userId2, NOW(), NOW());"); |
|
41
|
|
|
$stmt->bindValue(":userId", $userId, \PDO::PARAM_INT); |
|
42
|
|
|
$stmt->bindValue(":addressId", $addressId, \PDO::PARAM_INT); |
|
43
|
|
|
$stmt->bindValue(":userId2", $userId, \PDO::PARAM_INT); |
|
44
|
|
|
$stmt->execute(); |
|
45
|
|
|
|
|
46
|
|
|
return true; |
|
47
|
|
|
} catch (\PDOException $exception) { |
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public static function getAddressByUserId(int $userId) |
|
53
|
|
|
{ |
|
54
|
|
|
try { |
|
55
|
|
|
$stmt = Connect::getInstance()->prepare("SELECT redstore.address.* FROM redstore.address INNER JOIN redstore.user_address ON redstore.address.id = redstore.user_address.address_id WHERE redstore.user_address.user_id = :userId;"); |
|
56
|
|
|
$stmt->bindValue(":userId", $userId, \PDO::PARAM_INT); |
|
57
|
|
|
$stmt->execute(); |
|
58
|
|
|
|
|
59
|
|
|
if (!$stmt->rowCount()) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $stmt->fetchAll(); |
|
64
|
|
|
} catch (\PDOException $exception) { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static function getBindedAddress(int $userId, int $addressId) |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
|
|
$stmt = Connect::getInstance()->prepare("SELECT user_id, address_id FROM redstore.user_address WHERE user_id = :userId AND address_id = :addressId;"); |
|
73
|
|
|
$stmt->bindValue(":userId", $userId, \PDO::PARAM_INT); |
|
74
|
|
|
$stmt->bindValue(":addressId", $addressId, \PDO::PARAM_INT); |
|
75
|
|
|
$stmt->execute(); |
|
76
|
|
|
|
|
77
|
|
|
if (!$stmt->rowCount()) { |
|
78
|
|
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $stmt->fetchAll(); |
|
82
|
|
|
} catch (\PDOException $exception) { |
|
83
|
|
|
return null; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
|
|
public function save(): bool |
|
91
|
|
|
{ |
|
92
|
|
|
/** Address Update */ |
|
93
|
|
|
if (!empty($this->id)) { |
|
|
|
|
|
|
94
|
|
|
$addressId = $this->id; |
|
95
|
|
|
|
|
96
|
|
|
$this->update($this->safe(), "id = :id", "id={$addressId}"); |
|
97
|
|
|
if ($this->fail()) { |
|
98
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if (!$this->required()) { |
|
104
|
|
|
$this->error = "Campos inválidos!"; |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** Address Create */ |
|
109
|
|
|
if (empty($this->id)) { |
|
110
|
|
|
$addressId = $this->create($this->safe()); |
|
111
|
|
|
if ($this->fail()) { |
|
112
|
|
|
$this->error = "Erro ao cadastrar, verifique os dados"; |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->data = ($this->findById($addressId))->data(); |
|
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|