12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- <?php
- namespace Juju\PDO;
- class Filter {
- private static $filters = [];
- public $name;
- private $filter;
- private $sprintf;
- private function __construct(string $name = null, string $filter){
- $this->filter = $filter;
- if(!is_null($name)){
- $this->name = $name;
- self::$filters[$name] = $this;
- }
- }
- public function name(){
- return $this->name;
- }
- public function __toString(){
- return $this->filter;
- }
- public static function named(string $name){
- if(!isset(self::$filters[$name])){
- throw new \Exception("Filter {$name} is not defined");
- }
- return self::$filters[$name];
- }
- public static function add(string $name, string $filter){
- if(is_null($name)){
- throw new Exception("Named filters cannot have a null name");
- }
- if(isset(self::$filters[$name])){
- throw new \Exception("Filter {$name} is already defined");
- }
- return new Filter($name, $filter);
- }
- public static function create(string $filter){
- return new Filter(null, $filter);
- }
- }
- Filter::add('not null', 'is not null');
- Filter::add('null', 'is null');
- ?>
|