Overview

Namespaces

  • NGS
    • Client
      • Exception
    • Converter
    • Patterns
  • PHP

Classes

  • AggregateDomainEvent
  • AggregateRoot
  • CubeBuilder
  • DomainEvent
  • GenericSearch
  • Identifiable
  • OlapCube
  • Repository
  • Search
  • Searchable
  • SearchBuilder
  • Snapshot
  • Specification
  • Templater

Interfaces

  • IDomainObject
  • IIdentifiable
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Customized search on domain object
 13:  *
 14:  * @method GenericSearch equals($property, $value)
 15:  * @method GenericSearch eq($property, $value)
 16:  * @method GenericSearch notEquals($property, $value)
 17:  * @method GenericSearch neq($property, $value)
 18:  * @method GenericSearch lessThan($property, $value)
 19:  * @method GenericSearch lt($property, $value)
 20:  * @method GenericSearch lessOrEqualThan($property, $value)
 21:  * @method GenericSearch moreThan($property, $value)
 22:  * @method GenericSearch gt($property, $value)
 23:  * @method GenericSearch moreOrEqualThan($property, $value)
 24:  * @method GenericSearch gte($property, $value)
 25:  * @method GenericSearch valueIn($property, $value)
 26:  * @method GenericSearch in($property, $value)
 27:  * @method GenericSearch valueNotIn($property, $value)
 28:  * @method GenericSearch notIn($property, $value)
 29:  * @method GenericSearch inValue($property, $value)
 30:  * @method GenericSearch notInValue($property, $value)
 31:  * @method GenericSearch startsWithValue($property, $value)
 32:  * @method GenericSearch startsWith($property, $value)
 33:  * @method GenericSearch startsWithCaseInsensitiveValue($property, $value)
 34:  * @method GenericSearch startsWithCI($property, $value)
 35:  * @method GenericSearch notStartsWithValue($property, $value)
 36:  * @method GenericSearch notStartsWith($property, $value)
 37:  * @method GenericSearch notStartsWithCaseInsensitiveValue($property, $value)
 38:  * @method GenericSearch notStartsWithCI($property, $value)
 39:  * @method GenericSearch valueStartsWith($property, $value)
 40:  * @method GenericSearch valueStartsWithCaseInsensitive($property, $value)
 41:  * @method GenericSearch valueNotStartsWith($property, $value)
 42:  * @method GenericSearch valueNotStartsWithCaseInsensitive($property, $value)
 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:      * Creates new search for a domain object
 98:      * @param string $class Existing domain object class name
 99:      * @throws  \InvalidArgumentException If class does not exsit
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:      * Gets array of filters.
124:      * <code>
125:      * array('property' => array('Key'=>'int', 'Value'=>'mixed'))
126:      * </code>
127:      * @return array|null Filters array or null if no filters defined
128:      */
129:     public function getFilters()
130:     {
131:         return $this->filters;
132:     }
133: 
134:     /**
135:      * Gets searched domain object class
136:      *
137:      * @return string Domain object class
138:      */
139:     public function getObject()
140:     {
141:         return $this->domainObject;
142:     }
143: 
144:     /**
145:      * Performs search
146:      *
147:      * @return array
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: 
API documentation generated by ApiGen 2.8.0