Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
arrayaccesswithreferences.php
1<?php
2
4
6
8{
10 protected $sessionData = [];
12 protected $nullPointers = [];
14 protected $strictMode = false;
15
16 public function has($name)
17 {
18 $this->processLazyStart();
19
20 return
21 isset($this->sessionData[$name]) ||
22 (empty($this->nullPointers[$name]) && $this->sessionData && array_key_exists($name, $this->sessionData))
23 ;
24 }
25
26 public function &get($name)
27 {
28 $this->processLazyStart();
29
30 if (!isset($this->sessionData[$name]) && !array_key_exists($name, $this->sessionData))
31 {
32 if ($this->strictMode)
33 {
34 $trace = Helper::getBackTrace(1, DEBUG_BACKTRACE_IGNORE_ARGS)[0];
35 trigger_error("Notice: Undefined index: {$name} in {$trace['function']} called from {$trace['file']} on line {$trace['line']}.\n", E_USER_NOTICE);
36 }
37 $this->nullPointers[$name] = true;
38 }
39
40 return $this->sessionData[$name];
41 }
42
43 public function set($name, $value)
44 {
45 $this->processLazyStart();
46
47 $this->sessionData[$name] = $value;
48 unset($this->nullPointers[$name]);
49 }
50
51 public function remove($name)
52 {
53 $this->processLazyStart();
54
55 unset($this->sessionData[$name], $this->nullPointers[$name]);
56 }
57
58 public function delete($name)
59 {
60 $this->remove($name);
61 }
62
63 public function offsetExists($offset): bool
64 {
65 $this->processLazyStart();
66
67 return isset($this->sessionData[$offset]);
68 }
69
70 #[\ReturnTypeWillChange]
71 public function &offsetGet($offset)
72 {
73 return $this->get($offset);
74 }
75
76 public function offsetSet($offset, $value): void
77 {
78 if ($offset === null)
79 {
80 $this->processLazyStart();
81
82 $this->sessionData[] = $value;
83 }
84 else
85 {
86 $this->set($offset, $value);
87 }
88 }
89
90 public function offsetUnset($offset): void
91 {
92 $this->remove($offset);
93 }
94
95 public function refineReferencesBeforeSave(): void
96 {
97 foreach ($this->nullPointers as $key => $exists)
98 {
99 if ($exists === true && $this->sessionData[$key] === null)
100 {
101 unset($this->sessionData[$key]);
102 }
103 }
104
105 $this->nullPointers = [];
106 }
107
108 protected function processLazyStart()
109 {}
110}