1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* File:Mobile.php |
6
|
|
|
* |
7
|
|
|
* @author Maciej Sławik <[email protected]> |
8
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace LizardMedia\CartRuleMobile\Model\Rule\Condition; |
12
|
|
|
|
13
|
|
|
use LizardMedia\CartRuleMobile\Api\MobileDetectorInterface; |
14
|
|
|
use Magento\Config\Model\Config\Source\Yesno; |
15
|
|
|
use Magento\Framework\Model\AbstractModel; |
16
|
|
|
use Magento\Rule\Model\Condition\AbstractCondition; |
17
|
|
|
use Magento\Rule\Model\Condition\Context; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Mobile |
21
|
|
|
* @package LizardMedia\CartRuleMobile\Model\Rule\Condition |
22
|
|
|
*/ |
23
|
|
|
class Mobile extends AbstractCondition |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Yesno |
27
|
|
|
*/ |
28
|
|
|
protected $sourceYesNo; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var MobileDetectorInterface |
32
|
|
|
*/ |
33
|
|
|
private $mobileDetector; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* @param Context $context |
38
|
|
|
* @param Yesno $sourceYesNo |
39
|
|
|
* @param MobileDetectorInterface $mobileDetector |
40
|
|
|
* @param array $data |
41
|
|
|
*/ |
42
|
|
|
public function __construct( |
43
|
|
|
Context $context, |
44
|
|
|
Yesno $sourceYesNo, |
45
|
|
|
MobileDetectorInterface $mobileDetector, |
46
|
|
|
array $data = [] |
47
|
|
|
) { |
48
|
|
|
$this->callParentConstructor($context, $data); |
49
|
|
|
$this->sourceYesNo = $sourceYesNo; |
50
|
|
|
$this->mobileDetector = $mobileDetector; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param AbstractModel $model |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function validate(AbstractModel $model) |
58
|
|
|
{ |
59
|
|
|
$enabledOnMobile = (bool)$this->getValue(); |
60
|
|
|
$isMobile = $this->mobileDetector->isMobileDevice(); |
61
|
|
|
|
62
|
|
|
return $enabledOnMobile === $isMobile; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getInputType() |
69
|
|
|
{ |
70
|
|
|
return 'select'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getValueElementType() |
77
|
|
|
{ |
78
|
|
|
return 'select'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array|mixed |
83
|
|
|
*/ |
84
|
|
|
public function getValueSelectOptions() |
85
|
|
|
{ |
86
|
|
|
if (!$this->hasData('value_select_options')) { |
87
|
|
|
$this->setData( |
88
|
|
|
'value_select_options', |
89
|
|
|
$this->sourceYesNo->toOptionArray() |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
return $this->getData('value_select_options'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return Mobile |
97
|
|
|
*/ |
98
|
|
|
public function loadAttributeOptions() |
99
|
|
|
{ |
100
|
|
|
$this->setData( |
101
|
|
|
'attribute_option', |
102
|
|
|
[ |
103
|
|
|
'is_mobile' => __('Mobile device') |
104
|
|
|
] |
105
|
|
|
); |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Context $context |
112
|
|
|
* @param array $data |
113
|
|
|
* @codeCoverageIgnore |
114
|
|
|
*/ |
115
|
|
|
protected function callParentConstructor(Context $context, array $data): void |
116
|
|
|
{ |
117
|
|
|
parent::__construct($context, $data); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.