Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Context.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class Context
15 {
19  const CONTEXT_CURRENCY = 'current_currency';
20 
26  protected $data = [];
27 
31  protected $default = [];
32 
36  private $serializer;
37 
43  public function __construct(array $data = [], array $default = [], Json $serializer = null)
44  {
45  $this->data = $data;
46  $this->default = $default;
47  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
48  }
49 
58  public function setValue($name, $value, $default)
59  {
60  if ($default !== null) {
61  $this->default[$name] = $default;
62  }
63  $this->data[$name] = $value;
64  return $this;
65  }
66 
73  public function unsValue($name)
74  {
75  unset($this->data[$name]);
76  return $this;
77  }
78 
85  public function getValue($name)
86  {
87  return $this->data[$name] ?? ($this->default[$name] ?? null);
88  }
89 
95  public function getData()
96  {
97  $data = [];
98  foreach ($this->data as $name => $value) {
99  if ($value && $value != $this->default[$name]) {
100  $data[$name] = $value;
101  }
102  }
103  return $data;
104  }
105 
111  public function getVaryString()
112  {
113  $data = $this->getData();
114  if (!empty($data)) {
115  ksort($data);
116  return sha1($this->serializer->serialize($data));
117  }
118  return null;
119  }
120 
126  public function toArray()
127  {
128  return [
129  'data' => $this->data,
130  'default' => $this->default
131  ];
132  }
133 }
setValue($name, $value, $default)
Definition: Context.php:58
__construct(array $data=[], array $default=[], Json $serializer=null)
Definition: Context.php:43
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14