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