Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultValidator.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class DefaultValidator extends \Magento\Framework\Validator\AbstractValidator
13 {
20 
26  protected $priceTypes;
27 
32  public function __construct(
33  \Magento\Catalog\Model\ProductOptions\ConfigInterface $productOptionConfig,
34  \Magento\Catalog\Model\Config\Source\Product\Options\Price $priceConfig
35  ) {
36  foreach ($productOptionConfig->getAll() as $option) {
37  foreach ($option['types'] as $type) {
38  $this->productOptionTypes[] = $type['name'];
39  }
40  }
41 
42  foreach ($priceConfig->toOptionArray() as $item) {
43  $this->priceTypes[] = $item['value'];
44  }
45  }
46 
58  public function isValid($value)
59  {
60  $messages = [];
61 
62  if (!$this->validateOptionRequiredFields($value)) {
63  $messages['option required fields'] = 'Missed values for option required fields';
64  }
65 
66  if (!$this->validateOptionType($value)) {
67  $messages['option type'] = 'Invalid option type';
68  }
69 
70  if (!$this->validateOptionValue($value)) {
71  $messages['option values'] = 'Invalid option value';
72  }
73 
74  $this->_addMessages($messages);
75 
76  return empty($messages);
77  }
78 
86  {
88  $product = $option->getProduct();
89  if ($product) {
90  $storeId = $product->getStoreId();
91  }
92  $title = $option->getTitle();
93  return $this->isValidOptionTitle($title, $storeId) && !$this->isEmpty($option->getType());
94  }
95 
103  protected function isValidOptionTitle($title, $storeId)
104  {
105  // we should be able to set null title for not default store (used for deletion from store view)
106  if ($storeId > \Magento\Store\Model\Store::DEFAULT_STORE_ID && $title === null) {
107  return true;
108  }
109 
110  // checking whether title is null and is empty string
111  if ($title === null || $title === '') {
112  return false;
113  }
114 
115  return true;
116  }
117 
124  protected function validateOptionType(Option $option)
125  {
126  return $this->isInRange($option->getType(), $this->productOptionTypes);
127  }
128 
135  protected function validateOptionValue(Option $option)
136  {
137  return $this->isInRange($option->getPriceType(), $this->priceTypes);
138  }
139 
146  protected function isEmpty($value)
147  {
148  return empty($value);
149  }
150 
158  protected function isInRange($value, array $range)
159  {
160  return in_array($value, $range);
161  }
162 
169  protected function isNegative($value)
170  {
171  return intval($value) < 0;
172  }
173 }
$title
Definition: default.phtml:14
__construct(\Magento\Catalog\Model\ProductOptions\ConfigInterface $productOptionConfig, \Magento\Catalog\Model\Config\Source\Product\Options\Price $priceConfig)
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16