1: <?php
2:
3: namespace Chromabits\Nucleus\Data;
4:
5: use ArrayAccess;
6: use Chromabits\Nucleus\Control\Maybe;
7: use Chromabits\Nucleus\Foundation\BaseObject;
8:
9: /**
10: * Class ArrayAccessMap
11: *
12: * A Map backed by an ArrayAccess object.
13: *
14: * @author Eduardo Trujillo <ed@chromabits.com>
15: * @package Chromabits\Nucleus\Data
16: */
17: class ArrayAccessMap extends BaseObject
18: {
19: /**
20: * @var ArrayAccess
21: */
22: protected $value;
23:
24: /**
25: * Construct an instance of a ArrayAccessMap.
26: *
27: * @param ArrayAccess $input
28: */
29: public function __construct(ArrayAccess $input)
30: {
31: parent::__construct();
32:
33: $this->value = $input;
34: }
35:
36: /**
37: * Get the value of the provided key.
38: *
39: * @param string $key
40: *
41: * @return Maybe
42: */
43: public function lookup($key)
44: {
45: if (!$this->member($key)) {
46: return Maybe::nothing();
47: }
48:
49: return Maybe::just($this->value->offsetGet($key));
50: }
51:
52: /**
53: * Return a new Map of the same type containing the added key.
54: *
55: * @param string $key
56: * @param mixed $value
57: *
58: * @return static
59: */
60: public function insert($key, $value)
61: {
62: $cloned = clone $this->value;
63:
64: $cloned->offsetSet($key, $value);
65:
66: return new static($cloned);
67: }
68:
69: /**
70: * Return whether or not the map contains the specified key.
71: *
72: * @param string $key
73: *
74: * @return bool
75: */
76: public function member($key)
77: {
78: return $this->value->offsetExists($key);
79: }
80:
81: /**
82: * Return a new Map of the same type without the specified key.
83: *
84: * @param string $key
85: *
86: * @return static
87: */
88: public function delete($key)
89: {
90: $cloned = clone $this->value;
91:
92: $cloned->offsetUnset($key);
93:
94: return new static($cloned);
95: }
96: }
97: