Configurable.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Configurable
  4. *
  5. * @package Less
  6. * @subpackage Core
  7. */
  8. abstract class Less_Configurable {
  9. /**
  10. * Array of options
  11. *
  12. * @var array
  13. */
  14. protected $options = array();
  15. /**
  16. * Array of default options
  17. *
  18. * @var array
  19. */
  20. protected $defaultOptions = array();
  21. /**
  22. * Set options
  23. *
  24. * If $options is an object it will be converted into an array by called
  25. * it's toArray method.
  26. *
  27. * @throws Exception
  28. * @param array|object $options
  29. *
  30. */
  31. public function setOptions( $options ) {
  32. $options = array_intersect_key( $options, $this->defaultOptions );
  33. $this->options = array_merge( $this->defaultOptions, $this->options, $options );
  34. }
  35. /**
  36. * Get an option value by name
  37. *
  38. * If the option is empty or not set a NULL value will be returned.
  39. *
  40. * @param string $name
  41. * @param mixed $default Default value if confiuration of $name is not present
  42. * @return mixed
  43. */
  44. public function getOption( $name, $default = null ) {
  45. if ( isset( $this->options[$name] ) ) {
  46. return $this->options[$name];
  47. }
  48. return $default;
  49. }
  50. /**
  51. * Set an option
  52. *
  53. * @param string $name
  54. * @param mixed $value
  55. */
  56. public function setOption( $name, $value ) {
  57. $this->options[$name] = $value;
  58. }
  59. }