|
7 | 7 |
|
8 | 8 | namespace Magento\Customer\Model\Validator; |
9 | 9 |
|
| 10 | +use DateTimeZone; |
10 | 11 | use Magento\Customer\Model\Customer; |
11 | 12 | use Magento\Framework\Validator\AbstractValidator; |
12 | | -use Magento\Store\Api\Data\StoreInterface as StoreInterface; |
13 | | -use Magento\Store\Model\StoreManagerInterface; |
14 | 13 |
|
15 | 14 | /** |
16 | 15 | * Customer dob field validator. |
17 | 16 | */ |
18 | 17 | class Dob extends AbstractValidator |
19 | 18 | { |
20 | 19 | /** |
21 | | - * @var \DateTime |
22 | | - */ |
23 | | - private \DateTime $currentDate; |
24 | | - |
25 | | - /** |
26 | | - * @var StoreManagerInterface |
27 | | - */ |
28 | | - private StoreManagerInterface $storeManager; |
29 | | - |
30 | | - /** |
31 | | - * @param StoreManagerInterface $storeManager |
32 | | - */ |
33 | | - public function __construct(StoreManagerInterface $storeManager) |
34 | | - { |
35 | | - $this->currentDate = new \DateTime(); |
36 | | - $this->storeManager = $storeManager; |
37 | | - } |
38 | | - |
39 | | - /** |
40 | | - * Validate name fields. |
| 20 | + * Validate dob field. |
41 | 21 | * |
42 | 22 | * @param Customer $customer |
43 | 23 | * @return bool |
44 | 24 | */ |
45 | 25 | public function isValid($customer): bool |
46 | 26 | { |
47 | | - if (!$this->isValidDob($customer->getDob(), $customer->getStoreId())) { |
48 | | - parent::_addMessages([['dob' => 'The Date of Birth should not be greater than today.']]); |
| 27 | + $timezone = new DateTimeZone($customer->getStore()->getConfig('general/locale/timezone')); |
| 28 | + if (!$this->isValidDob($customer->getDob(), $timezone)) { |
| 29 | + $this->_addMessages([['dob' => 'The Date of Birth should not be greater than today.']]); |
49 | 30 | } |
50 | 31 |
|
51 | | - return count($this->_messages) == 0; |
| 32 | + return count($this->_messages) === 0; |
52 | 33 | } |
53 | 34 |
|
54 | 35 | /** |
55 | 36 | * Check if specified dob is not in the future |
56 | 37 | * |
57 | 38 | * @param string|null $dobValue |
58 | | - * @param null|string|bool|int|StoreInterface $storeId |
| 39 | + * @param DateTimeZone $timezone |
59 | 40 | * @return bool |
60 | 41 | */ |
61 | | - private function isValidDob(?string $dobValue, null|string|bool|int|StoreInterface $storeId): bool |
| 42 | + private function isValidDob(?string $dobValue, ?DateTimeZone $timezone = null): bool |
62 | 43 | { |
63 | 44 | if ($dobValue != null) { |
64 | 45 |
|
65 | | - // Get the timezone of the store |
66 | | - $store = $this->storeManager->getStore($storeId); |
67 | | - $timezone = $store->getConfig('general/locale/timezone'); |
68 | | - |
69 | 46 | // Get the date of birth and set the time to 00:00:00 |
70 | | - $dobDate = new \DateTime($dobValue, new \DateTimeZone($timezone)); |
| 47 | + $dobDate = new \DateTime($dobValue, $timezone); |
71 | 48 | $dobDate->setTime(0, 0, 0); |
72 | 49 |
|
73 | 50 | // Get the timestamp of the date of birth and the current date |
74 | 51 | $dobTimestamp = $dobDate->getTimestamp(); |
75 | | - $currentTimestamp = $this->currentDate->getTimestamp(); |
| 52 | + $currentTimestamp = time(); |
76 | 53 |
|
77 | 54 | // If the date's of birth first minute is in the future, return false - the day has not started yet |
78 | | - if ($dobTimestamp > $currentTimestamp) { |
79 | | - return false; |
80 | | - } |
| 55 | + return ($dobTimestamp <= $currentTimestamp); |
81 | 56 | } |
82 | 57 |
|
83 | 58 | return true; |
|
0 commit comments