fluent.php 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php namespace Laravel;

class Fluent {

	/**
	 * All of the attributes set on the fluent container.
	 *
	 * @var array
	 */
10
	public $attributes = array();
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

	/**
	 * 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)
	{
Taylor Otwell committed
40
		return array_get($this->attributes, $attribute, $default);
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
	}

	/**
	 * 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;
	}

}