fluent.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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($this->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. /**
  72. * Dynamically check if an attribute is set.
  73. */
  74. public function __isset($key)
  75. {
  76. return isset($this->attributes[$key]);
  77. }
  78. /**
  79. * Dynamically unset an attribute.
  80. */
  81. public function __unset($key)
  82. {
  83. unset($this->attributes[$key]);
  84. }
  85. }