1: <?php
2:
3: namespace Deimos\Cookie;
4:
5: use Deimos\Builder\Builder;
6: use Deimos\Cookie\Extensions\Option;
7: use Deimos\Cookie\Extensions\Variable;
8: use Deimos\Helper\Traits\Helper;
9: use Deimos\Secure\Secure;
10:
11: abstract class Extension
12: {
13:
14: const OPTION_LIFETIME = 'lifetime';
15: const OPTION_EXPIRE = 'expire';
16: const OPTION_PATH = 'path';
17: const OPTION_DOMAIN = 'domain';
18: const OPTION_SECURE = 'secure';
19: const OPTION_HTTP_ONLY = 'httpOnly';
20:
21: const SECURE_ALGORITHM = 'algorithm';
22: const SECURE_SECRET = 'secret';
23: const SECURE_IV = 'iv';
24:
25: use Option;
26: use Helper;
27: use Variable;
28:
29: /**
30: * @var Secure[]
31: */
32: private $secure = [];
33:
34: /**
35: * @var SecureDefault
36: */
37: private $secureDefault;
38:
39: /**
40: * Session constructor.
41: *
42: * @param Builder $builder
43: * @param array $options
44: */
45: public function __construct(Builder $builder, array $options = [])
46: {
47: $this->builder = $builder;
48: $this->options = $this->merge($this->options, $options);
49:
50: $this->init();
51: }
52:
53: /**
54: * @param array $options
55: *
56: * @return array
57: */
58: protected function options(array $options = null)
59: {
60: $dataOptions = $this->options;
61:
62: if ($options)
63: {
64: $dataOptions = $this->merge($dataOptions, $options);
65: }
66:
67: return $dataOptions;
68: }
69:
70: /**
71: * @param $options
72: *
73: * @return Secure
74: */
75: protected function secure(array $options)
76: {
77: $option = $options[self::OPTION_SECURE];
78:
79: if (is_array($option))
80: {
81: $key = $this->helper()->json()->encode($option);
82:
83: if (!isset($this->secure[$key]))
84: {
85: $secure = new Secure();
86:
87: if (isset($option[self::SECURE_ALGORITHM]))
88: {
89: $secure->algorithm($option[self::SECURE_ALGORITHM]);
90: }
91:
92: if (isset($option[self::SECURE_SECRET]))
93: {
94: $secure->secret($option[self::SECURE_SECRET]);
95: }
96:
97: if (isset($option[self::SECURE_IV]))
98: {
99: $secure->iv($option[self::SECURE_IV]);
100: }
101:
102: $this->secure[$key] = $secure;
103: }
104:
105: return $this->secure[$key];
106: }
107:
108: if (!$this->secureDefault)
109: {
110: $this->secureDefault = new SecureDefault();
111: }
112:
113: return $this->secureDefault;
114: }
115:
116: /**
117: * @param string $name
118: * @param string $value
119: * @param array $options
120: *
121: * @return string
122: *
123: * @throws \InvalidArgumentException
124: */
125: public function set($name, $value, array $options = [])
126: {
127: /**
128: * @param Secure $secure
129: */
130: if ($value !== null)
131: {
132: $secure = $this->secure($options);
133: $value = $secure->encrypt($value);
134:
135: $this->helper()->arr()->set($this->object, $name, $value);
136: }
137:
138: return $value;
139: }
140:
141: /**
142: * @param string $name
143: *
144: * @return bool
145: */
146: public function remove($name)
147: {
148: if (isset($this->{$name}))
149: {
150: unset($this->object[$name]);
151:
152: return true;
153: }
154:
155: return false;
156: }
157:
158: /**
159: * remove all keys
160: */
161: public final function removeAll()
162: {
163: foreach ($this->object() as $name => &$value)
164: {
165: $this->remove($name);
166: }
167: }
168:
169: /**
170: * @param string $name
171: *
172: * @return mixed
173: */
174: abstract public function __get($name);
175:
176: /**
177: * @param string $name
178: *
179: * @return bool
180: */
181: abstract public function __isset($name);
182:
183: /**
184: * @param string $name
185: * @param mixed $value
186: */
187: abstract public function __set($name, $value);
188:
189: }