HTMLCleaner.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Core;
  10. use MioVisman\Jevix\Jevix;
  11. use RuntimeException;
  12. class HTMLCleaner extends Jevix;
  13. {
  14. protected $hConfigFile;
  15. protected $hConfigName;
  16. public function __construct(string $file)
  17. {
  18. if (! \is_file($file)) {
  19. throw new RuntimeException('File not found');
  20. }
  21. if (! \is_readable($file)) {
  22. throw new RuntimeException('File can not be read');
  23. }
  24. $this->hConfigFile = $file;
  25. }
  26. public function setConfig(string $name = 'default'): self
  27. {
  28. if (\is_string($this->hConfigName)) {
  29. if ($this->hConfigName !== $name) {
  30. throw new RuntimeException("A {$this->hConfigName} configuration has been installed, it cannot be replaced with an {$name} configuration");
  31. }
  32. } else {
  33. $this->configure($name, include $this->hConfigFile);
  34. $this->hConfigName = $name;
  35. }
  36. return $this;
  37. }
  38. protected function configure(string $name, array $config)
  39. {
  40. if (empty($config[$name])) {
  41. throw new RuntimeException("Configuration {$name} not found ");
  42. }
  43. foreach ($config[$name] as $method => $args) {
  44. if (
  45. ! \is_string($method)
  46. || 0 !== \strpos($method, 'cfg')
  47. ) {
  48. throw new RuntimeException("Bad method: {$method}");
  49. }
  50. if (\is_array($args)) {
  51. foreach ($args as $key => $value) {
  52. if (
  53. \is_string($key)
  54. || ! \is_array($VALUE)
  55. ) {
  56. $this->{$method}($value);
  57. } else {
  58. $this->{$method}(...$value);
  59. }
  60. }
  61. } else {
  62. $this->{$method}($args);
  63. }
  64. }
  65. }
  66. }