| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\Framework\Support; |
| 4 |
|
| 5 |
use ArrayAccess; |
| 6 |
use JsonSerializable; |
| 7 |
use FluentCommunity\Framework\Support\Helper; |
| 8 |
|
| 9 |
class Fluent implements ArrayableInterface, ArrayAccess, JsonableInterface, JsonSerializable |
| 10 |
{ |
| 11 |
/** |
| 12 |
* All of the attributes set on the fluent instance. |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected $attributes = []; |
| 17 |
|
| 18 |
/** |
| 19 |
* Create a new fluent instance. |
| 20 |
* |
| 21 |
* @param array|object $attributes |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct($attributes = []) |
| 25 |
{ |
| 26 |
foreach ((array) $attributes as $key => $value) { |
| 27 |
$this->attributes[$key] = $value; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Get an attribute from the fluent instance. |
| 33 |
* |
| 34 |
* @param string $key |
| 35 |
* @param mixed $default |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public function get($key, $default = null) |
| 39 |
{ |
| 40 |
if (array_key_exists($key, $this->attributes)) { |
| 41 |
return $this->attributes[$key]; |
| 42 |
} |
| 43 |
|
| 44 |
return Helper::value($default); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get the attributes from the fluent instance. |
| 49 |
* |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function getAttributes() |
| 53 |
{ |
| 54 |
return $this->attributes; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Convert the fluent instance to an array. |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function toArray() |
| 63 |
{ |
| 64 |
return $this->attributes; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Convert the object into something JSON serializable. |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
#[\ReturnTypeWillChange] |
| 73 |
public function jsonSerialize() |
| 74 |
{ |
| 75 |
return $this->toArray(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Convert the fluent instance to JSON. |
| 80 |
* |
| 81 |
* @param int $options |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public function toJson($options = 0) |
| 85 |
{ |
| 86 |
return json_encode($this->jsonSerialize(), $options); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Determine if the given offset exists. |
| 91 |
* |
| 92 |
* @param string $offset |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
#[\ReturnTypeWillChange] |
| 96 |
public function offsetExists($offset) |
| 97 |
{ |
| 98 |
return isset($this->attributes[$offset]); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the value for a given offset. |
| 103 |
* |
| 104 |
* @param string $offset |
| 105 |
* @return mixed |
| 106 |
*/ |
| 107 |
#[\ReturnTypeWillChange] |
| 108 |
public function offsetGet($offset) |
| 109 |
{ |
| 110 |
return $this->get($offset); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Set the value at the given offset. |
| 115 |
* |
| 116 |
* @param string $offset |
| 117 |
* @param mixed $value |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
#[\ReturnTypeWillChange] |
| 121 |
public function offsetSet($offset, $value) |
| 122 |
{ |
| 123 |
$this->attributes[$offset] = $value; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Unset the value at the given offset. |
| 128 |
* |
| 129 |
* @param string $offset |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
#[\ReturnTypeWillChange] |
| 133 |
public function offsetUnset($offset) |
| 134 |
{ |
| 135 |
unset($this->attributes[$offset]); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Handle dynamic calls to the fluent instance to set attributes. |
| 140 |
* |
| 141 |
* @param string $method |
| 142 |
* @param array $parameters |
| 143 |
* @return $this |
| 144 |
*/ |
| 145 |
public function __call($method, $parameters) |
| 146 |
{ |
| 147 |
$this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true; |
| 148 |
|
| 149 |
return $this; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Dynamically retrieve the value of an attribute. |
| 154 |
* |
| 155 |
* @param string $key |
| 156 |
* @return mixed |
| 157 |
*/ |
| 158 |
public function __get($key) |
| 159 |
{ |
| 160 |
return $this->get($key); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Dynamically set the value of an attribute. |
| 165 |
* |
| 166 |
* @param string $key |
| 167 |
* @param mixed $value |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
public function __set($key, $value) |
| 171 |
{ |
| 172 |
$this->offsetSet($key, $value); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Dynamically check if an attribute is set. |
| 177 |
* |
| 178 |
* @param string $key |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public function __isset($key) |
| 182 |
{ |
| 183 |
return $this->offsetExists($key); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Dynamically unset an attribute. |
| 188 |
* |
| 189 |
* @param string $key |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public function __unset($key) |
| 193 |
{ |
| 194 |
$this->offsetUnset($key); |
| 195 |
} |
| 196 |
} |
| 197 |
|