Filter.php (2762B)
1 <?php 2 3 namespace PartKeepr\DoctrineReflectionBundle\Filter; 4 5 class Filter implements AssociationPropertyInterface 6 { 7 use AssociationPropertyTrait; 8 9 const TYPE_AND = 'and'; 10 const TYPE_OR = 'or'; 11 12 const OPERATOR_LESS_THAN = '<'; 13 const OPERATOR_GREATER_THAN = '>'; 14 const OPERATOR_EQUALS = '='; 15 const OPERATOR_GREATER_THAN_EQUALS = '>='; 16 const OPERATOR_LESS_THAN_EQUALS = '<='; 17 const OPERATOR_NOT_EQUALS = '!='; 18 const OPERATOR_IN = 'in'; 19 const OPERATOR_LIKE = 'like'; 20 21 const OPERATORS = [ 22 self::OPERATOR_LESS_THAN, 23 self::OPERATOR_GREATER_THAN, 24 self::OPERATOR_EQUALS, 25 self::OPERATOR_GREATER_THAN_EQUALS, 26 self::OPERATOR_LESS_THAN_EQUALS, 27 self::OPERATOR_NOT_EQUALS, 28 self::OPERATOR_IN, 29 self::OPERATOR_LIKE, 30 ]; 31 32 const TYPES = [ 33 self::TYPE_AND, 34 self::TYPE_OR, 35 ]; 36 37 /** 38 * The type. 39 * 40 * @var string 41 */ 42 private $type; 43 44 /** 45 * @var string 46 */ 47 private $operator; 48 49 /** 50 * @var string 51 */ 52 private $value; 53 54 /** 55 * SubFilters. 56 * 57 * @var array 58 */ 59 private $subFilters; 60 61 public function __construct($type = self::TYPE_AND) 62 { 63 $this->setType($type); 64 $this->setSubFilters([]); 65 } 66 67 /** 68 * @return string 69 */ 70 public function getType() 71 { 72 return $this->type; 73 } 74 75 /** 76 * @param string $type 77 * 78 * @throws \Exception 79 */ 80 public function setType($type) 81 { 82 if (!in_array($type, self::TYPES)) { 83 throw new \Exception("Invalid type $type"); 84 } 85 $this->type = $type; 86 } 87 88 /** 89 * @return string 90 */ 91 public function getOperator() 92 { 93 return $this->operator; 94 } 95 96 /** 97 * @param string $operator 98 * 99 * @throws \Exception Thrown if an invalid operator was passed 100 */ 101 public function setOperator($operator) 102 { 103 if (!in_array(strtolower($operator), self::OPERATORS)) { 104 throw new \Exception("Invalid operator $operator"); 105 } 106 $this->operator = strtolower($operator); 107 } 108 109 /** 110 * @return string 111 */ 112 public function getValue() 113 { 114 return $this->value; 115 } 116 117 /** 118 * @param string $value 119 */ 120 public function setValue($value) 121 { 122 $this->value = $value; 123 } 124 125 /** 126 * @return array 127 */ 128 public function getSubFilters() 129 { 130 return $this->subFilters; 131 } 132 133 /** 134 * @param array $subFilters 135 */ 136 public function setSubFilters($subFilters) 137 { 138 $this->subFilters = $subFilters; 139 } 140 141 public function hasSubFilters() 142 { 143 return count($this->subFilters) > 0; 144 } 145 }