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