1: <?php
2: namespace NGS\Patterns;
3:
4: require_once(__DIR__.'/../Converter/PrimitiveConverter.php');
5: require_once(__DIR__.'/../Client/DomainProxy.php');
6:
7: use \NGS\Client\DomainProxy;
8: use \NGS\Converter\PrimitiveConverter;
9: use \NGS\Name;
10:
11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43:
44: class GenericSearch extends Search
45: {
46: private $domainObject;
47: private $filters;
48:
49: private static $filterMap = array(
50: 'equals' => 0,
51: 'eq' => 0,
52:
53: 'notEquals' => 1,
54: 'neq' => 1,
55:
56: 'lessThan' => 2,
57: 'lt' => 2,
58:
59: 'lessOrEqualThan' => 3,
60: 'lte' => 3,
61:
62: 'moreThan' => 4,
63: 'gt' => 4,
64:
65: 'moreOrEqualThan' => 5,
66: 'gte' => 5,
67:
68: 'valueIn' => 6,
69: 'in' => 6,
70:
71: 'valueNotIn' => 7,
72: 'notIn' => 7,
73:
74: 'inValue' => 8,
75:
76: 'notInValue' => 9,
77:
78: 'startsWithValue' => 10,
79: 'startsWith' => 10,
80:
81: 'startsWithCaseInsensitiveValue' => 11,
82: 'startsWithCI' => 11,
83:
84: 'notStartsWithValue' => 12,
85: 'notStartsWith' => 12,
86:
87: 'notStartsWithCaseInsensitiveValue' => 13,
88: 'notStartsWithCI' => 13,
89:
90: 'valueStartsWith' => 14,
91: 'valueStartsWithCaseInsensitive' => 15,
92: 'valueNotStartsWith' => 16,
93: 'valueNotStartsWithCaseInsensitive' => 17
94: );
95:
96: 97: 98: 99: 100:
101: public function __construct($class)
102: {
103: if (!(class_exists($class))) {
104: throw new \InvalidArgumentException('Domain object "'.$class.'" doesnot exist');
105: }
106: $this->domainObject = $class;
107: }
108:
109:
110:
111: public function __call($filter, $params)
112: {
113: if (count($params)<2) {
114: throw new \InvalidArgumentException('No value defined for filter '.$filter.'');
115: }
116: if (!isset($params[0])) {
117: throw new \InvalidArgumentException('No property defined for filter '.$filter.'');
118: }
119: return $this->filter($filter, $params[0], $params[1]);
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function getFilters()
130: {
131: return $this->filters;
132: }
133:
134: 135: 136: 137: 138:
139: public function getObject()
140: {
141: return $this->domainObject;
142: }
143:
144: 145: 146: 147: 148:
149: public function search()
150: {
151: return
152: DomainProxy::instance()->searchGeneric(
153: $this->domainObject,
154: $this->filters,
155: $this->limit,
156: $this->offset,
157: $this->order);
158: }
159:
160: public function count()
161: {
162: return
163: DomainProxy::instance()->countGeneric(
164: $this->domainObject,
165: $this->filters);
166: }
167:
168: private function filter($type, $property, $value)
169: {
170: if(!isset(self::$filterMap[$type])) {
171: throw new \InvalidArgumentException('Undefined filter "'.$type.'"');
172: }
173: if (!property_exists($this->domainObject, $property)) {
174: throw new \InvalidArgumentException('Cannot filter search on "'.$this->domainObject.'"" with non-existing property "'.$property.'"');
175: }
176:
177: $filterId = self::$filterMap[$type];
178: if (!isset($this->filters[$property])) {
179: $this->filters[$property] = array();
180: }
181: $this->filters[$property][] = array(
182: 'Key' => $filterId,
183: 'Value' => json_encode($value)
184: );
185: return $this;
186: }
187: }
188: