1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Domain Entity Class |
6
|
|
|
* @author Max Demian <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ticaje\AliexpressConsumer\Domain\Entity\Request\Validator\Product; |
10
|
|
|
|
11
|
|
|
use Ticaje\Contract\Patterns\Interfaces\Decorator\DecoratorInterface; |
12
|
|
|
use Ticaje\AeSdk\Infrastructure\Interfaces\Provider\Request\RequestDtoInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ValidatorTrait |
16
|
|
|
* @package Ticaje\AliexpressConsumer\Domain\Entity\Request\Validator\Product |
17
|
|
|
*/ |
18
|
|
|
trait ValidatorTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @inheritDoc |
22
|
|
|
*/ |
23
|
|
|
public function validate(RequestDtoInterface $request): DecoratorInterface |
24
|
|
|
{ |
25
|
|
|
$requestContent = $request->getContent(); |
26
|
|
|
$params = json_decode($requestContent); // This might be subject to a volatile strategy |
27
|
|
|
foreach (self::ARTIFACTS as $component) { |
|
|
|
|
28
|
|
|
$function = "validate{$component}"; |
29
|
|
|
$validComponent = $this->$function($params); |
30
|
|
|
if (!$validComponent) { |
31
|
|
|
return $this->response; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
$this->processResponse(1, "Request is Valid", $requestContent); |
35
|
|
|
return $this->response; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $params |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
private function validateObjects($params) |
43
|
|
|
{ |
44
|
|
|
$result = is_object($params); |
45
|
|
|
$result ?: $this->processResponse(0, "Request must be an object"); |
46
|
|
|
return $result; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $params |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
|
|
private function validateMainImage($params) |
54
|
|
|
{ |
55
|
|
|
$result = (function () use ($params) { |
56
|
|
|
$mainImageArray = $params->main_image_urls_list ?? null; |
57
|
|
|
return $mainImageArray |
58
|
|
|
&& is_array($mainImageArray) |
59
|
|
|
&& 6 > count($mainImageArray); |
60
|
|
|
})(); |
61
|
|
|
$result ?: $this->processResponse(0, "Incorrect Images compliance"); |
62
|
|
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $params |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
private function validateSkuInfoList($params) |
70
|
|
|
{ |
71
|
|
|
$result = (function ($params) { |
72
|
|
|
$list = $params->sku_info_list ?? null; |
73
|
|
|
return $list |
74
|
|
|
&& is_array($list) |
75
|
|
|
&& count($list) > 0 |
76
|
|
|
&& (function ($list) { |
77
|
|
|
$missing = []; |
78
|
|
|
foreach (static::SKU_INFO_LIST_REQUIRED_ATTRIBUTES as $property) { |
|
|
|
|
79
|
|
|
property_exists($list[0], $property) ?: array_push($missing, $property); |
80
|
|
|
} |
81
|
|
|
return empty($missing); |
82
|
|
|
})($list); |
83
|
|
|
})($params); |
84
|
|
|
$result ?: $this->processResponse(0, "Incorrect Info List compliance"); |
85
|
|
|
return $result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $params |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
private function validateAttributes($params) |
93
|
|
|
{ |
94
|
|
|
$result = (function ($params) use (&$missing) { |
95
|
|
|
$missing = []; |
96
|
|
|
foreach (static::REQUIRED_ATTRIBUTES as $property) { |
|
|
|
|
97
|
|
|
property_exists($params, $property) ?: array_push($missing, $property); |
98
|
|
|
} |
99
|
|
|
return empty($missing); |
100
|
|
|
})($params); |
101
|
|
|
$result ?: (function ($missing) { |
102
|
|
|
$missingAttributes = implode(', ', $missing); |
103
|
|
|
$this->processResponse(0, "Missing parameters {$missingAttributes}"); |
104
|
|
|
})($missing); |
105
|
|
|
return $result; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $success |
110
|
|
|
* @param $message |
111
|
|
|
* @param null $result |
|
|
|
|
112
|
|
|
* Kind of mixing concerns yeh |
113
|
|
|
*/ |
114
|
|
|
private function processResponse($success = true, $message, $result = null) |
115
|
|
|
{ |
116
|
|
|
$this->response |
117
|
|
|
->setSuccess((bool)$success) |
118
|
|
|
->setRequest($result) |
119
|
|
|
->setMessage($message); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|