/src/Validators/DeliveryMethodsValidator.php

https://gitlab.com/mallgroup/mpapi-client · PHP · 274 lines · 209 code · 12 blank · 53 comment · 19 complexity · 55b44e1d8ad72c921ff77dd3b09e0c0e MD5 · raw file

  1. <?php
  2. namespace Marketplace\Validators;
  3. use Marketplace\Entity\DeliveryMethod;
  4. use Marketplace\Entity\DeliverySetupPricing;
  5. use Marketplace\Exception\ValidatorException;
  6. /**
  7. * Parameters validator
  8. *
  9. * @author Martin Hrdlicka <martin.hrdlicka@mall.cz>
  10. */
  11. class DeliveryMethodsValidator extends AbstractValidator
  12. {
  13. /**
  14. *
  15. * @var string
  16. */
  17. const DELIVERY_METHODS = 'delivery_methods';
  18. /**
  19. *
  20. * @var string
  21. */
  22. const DELIVERY_SETUPS = 'delivery_setups';
  23. /**
  24. *
  25. * @var array
  26. */
  27. protected $sourceStructure = [
  28. 'id' => self::VAR_TYPE_STRING,
  29. DeliveryMethod::KEY_TITLE => self::VAR_TYPE_STRING,
  30. DeliveryMethod::KEY_PRICE => [
  31. self::VAR_TYPE_INTEGER,
  32. self::VAR_TYPE_DOUBLE
  33. ],
  34. DeliveryMethod::KEY_COD_PRICE => [
  35. self::VAR_TYPE_INTEGER,
  36. self::VAR_TYPE_DOUBLE
  37. ],
  38. DeliveryMethod::KEY_FREE_LIMIT => [
  39. self::VAR_TYPE_INTEGER,
  40. self::VAR_TYPE_DOUBLE
  41. ],
  42. DeliveryMethod::KEY_DELIVERY_DELAY => [
  43. self::VAR_TYPE_INTEGER,
  44. self::VAR_TYPE_DOUBLE
  45. ]
  46. ];
  47. /**
  48. *
  49. * @var array
  50. */
  51. protected $deliverySetupSourceStructure = [
  52. 'id' => self::VAR_TYPE_STRING,
  53. 'pricing' => self::VAR_TYPE_ARRAY
  54. ];
  55. /**
  56. *
  57. * @var array
  58. */
  59. protected $pricingSourceStructure = [
  60. 'id' => self::VAR_TYPE_STRING,
  61. DeliverySetupPricing::KEY_PRICE => [
  62. self::VAR_TYPE_INTEGER,
  63. self::VAR_TYPE_DOUBLE
  64. ],
  65. DeliverySetupPricing::KEY_COD_PRICE => [
  66. self::VAR_TYPE_INTEGER,
  67. self::VAR_TYPE_DOUBLE
  68. ],
  69. DeliverySetupPricing::KEY_FREE_LIMIT => [
  70. self::VAR_TYPE_INTEGER,
  71. self::VAR_TYPE_DOUBLE
  72. ],
  73. DeliverySetupPricing::KEY_DELIVERY_DELAY => [
  74. self::VAR_TYPE_INTEGER,
  75. self::VAR_TYPE_DOUBLE
  76. ]
  77. ];
  78. /**
  79. * Validate delivery methods
  80. *
  81. * @param array $validationData
  82. * @throws ValidatorException
  83. * @return boolean
  84. */
  85. public function validate($validationData)
  86. {
  87. if (empty($validationData) || !is_array($validationData)) {
  88. throw $this->generateThrow(ValidatorException::MSG_BAD_DATASTRUCTURE);
  89. }
  90. if (!isset($validationData[self::DELIVERY_METHODS]) || $validationData[self::DELIVERY_METHODS] === null) {
  91. throw $this->generateThrow(ValidatorException::MSG_MISSING_DELIVERY_METHODS);
  92. }
  93. foreach ($validationData[self::DELIVERY_METHODS] as $delivery_method) {
  94. $methodCurrentPosition = implode('/', [
  95. self::DELIVERY_METHODS,
  96. array_search($delivery_method, $validationData[self::DELIVERY_METHODS])
  97. ]);
  98. $this->validateStructure($delivery_method, $this->sourceStructure, $methodCurrentPosition);
  99. // ID validate
  100. if ($this->validateLength($delivery_method['id'], 50, 1) === false) {
  101. throw $this->generateThrow(sprintf(
  102. ValidatorException::MSG_INVALID_VALUE_BETWEEN,
  103. 'id',
  104. $validationData[$delivery_method['id']],
  105. 1,
  106. 50
  107. ));
  108. }
  109. // Title validate
  110. if ($this->validateLength($delivery_method[DeliveryMethod::KEY_TITLE], 200, 1) === false) {
  111. throw $this->generateThrow(sprintf(
  112. ValidatorException::MSG_INVALID_VALUE_BETWEEN,
  113. DeliveryMethod::KEY_TITLE,
  114. $validationData[$delivery_method[DeliveryMethod::KEY_TITLE]],
  115. 1,
  116. 200
  117. ));
  118. }
  119. //Price validate
  120. if ($delivery_method[DeliveryMethod::KEY_PRICE] < 0) {
  121. throw $this->generateThrow(sprintf(
  122. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  123. implode('/', [$methodCurrentPosition, DeliveryMethod::KEY_PRICE]),
  124. $delivery_method[DeliveryMethod::KEY_PRICE],
  125. 0,
  126. 0
  127. ));
  128. }
  129. //COD price validate
  130. if ($delivery_method[DeliveryMethod::KEY_COD_PRICE] < 0) {
  131. throw $this->generateThrow(sprintf(
  132. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  133. implode('/', [$methodCurrentPosition, DeliveryMethod::KEY_COD_PRICE]),
  134. $delivery_method[DeliveryMethod::KEY_COD_PRICE],
  135. 0,
  136. 0
  137. ));
  138. }
  139. //Free limit validate
  140. if ($delivery_method[DeliveryMethod::KEY_FREE_LIMIT] < 0) {
  141. throw $this->generateThrow(sprintf(
  142. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  143. implode('/', [$methodCurrentPosition, DeliveryMethod::KEY_FREE_LIMIT]),
  144. $delivery_method[DeliveryMethod::KEY_FREE_LIMIT],
  145. 0,
  146. 0
  147. ));
  148. }
  149. //Delivery delay validate
  150. if ($delivery_method[DeliveryMethod::KEY_DELIVERY_DELAY] < 0) {
  151. throw $this->generateThrow(sprintf(
  152. ValidatorException::MSG_INVALID_VALUE,
  153. $delivery_method[DeliveryMethod::KEY_DELIVERY_DELAY],
  154. 0,
  155. implode('/', [$methodCurrentPosition, DeliveryMethod::KEY_DELIVERY_DELAY])
  156. ));
  157. }
  158. // is pick up point validate
  159. if (isset($delivery_method[DeliveryMethod::KEY_IS_PICKUP_POINT]) &&
  160. is_bool($delivery_method[DeliveryMethod::KEY_IS_PICKUP_POINT]) === false) {
  161. throw $this->generateThrow(sprintf(
  162. ValidatorException::MSG_BAD_DATE_TYPE,
  163. implode('/', [$methodCurrentPosition, DeliveryMethod::KEY_IS_PICKUP_POINT]),
  164. gettype($delivery_method[DeliveryMethod::KEY_IS_PICKUP_POINT]),
  165. 'bool'
  166. ));
  167. }
  168. }
  169. if (isset($validationData[self::DELIVERY_SETUPS]) && is_array($validationData[self::DELIVERY_SETUPS])) {
  170. $this->validateDeliverySetups($validationData);
  171. }
  172. return true;
  173. }
  174. /**
  175. * Validate delivery setups
  176. *
  177. * @param $validationData
  178. * @throws ValidatorException
  179. * @return boolean
  180. */
  181. public function validateDeliverySetups($validationData)
  182. {
  183. foreach ($validationData[self::DELIVERY_SETUPS] as $delivery_setup) {
  184. $setupCurrentPosition = implode('/', [
  185. self::DELIVERY_SETUPS,
  186. array_search($delivery_setup, $validationData[self::DELIVERY_SETUPS])
  187. ]);
  188. $this->validateStructure(
  189. $delivery_setup,
  190. $this->deliverySetupSourceStructure,
  191. implode('/', [$setupCurrentPosition])
  192. );
  193. // ID validate
  194. if ($this->validateLength($delivery_setup['id'], 50, 1) === false) {
  195. throw $this->generateThrow(sprintf(
  196. ValidatorException::MSG_INVALID_VALUE_BETWEEN,
  197. implode('/', [$setupCurrentPosition, 'id']),
  198. $validationData[$delivery_setup['id']],
  199. 1,
  200. 50
  201. ));
  202. }
  203. //Pricing
  204. foreach ($delivery_setup['pricing'] as $pricing) {
  205. $pricingCurrentPosition = implode('/', [
  206. $setupCurrentPosition,
  207. 'pricing',
  208. array_search($pricing, $delivery_setup['pricing'])
  209. ]);
  210. $this->validateStructure($pricing, $this->pricingSourceStructure, $pricingCurrentPosition);
  211. // ID validate
  212. if (in_array($pricing['id'], array_column($validationData[self::DELIVERY_METHODS], 'id')) === false) {
  213. throw $this->generateThrow(sprintf(
  214. ValidatorException::MSG_DELIVERY_METHOD_ID_NOT_FOUND,
  215. implode('/', [$pricingCurrentPosition, 'id']),
  216. $pricing['id']
  217. ));
  218. }
  219. //Price validate
  220. if ($pricing[DeliverySetupPricing::KEY_PRICE] < 0) {
  221. throw $this->generateThrow(sprintf(
  222. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  223. implode('/', [$pricingCurrentPosition, DeliverySetupPricing::KEY_PRICE]),
  224. $pricing[DeliverySetupPricing::KEY_PRICE],
  225. 0,
  226. 0
  227. ));
  228. }
  229. //COD price validate
  230. if ($pricing[DeliverySetupPricing::KEY_COD_PRICE] < 0) {
  231. throw $this->generateThrow(sprintf(
  232. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  233. implode('/', [$pricingCurrentPosition, DeliverySetupPricing::KEY_COD_PRICE]),
  234. $pricing[DeliverySetupPricing::KEY_COD_PRICE],
  235. 0,
  236. 0
  237. ));
  238. }
  239. //Free limit validate
  240. if ($pricing[DeliverySetupPricing::KEY_FREE_LIMIT] < 0) {
  241. throw $this->generateThrow(sprintf(
  242. ValidatorException::MSG_INVALID_FLOAT_VALUE_MIN,
  243. implode('/', [$pricingCurrentPosition, DeliverySetupPricing::KEY_FREE_LIMIT]),
  244. $pricing[DeliverySetupPricing::KEY_FREE_LIMIT],
  245. 0,
  246. 0
  247. ));
  248. }
  249. //Delivery delay validate
  250. if ($pricing[DeliveryMethod::KEY_DELIVERY_DELAY] < 0) {
  251. throw $this->generateThrow(sprintf(
  252. ValidatorException::MSG_INVALID_VALUE,
  253. $pricing[DeliverySetupPricing::KEY_DELIVERY_DELAY],
  254. 0,
  255. implode('/', [$pricingCurrentPosition, DeliverySetupPricing::KEY_DELIVERY_DELAY])
  256. ));
  257. }
  258. }
  259. }
  260. return true;
  261. }
  262. }