AbstractCondition.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Rule\Model\Condition;
  7. use Magento\Framework\Data\Form;
  8. use Magento\Framework\Data\Form\Element\AbstractElement;
  9. /**
  10. * Abstract Rule condition data model
  11. *
  12. * @method string getOperator()
  13. * @method string getFormName()
  14. * @method setFormName()
  15. * @SuppressWarnings(PHPMD.ExcessivePublicCount)
  16. * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
  17. * @api
  18. * @since 100.0.2
  19. */
  20. abstract class AbstractCondition extends \Magento\Framework\DataObject implements ConditionInterface
  21. {
  22. /**
  23. * Defines which operators will be available for this condition
  24. * @var string
  25. */
  26. protected $_inputType = null;
  27. /**
  28. * Default values for possible operator options
  29. * @var array
  30. */
  31. protected $_defaultOperatorOptions = null;
  32. /**
  33. * Default combinations of operator options, depending on input type
  34. * @var array
  35. */
  36. protected $_defaultOperatorInputByType = null;
  37. /**
  38. * List of input types for values which should be array
  39. * @var string[]
  40. */
  41. protected $_arrayInputTypes = [];
  42. /**
  43. * @var \Magento\Framework\View\Asset\Repository
  44. */
  45. protected $_assetRepo;
  46. /**
  47. * @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  48. */
  49. protected $_localeDate;
  50. /**
  51. * @var \Magento\Framework\View\LayoutInterface
  52. */
  53. protected $_layout;
  54. /**
  55. * Base name for hidden elements.
  56. *
  57. * @var string
  58. */
  59. protected $elementName = 'rule';
  60. /**
  61. * @param Context $context
  62. * @param array $data
  63. */
  64. public function __construct(Context $context, array $data = [])
  65. {
  66. $this->_assetRepo = $context->getAssetRepository();
  67. $this->_localeDate = $context->getLocaleDate();
  68. $this->_layout = $context->getLayout();
  69. parent::__construct($data);
  70. $this->loadAttributeOptions()->loadOperatorOptions()->loadValueOptions();
  71. $options = $this->getAttributeOptions();
  72. if ($options) {
  73. reset($options);
  74. $this->setAttribute(key($options));
  75. }
  76. $options = $this->getOperatorOptions();
  77. if ($options) {
  78. reset($options);
  79. $this->setOperator(key($options));
  80. }
  81. }
  82. /**
  83. * Default operator input by type map getter
  84. *
  85. * @return array
  86. */
  87. public function getDefaultOperatorInputByType()
  88. {
  89. if (null === $this->_defaultOperatorInputByType) {
  90. $this->_defaultOperatorInputByType = [
  91. 'string' => ['==', '!=', '>=', '>', '<=', '<', '{}', '!{}', '()', '!()'],
  92. 'numeric' => ['==', '!=', '>=', '>', '<=', '<', '()', '!()'],
  93. 'date' => ['==', '>=', '<='],
  94. 'select' => ['==', '!=', '<=>'],
  95. 'boolean' => ['==', '!=', '<=>'],
  96. 'multiselect' => ['{}', '!{}', '()', '!()'],
  97. 'grid' => ['()', '!()'],
  98. ];
  99. $this->_arrayInputTypes = ['multiselect', 'grid'];
  100. }
  101. return $this->_defaultOperatorInputByType;
  102. }
  103. /**
  104. * Default operator options getter.
  105. *
  106. * Provides all possible operator options.
  107. *
  108. * @return array
  109. */
  110. public function getDefaultOperatorOptions()
  111. {
  112. if (null === $this->_defaultOperatorOptions) {
  113. $this->_defaultOperatorOptions = [
  114. '==' => __('is'),
  115. '!=' => __('is not'),
  116. '>=' => __('equals or greater than'),
  117. '<=' => __('equals or less than'),
  118. '>' => __('greater than'),
  119. '<' => __('less than'),
  120. '{}' => __('contains'),
  121. '!{}' => __('does not contain'),
  122. '()' => __('is one of'),
  123. '!()' => __('is not one of'),
  124. '<=>' => __('is undefined'),
  125. ];
  126. }
  127. return $this->_defaultOperatorOptions;
  128. }
  129. /**
  130. * Get rule form.
  131. *
  132. * @return Form
  133. */
  134. public function getForm()
  135. {
  136. return $this->getRule()->getForm();
  137. }
  138. /**
  139. * Get condition as array.
  140. *
  141. * @param array $arrAttributes
  142. * @return array
  143. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  144. */
  145. public function asArray(array $arrAttributes = [])
  146. {
  147. return [
  148. 'type' => $this->getType(),
  149. 'attribute' => $this->getAttribute(),
  150. 'operator' => $this->getOperator(),
  151. 'value' => $this->getValue(),
  152. 'is_value_processed' => $this->getIsValueParsed(),
  153. ];
  154. }
  155. /**
  156. * Get tables to join
  157. *
  158. * @return array
  159. */
  160. public function getTablesToJoin()
  161. {
  162. return [];
  163. }
  164. /**
  165. * Get value to bind
  166. *
  167. * @return array|float|int|mixed|string
  168. */
  169. public function getBindArgumentValue()
  170. {
  171. return $this->getValueParsed();
  172. }
  173. /**
  174. * Get field by attribute
  175. *
  176. * @return string
  177. */
  178. public function getMappedSqlField()
  179. {
  180. return $this->getAttribute();
  181. }
  182. /**
  183. * Get condition as xml.
  184. *
  185. * @return string
  186. */
  187. public function asXml()
  188. {
  189. return "<type>" .
  190. $this->getType() .
  191. "</type>" .
  192. "<attribute>" .
  193. $this->getAttribute() .
  194. "</attribute>" .
  195. "<operator>" .
  196. $this->getOperator() .
  197. "</operator>" .
  198. "<value>" .
  199. $this->getValue() .
  200. "</value>";
  201. }
  202. /**
  203. * Load condition from array.
  204. *
  205. * @param array $arr
  206. * @return $this
  207. * @SuppressWarnings(PHPMD.NPathComplexity)
  208. */
  209. public function loadArray($arr)
  210. {
  211. $this->setType($arr['type']);
  212. $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
  213. $this->setOperator(isset($arr['operator']) ? $arr['operator'] : false);
  214. $this->setValue(isset($arr['value']) ? $arr['value'] : false);
  215. $this->setIsValueParsed(isset($arr['is_value_parsed']) ? $arr['is_value_parsed'] : false);
  216. return $this;
  217. }
  218. /**
  219. * Load condition from xml.
  220. *
  221. * @param string|array $xml
  222. * @return $this
  223. */
  224. public function loadXml($xml)
  225. {
  226. if (is_string($xml)) {
  227. $xml = simplexml_load_string($xml);
  228. }
  229. $this->loadArray((array)$xml);
  230. return $this;
  231. }
  232. /**
  233. * Load attribute options.
  234. *
  235. * @return $this
  236. */
  237. public function loadAttributeOptions()
  238. {
  239. return $this;
  240. }
  241. /**
  242. * Get attribute options.
  243. *
  244. * @return array
  245. */
  246. public function getAttributeOptions()
  247. {
  248. return [];
  249. }
  250. /**
  251. * Get attribute select options.
  252. *
  253. * @return array
  254. */
  255. public function getAttributeSelectOptions()
  256. {
  257. $opt = [];
  258. foreach ($this->getAttributeOption() as $key => $value) {
  259. $opt[] = ['value' => $key, 'label' => $value];
  260. }
  261. return $opt;
  262. }
  263. /**
  264. * Get attribute name.
  265. *
  266. * @return string
  267. */
  268. public function getAttributeName()
  269. {
  270. return $this->getAttributeOption($this->getAttribute());
  271. }
  272. /**
  273. * Load operator options.
  274. *
  275. * @return $this
  276. */
  277. public function loadOperatorOptions()
  278. {
  279. $this->setOperatorOption($this->getDefaultOperatorOptions());
  280. $this->setOperatorByInputType($this->getDefaultOperatorInputByType());
  281. return $this;
  282. }
  283. /**
  284. * This value will define which operators will be available for this condition.
  285. *
  286. * Possible values are: string, numeric, date, select, multiselect, grid, boolean
  287. *
  288. * @return string
  289. */
  290. public function getInputType()
  291. {
  292. return null === $this->_inputType ? 'string' : $this->_inputType;
  293. }
  294. /**
  295. * Get operator select options.
  296. *
  297. * @return array
  298. */
  299. public function getOperatorSelectOptions()
  300. {
  301. $type = $this->getInputType();
  302. $opt = [];
  303. $operatorByType = $this->getOperatorByInputType();
  304. foreach ($this->getOperatorOption() as $key => $value) {
  305. if (!$operatorByType || in_array($key, $operatorByType[$type])) {
  306. $opt[] = ['value' => $key, 'label' => $value];
  307. }
  308. }
  309. return $opt;
  310. }
  311. /**
  312. * Get operator name.
  313. *
  314. * @return array
  315. */
  316. public function getOperatorName()
  317. {
  318. return $this->getOperatorOption($this->getOperator());
  319. }
  320. /**
  321. * Load value options.
  322. *
  323. * @return $this
  324. */
  325. public function loadValueOptions()
  326. {
  327. $this->setValueOption([]);
  328. return $this;
  329. }
  330. /**
  331. * Get value select options.
  332. *
  333. * @return array
  334. */
  335. public function getValueSelectOptions()
  336. {
  337. $opt = [];
  338. if ($this->hasValueOption()) {
  339. foreach ((array)$this->getValueOption() as $key => $value) {
  340. $opt[] = ['value' => $key, 'label' => $value];
  341. }
  342. }
  343. return $opt;
  344. }
  345. /**
  346. * Retrieve parsed value
  347. *
  348. * @return array|string|int|float
  349. */
  350. public function getValueParsed()
  351. {
  352. if (!$this->hasValueParsed()) {
  353. $value = $this->getData('value');
  354. if (is_array($value) && count($value) === 1) {
  355. $value = reset($value);
  356. }
  357. if (!is_array($value) && $this->isArrayOperatorType() && $value) {
  358. $value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
  359. }
  360. $this->setValueParsed($value);
  361. }
  362. return $this->getData('value_parsed');
  363. }
  364. /**
  365. * Check if value should be array
  366. *
  367. * Depends on operator input type
  368. *
  369. * @return bool
  370. */
  371. public function isArrayOperatorType()
  372. {
  373. $operator = $this->getOperator();
  374. return $operator === '()' || $operator === '!()' || in_array($this->getInputType(), $this->_arrayInputTypes);
  375. }
  376. /**
  377. * Get value.
  378. *
  379. * @return mixed
  380. */
  381. public function getValue()
  382. {
  383. if ($this->getInputType() == 'date' && !$this->getIsValueParsed()) {
  384. // date format intentionally hard-coded
  385. $this->setValue(
  386. (new \DateTime($this->getData('value')))->format('Y-m-d H:i:s')
  387. );
  388. $this->setIsValueParsed(true);
  389. }
  390. return $this->getData('value');
  391. }
  392. /**
  393. * Get value name.
  394. *
  395. * @return array|string
  396. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  397. */
  398. public function getValueName()
  399. {
  400. $value = $this->getValue();
  401. if ($value === null || '' === $value) {
  402. return '...';
  403. }
  404. $options = $this->getValueSelectOptions();
  405. $valueArr = [];
  406. if (!empty($options)) {
  407. foreach ($options as $option) {
  408. if (is_array($value)) {
  409. if (in_array($option['value'], $value)) {
  410. $valueArr[] = $option['label'];
  411. }
  412. } elseif (isset($option['value'])) {
  413. if (is_array($option['value'])) {
  414. foreach ($option['value'] as $optionValue) {
  415. if ($optionValue['value'] == $value) {
  416. return $optionValue['label'];
  417. }
  418. }
  419. }
  420. if ($option['value'] == $value) {
  421. return $option['label'];
  422. }
  423. }
  424. }
  425. }
  426. if (!empty($valueArr)) {
  427. $value = implode(', ', $valueArr);
  428. } elseif (is_array($value)) {
  429. $value = implode(', ', $value);
  430. }
  431. return $value;
  432. }
  433. /**
  434. * Get inherited conditions selectors
  435. *
  436. * @return array
  437. */
  438. public function getNewChildSelectOptions()
  439. {
  440. return [['value' => '', 'label' => __('Please choose a condition to add.')]];
  441. }
  442. /**
  443. * Get new child name.
  444. *
  445. * @return string
  446. */
  447. public function getNewChildName()
  448. {
  449. return $this->getAddLinkHtml();
  450. }
  451. /**
  452. * Get this condition as html.
  453. *
  454. * @return string
  455. */
  456. public function asHtml()
  457. {
  458. return $this->getTypeElementHtml() .
  459. $this->getAttributeElementHtml() .
  460. $this->getOperatorElementHtml() .
  461. $this->getValueElementHtml() .
  462. $this->getRemoveLinkHtml() .
  463. $this->getChooserContainerHtml();
  464. }
  465. /**
  466. * Get this condition with subconditions as html.
  467. *
  468. * @return string
  469. */
  470. public function asHtmlRecursive()
  471. {
  472. return $this->asHtml();
  473. }
  474. /**
  475. * Get type element.
  476. *
  477. * @return AbstractElement
  478. */
  479. public function getTypeElement()
  480. {
  481. return $this->getForm()->addField(
  482. $this->getPrefix() . '__' . $this->getId() . '__type',
  483. 'hidden',
  484. [
  485. 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][type]',
  486. 'value' => $this->getType(),
  487. 'no_span' => true,
  488. 'class' => 'hidden',
  489. 'data-form-part' => $this->getFormName()
  490. ]
  491. );
  492. }
  493. /**
  494. * Get type element html.
  495. *
  496. * @return string
  497. */
  498. public function getTypeElementHtml()
  499. {
  500. return $this->getTypeElement()->getHtml();
  501. }
  502. /**
  503. * Get attribute element.
  504. *
  505. * @return $this
  506. */
  507. public function getAttributeElement()
  508. {
  509. if (null === $this->getAttribute()) {
  510. $options = $this->getAttributeOption();
  511. if ($options) {
  512. reset($options);
  513. $this->setAttribute(key($options));
  514. }
  515. }
  516. return $this->getForm()->addField(
  517. $this->getPrefix() . '__' . $this->getId() . '__attribute',
  518. 'select',
  519. [
  520. 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][attribute]',
  521. 'values' => $this->getAttributeSelectOptions(),
  522. 'value' => $this->getAttribute(),
  523. 'value_name' => $this->getAttributeName(),
  524. 'data-form-part' => $this->getFormName()
  525. ]
  526. )->setRenderer(
  527. $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class)
  528. );
  529. }
  530. /**
  531. * Get attribute element html.
  532. *
  533. * @return string
  534. */
  535. public function getAttributeElementHtml()
  536. {
  537. return $this->getAttributeElement()->getHtml();
  538. }
  539. /**
  540. * Retrieve Condition Operator element Instance.
  541. *
  542. * If the operator value is empty - define first available operator value as default.
  543. *
  544. * @return \Magento\Framework\Data\Form\Element\Select
  545. */
  546. public function getOperatorElement()
  547. {
  548. $options = $this->getOperatorSelectOptions();
  549. if ($this->getOperator() === null) {
  550. $option = reset($options);
  551. $this->setOperator($option['value']);
  552. }
  553. $elementId = sprintf('%s__%s__operator', $this->getPrefix(), $this->getId());
  554. $elementName = sprintf($this->elementName . '[%s][%s][operator]', $this->getPrefix(), $this->getId());
  555. $element = $this->getForm()->addField(
  556. $elementId,
  557. 'select',
  558. [
  559. 'name' => $elementName,
  560. 'values' => $options,
  561. 'value' => $this->getOperator(),
  562. 'value_name' => $this->getOperatorName(),
  563. 'data-form-part' => $this->getFormName()
  564. ]
  565. );
  566. $element->setRenderer($this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class));
  567. return $element;
  568. }
  569. /**
  570. * Get operator element html.
  571. *
  572. * @return string
  573. */
  574. public function getOperatorElementHtml()
  575. {
  576. return $this->getOperatorElement()->getHtml();
  577. }
  578. /**
  579. * Value element type will define renderer for condition value element
  580. *
  581. * @see \Magento\Framework\Data\Form\Element
  582. * @return string
  583. */
  584. public function getValueElementType()
  585. {
  586. return 'text';
  587. }
  588. /**
  589. * Get value element renderer.
  590. *
  591. * @return \Magento\Rule\Block\Editable
  592. */
  593. public function getValueElementRenderer()
  594. {
  595. if (strpos($this->getValueElementType(), '/') !== false) {
  596. return $this->_layout->getBlockSingleton($this->getValueElementType());
  597. }
  598. return $this->_layout->getBlockSingleton(\Magento\Rule\Block\Editable::class);
  599. }
  600. /**
  601. * Get value element.
  602. *
  603. * @return $this
  604. */
  605. public function getValueElement()
  606. {
  607. $elementParams = [
  608. 'name' => $this->elementName . '[' . $this->getPrefix() . '][' . $this->getId() . '][value]',
  609. 'value' => $this->getValue(),
  610. 'values' => $this->getValueSelectOptions(),
  611. 'value_name' => $this->getValueName(),
  612. 'after_element_html' => $this->getValueAfterElementHtml(),
  613. 'explicit_apply' => $this->getExplicitApply(),
  614. 'data-form-part' => $this->getFormName()
  615. ];
  616. if ($this->getInputType() == 'date') {
  617. // date format intentionally hard-coded
  618. $elementParams['input_format'] = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT;
  619. $elementParams['date_format'] = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT;
  620. $elementParams['placeholder'] = \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT;
  621. $elementParams['autocomplete'] = 'off';
  622. $elementParams['readonly'] = 'true';
  623. }
  624. return $this->getForm()->addField(
  625. $this->getPrefix() . '__' . $this->getId() . '__value',
  626. $this->getValueElementType(),
  627. $elementParams
  628. )->setRenderer(
  629. $this->getValueElementRenderer()
  630. );
  631. }
  632. /**
  633. * Get value element html.
  634. *
  635. * @return string
  636. */
  637. public function getValueElementHtml()
  638. {
  639. return $this->getValueElement()->getHtml();
  640. }
  641. /**
  642. * Get add link html.
  643. *
  644. * @return string
  645. */
  646. public function getAddLinkHtml()
  647. {
  648. $src = $this->_assetRepo->getUrl('images/rule_component_add.gif');
  649. return '<img src="' . $src . '" class="rule-param-add v-middle" alt="" title="' . __('Add') . '"/>';
  650. }
  651. /**
  652. * Get remove link html.
  653. *
  654. * @return string
  655. */
  656. public function getRemoveLinkHtml()
  657. {
  658. $src = $this->_assetRepo->getUrl('images/rule_component_remove.gif');
  659. $html = ' <span class="rule-param"><a href="javascript:void(0)" class="rule-param-remove" title="' . __(
  660. 'Remove'
  661. ) . '"><img src="' . $src . '" alt="" class="v-middle" /></a></span>';
  662. return $html;
  663. }
  664. /**
  665. * Get chooser container html.
  666. *
  667. * @return string
  668. */
  669. public function getChooserContainerHtml()
  670. {
  671. $url = $this->getValueElementChooserUrl();
  672. return $url ? '<div class="rule-chooser" url="' . $url . '"></div>' : '';
  673. }
  674. /**
  675. * Get this condition as string.
  676. *
  677. * @param string $format
  678. * @return string
  679. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  680. */
  681. public function asString($format = '')
  682. {
  683. return $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
  684. }
  685. /**
  686. * Get this condition with subconditions as string.
  687. *
  688. * @param int $level
  689. * @return string
  690. */
  691. public function asStringRecursive($level = 0)
  692. {
  693. return str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
  694. }
  695. /**
  696. * Validate product attribute value for condition
  697. *
  698. * @param object|array|int|string|float|bool $validatedValue product attribute value
  699. * @return bool
  700. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  701. * @SuppressWarnings(PHPMD.NPathComplexity)
  702. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  703. */
  704. public function validateAttribute($validatedValue)
  705. {
  706. if (is_object($validatedValue)) {
  707. return false;
  708. }
  709. /**
  710. * Condition attribute value
  711. */
  712. $value = $this->getValueParsed();
  713. /**
  714. * Comparison operator
  715. */
  716. $option = $this->getOperatorForValidate();
  717. // if operator requires array and it is not, or on opposite, return false
  718. if ($this->isArrayOperatorType() xor is_array($value)) {
  719. return false;
  720. }
  721. $result = false;
  722. switch ($option) {
  723. case '==':
  724. case '!=':
  725. if (is_array($value)) {
  726. if (!is_array($validatedValue)) {
  727. return false;
  728. }
  729. $result = !empty(array_intersect($value, $validatedValue));
  730. } else {
  731. if (is_array($validatedValue)) {
  732. $result = count($validatedValue) == 1 && array_shift($validatedValue) == $value;
  733. } else {
  734. $result = $this->_compareValues($validatedValue, $value);
  735. }
  736. }
  737. break;
  738. case '<=':
  739. case '>':
  740. if (!is_scalar($validatedValue)) {
  741. return false;
  742. }
  743. $result = $validatedValue <= $value;
  744. break;
  745. case '>=':
  746. case '<':
  747. if (!is_scalar($validatedValue)) {
  748. return false;
  749. }
  750. $result = $validatedValue >= $value;
  751. break;
  752. case '{}':
  753. case '!{}':
  754. if (is_scalar($validatedValue) && is_array($value)) {
  755. foreach ($value as $item) {
  756. if (stripos($validatedValue, (string)$item) !== false) {
  757. $result = true;
  758. break;
  759. }
  760. }
  761. } elseif (is_array($value)) {
  762. if (!is_array($validatedValue)) {
  763. return false;
  764. }
  765. $result = array_intersect($value, $validatedValue);
  766. $result = !empty($result);
  767. } else {
  768. if (is_array($validatedValue)) {
  769. $result = in_array($value, $validatedValue);
  770. } else {
  771. $result = $this->_compareValues($value, $validatedValue, false);
  772. }
  773. }
  774. break;
  775. case '()':
  776. case '!()':
  777. if (is_array($validatedValue)) {
  778. $result = count(array_intersect($validatedValue, (array)$value)) > 0;
  779. } else {
  780. $value = (array)$value;
  781. foreach ($value as $item) {
  782. if ($this->_compareValues($validatedValue, $item)) {
  783. $result = true;
  784. break;
  785. }
  786. }
  787. }
  788. break;
  789. }
  790. if ('!=' == $option || '>' == $option || '<' == $option || '!{}' == $option || '!()' == $option) {
  791. $result = !$result;
  792. }
  793. return $result;
  794. }
  795. /**
  796. * Case and type insensitive comparison of values
  797. *
  798. * @param string|int|float $validatedValue
  799. * @param string|int|float $value
  800. * @param bool $strict
  801. * @return bool
  802. */
  803. protected function _compareValues($validatedValue, $value, $strict = true)
  804. {
  805. if ($strict && is_numeric($validatedValue) && is_numeric($value)) {
  806. return $validatedValue == $value;
  807. }
  808. $validatePattern = preg_quote($validatedValue, '~');
  809. if ($strict) {
  810. $validatePattern = '^' . $validatePattern . '$';
  811. }
  812. return (bool)preg_match('~' . $validatePattern . '~iu', $value);
  813. }
  814. /**
  815. * Validate model.
  816. *
  817. * @param \Magento\Framework\Model\AbstractModel $model
  818. * @return bool
  819. */
  820. public function validate(\Magento\Framework\Model\AbstractModel $model)
  821. {
  822. if (!$model->hasData($this->getAttribute())) {
  823. $model->load($model->getId());
  824. }
  825. $attributeValue = $model->getData($this->getAttribute());
  826. return $this->validateAttribute($attributeValue);
  827. }
  828. /**
  829. * Retrieve operator for php validation
  830. *
  831. * @return string
  832. */
  833. public function getOperatorForValidate()
  834. {
  835. return $this->getOperator();
  836. }
  837. }