filter.class.php 1.1 KB

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