filter.class.php 822 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Juju\PDO;
  3. class Filter {
  4. private static $filters = [];
  5. private $name;
  6. private $filter;
  7. private function __construct(string $name, string $filter){
  8. $this->name = $name;
  9. self::$filters[$name] = $this;
  10. }
  11. public function name(){
  12. return $this->name;
  13. }
  14. public function __toString(){
  15. return $this->filter;
  16. }
  17. public static function named(string $name){
  18. if(!isset(self::$filters[$name])){
  19. throw new \Exception("Filter {$name} is not defined");
  20. }
  21. return self::$filters[$name];
  22. }
  23. public static function add(string $name, string $filter){
  24. if(isset(self::$filters[$name])){
  25. throw new \Exception("Filter {$name} is already defined");
  26. }
  27. return new Filter($name, $filter);
  28. }
  29. }
  30. Filter::add('not null', 'is not null');
  31. Filter::add('null', 'is null');
  32. ?>