Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Compare.php
Go to the documentation of this file.
1 <?php
7 
12 
16 class Compare
17 {
21  private $serializer;
22 
26  private $jsonValidator;
27 
34  public function __construct(
35  Json $serializer = null,
36  JsonValidator $jsonValidator = null
37  ) {
38  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
39  $this->jsonValidator = $jsonValidator ?: ObjectManager::getInstance()->get(JsonValidator::class);
40  }
41 
48  protected function getOptionValues($value)
49  {
50  if (is_string($value) && $this->jsonValidator->isValid($value)) {
51  $value = $this->serializer->unserialize($value);
52  if (is_array($value)) {
53  unset($value['qty'], $value['uenc']);
54  $value = array_filter($value, function ($optionValue) {
55  return !empty($optionValue);
56  });
57  }
58  }
59  return $value;
60  }
61 
69  public function compare(Item $target, Item $compared)
70  {
71  if ($target->getProductId() != $compared->getProductId()) {
72  return false;
73  }
74  $targetOptions = $this->getOptions($target);
75  $comparedOptions = $this->getOptions($compared);
76 
77  if (array_diff_key($targetOptions, $comparedOptions) != array_diff_key($comparedOptions, $targetOptions)
78  ) {
79  return false;
80  }
81  foreach ($targetOptions as $name => $value) {
82  if ($comparedOptions[$name] != $value) {
83  return false;
84  }
85  }
86  return true;
87  }
88 
95  public function getOptions(Item $item)
96  {
97  $options = [];
98  foreach ($item->getOptions() as $option) {
99  $options[$option->getCode()] = $this->getOptionValues($option->getValue());
100  }
101  return $options;
102  }
103 }
$target
Definition: skip.phtml:8
__construct(Json $serializer=null, JsonValidator $jsonValidator=null)
Definition: Compare.php:34
$value
Definition: gender.phtml:16
compare(Item $target, Item $compared)
Definition: Compare.php:69
if(!isset($_GET['name'])) $name
Definition: log.php:14