PuShaoWei /
SimpleEloquent
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Childish\support; |
||
| 3 | |||
| 4 | use ArrayAccess; |
||
| 5 | use JsonSerializable; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Fluent |
||
| 9 | * |
||
| 10 | * @author Pu ShaoWei <[email protected]> |
||
| 11 | * @date 2017/12/7 |
||
| 12 | * @package Childish\support |
||
| 13 | * @version 1.0 |
||
| 14 | */ |
||
| 15 | class Fluent implements ArrayAccess, JsonSerializable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * All of the attributes set on the container. |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $attributes = []; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Create a new fluent container instance. |
||
| 26 | * |
||
| 27 | * @param array|object $attributes |
||
| 28 | * @return void|mixed |
||
|
0 ignored issues
–
show
|
|||
| 29 | */ |
||
| 30 | public function __construct($attributes = []) |
||
| 31 | { |
||
| 32 | foreach ($attributes as $key => $value) { |
||
| 33 | $this->attributes[$key] = $value; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get an attribute from the container. |
||
| 39 | * |
||
| 40 | * @param string $key |
||
| 41 | * @param mixed $default |
||
| 42 | * @return mixed |
||
| 43 | */ |
||
| 44 | public function get($key, $default = null) |
||
| 45 | { |
||
| 46 | if (array_key_exists($key, $this->attributes)) { |
||
| 47 | return $this->attributes[$key]; |
||
| 48 | } |
||
| 49 | |||
| 50 | return $default instanceof \Closure ? $default() : $default; |
||
| 51 | |||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Get the attributes from the container. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function getAttributes() |
||
| 60 | { |
||
| 61 | return $this->attributes; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Convert the Fluent instance to an array. |
||
| 66 | * |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function toArray() |
||
| 70 | { |
||
| 71 | return $this->attributes; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Convert the object into something JSON serializable. |
||
| 76 | * |
||
| 77 | * @return array |
||
| 78 | */ |
||
| 79 | public function jsonSerialize() |
||
| 80 | { |
||
| 81 | return $this->toArray(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Convert the Fluent instance to JSON. |
||
| 86 | * |
||
| 87 | * @param int $options |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | public function toJson($options = 0) |
||
| 91 | { |
||
| 92 | return json_encode($this->jsonSerialize(), $options); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Determine if the given offset exists. |
||
| 97 | * |
||
| 98 | * @param string $offset |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | public function offsetExists($offset) |
||
| 102 | { |
||
| 103 | return isset($this->attributes[$offset]); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the value for a given offset. |
||
| 108 | * |
||
| 109 | * @param string $offset |
||
| 110 | * @return mixed |
||
| 111 | */ |
||
| 112 | public function offsetGet($offset) |
||
| 113 | { |
||
| 114 | return $this->get($offset); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Set the value at the given offset. |
||
| 119 | * |
||
| 120 | * @param string $offset |
||
| 121 | * @param mixed $value |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | public function offsetSet($offset, $value) |
||
| 125 | { |
||
| 126 | $this->attributes[$offset] = $value; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Unset the value at the given offset. |
||
| 131 | * |
||
| 132 | * @param string $offset |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function offsetUnset($offset) |
||
| 136 | { |
||
| 137 | unset($this->attributes[$offset]); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Handle dynamic calls to the container to set attributes. |
||
| 142 | * |
||
| 143 | * @param string $method |
||
| 144 | * @param array $parameters |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | public function __call($method, $parameters) |
||
| 148 | { |
||
| 149 | $this->attributes[$method] = count($parameters) > 0 ? $parameters[0] : true; |
||
| 150 | |||
| 151 | return $this; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Dynamically retrieve the value of an attribute. |
||
| 156 | * |
||
| 157 | * @param string $key |
||
| 158 | * @return mixed |
||
| 159 | */ |
||
| 160 | public function __get($key) |
||
| 161 | { |
||
| 162 | return $this->get($key); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Dynamically set the value of an attribute. |
||
| 167 | * |
||
| 168 | * @param string $key |
||
| 169 | * @param mixed $value |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | public function __set($key, $value) |
||
| 173 | { |
||
| 174 | $this->offsetSet($key, $value); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Dynamically check if an attribute is set. |
||
| 179 | * |
||
| 180 | * @param string $key |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function __isset($key) |
||
| 184 | { |
||
| 185 | return $this->offsetExists($key); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Dynamically unset an attribute. |
||
| 190 | * |
||
| 191 | * @param string $key |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | public function __unset($key) |
||
| 195 | { |
||
| 196 | $this->offsetUnset($key); |
||
| 197 | } |
||
| 198 | } |
||
| 199 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.