1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Source\Models; |
4
|
|
|
|
5
|
|
|
use Source\Core\Model; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* ProductImage class... |
9
|
|
|
* |
10
|
|
|
* @package \Source\Models\ProductImage |
11
|
|
|
*/ |
12
|
|
|
class ProductImage extends Model |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* ProductImage constructor. |
16
|
|
|
*/ |
17
|
|
|
public function __construct() |
18
|
|
|
{ |
19
|
|
|
parent::__construct('product_image', ['id'], ['product_id', 'url_slug', 'image']); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int $product_id |
24
|
|
|
* @param string $url_slug |
25
|
|
|
* @param string $image |
26
|
|
|
* @return ProductImage |
27
|
|
|
*/ |
28
|
|
|
public function bootstrap(int $product_id, string $url_slug, string $image): ProductImage |
29
|
|
|
{ |
30
|
|
|
$this->product_id = $product_id; |
|
|
|
|
31
|
|
|
$this->url_slug = $url_slug; |
|
|
|
|
32
|
|
|
$this->image = $image; |
|
|
|
|
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $slug |
38
|
|
|
* @param string $columns |
39
|
|
|
* @return null|ProductImage |
40
|
|
|
*/ |
41
|
|
|
public function findBySlug(string $slug, string $columns = "*"): ?ProductImage |
42
|
|
|
{ |
43
|
|
|
$find = $this->find("url_slug = :slug", "slug={$slug}", $columns); |
44
|
|
|
|
45
|
|
|
return $find->fetch(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $productId |
50
|
|
|
* @param string $columns |
51
|
|
|
* @return array|null |
52
|
|
|
*/ |
53
|
|
|
public function findAllByProductId(int $productId, string $columns = "*"): ?array |
54
|
|
|
{ |
55
|
|
|
$find = $this->find("product_id = :product_id", "product_id={$productId}", $columns); |
56
|
|
|
|
57
|
|
|
$data = $find->fetch(true); |
58
|
|
|
|
59
|
|
|
if (empty($data)) { |
60
|
|
|
return []; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($data as $key => $value) { |
64
|
|
|
$data[$key] = $value->data(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $data; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function save(): bool |
74
|
|
|
{ |
75
|
|
|
if (!$this->required()) { |
76
|
|
|
$this->error = "ID do produto, URL Slug e Base64, são obrigatórios!"; |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** ProductImage Update */ |
81
|
|
|
if (!empty($this->id)) { |
82
|
|
|
$this->update($this->safe(), "id = :id", "id={$this->id}"); |
83
|
|
|
if ($this->fail()) { |
84
|
|
|
$this->error = "Erro ao atualizar, verifique os dados"; |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** ProductImage Create */ |
90
|
|
|
if (empty($this->id)) { |
91
|
|
|
$this->id = $this->create($this->safe()); |
|
|
|
|
92
|
|
|
|
93
|
|
|
if ($this->fail()) { |
94
|
|
|
$this->error = "Erro ao cadastrar, verifique os dados"; |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->data = ($this->findById($this->id))->data(); |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|