-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfluent.php
96 lines (85 loc) · 1.76 KB
/
fluent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php namespace Laravel;
class Fluent {
/**
* All of the attributes set on the fluent container.
*
* @var array
*/
public $attributes = array();
/**
* Create a new fluent container instance.
*
* <code>
* Create a new fluent container with attributes
* $fluent = new Fluent(array('name' => 'Taylor'));
* </code>
*
* @param array $attributes
* @return void
*/
public function __construct($attributes = array())
{
foreach ($attributes as $key => $value)
{
$this->$key = $value;
}
}
/**
* Get an attribute from the fluent container.
*
* @param string $attribute
* @param mixed $default
* @return mixed
*/
public function get($attribute, $default = null)
{
return array_get($this->attributes, $attribute, $default);
}
/**
* Handle dynamic calls to the container to set attributes.
*
* <code>
* // Fluently set the value of a few attributes
* $fluent->name('Taylor')->age(25);
*
* // Set the value of an attribute to true (boolean)
* $fluent->nullable()->name('Taylor');
* </code>
*/
public function __call($method, $parameters)
{
$this->$method = (count($parameters) > 0) ? $parameters[0] : true;
return $this;
}
/**
* Dynamically retrieve the value of an attribute.
*/
public function __get($key)
{
if (array_key_exists($key, $this->attributes))
{
return $this->attributes[$key];
}
}
/**
* Dynamically set the value of an attribute.
*/
public function __set($key, $value)
{
$this->attributes[$key] = $value;
}
/**
* Dynamically check if an attribute is set.
*/
public function __isset($key)
{
return isset($this->attributes[$key]);
}
/**
* Dynamically unset an attribute.
*/
public function __unset($key)
{
unset($this->attributes[$key]);
}
}