fluent.php 1.5 KB

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