Options.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace PHPHtmlParser;
  3. /**
  4. * Class Options
  5. *
  6. * @package PHPHtmlParser
  7. * @property bool whitespaceTextNode
  8. * @property bool strict
  9. * @property string|null enforceEncoding
  10. * @property bool cleanupInput
  11. * @property bool removeScripts
  12. * @property bool removeStyles
  13. * @property bool preserveLineBreaks
  14. * @property bool removeDoubleSpace
  15. */
  16. class Options
  17. {
  18. /**
  19. * The default options array
  20. *
  21. * @param array
  22. */
  23. protected $defaults = [
  24. 'whitespaceTextNode' => true,
  25. 'strict' => false,
  26. 'enforceEncoding' => null,
  27. 'cleanupInput' => true,
  28. 'removeScripts' => true,
  29. 'removeStyles' => true,
  30. 'preserveLineBreaks' => false,
  31. 'removeDoubleSpace' => true,
  32. ];
  33. /**
  34. * The list of all current options set.
  35. *
  36. * @param array
  37. */
  38. protected $options = [];
  39. /**
  40. * Sets the default options in the options array
  41. */
  42. public function __construct()
  43. {
  44. $this->options = $this->defaults;
  45. }
  46. /**
  47. * A magic get to call the get() method.
  48. *
  49. * @param string $key
  50. * @return mixed
  51. * @uses $this->get()
  52. */
  53. public function __get($key)
  54. {
  55. return $this->get($key);
  56. }
  57. /**
  58. * Sets a new options param to override the current option array.
  59. *
  60. * @param array $options
  61. * @return Options
  62. * @chainable
  63. */
  64. public function setOptions(array $options): Options
  65. {
  66. foreach ($options as $key => $option) {
  67. $this->options[$key] = $option;
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Gets the value associated to the key, or null if the key is not
  73. * found.
  74. *
  75. * @param string
  76. * @return mixed
  77. */
  78. public function get(string $key)
  79. {
  80. if (isset($this->options[$key])) {
  81. return $this->options[$key];
  82. }
  83. return null;
  84. }
  85. }