Overview

Namespaces

  • PipelinerSales
    • ApiClient
      • Http
      • Query
      • Repository
        • Rest

Classes

  • Criteria
  • Filter
  • Sort
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This file is part of the Pipeliner API client library for PHP
  4:  *
  5:  * Copyright 2014 Pipelinersales, Inc. All Rights Reserved.
  6:  * For the full license information, see the attached LICENSE file.
  7:  */
  8: 
  9: namespace PipelinerSales\ApiClient\Query;
 10: 
 11: use PipelinerSales\ApiClient\Defaults;
 12: use PipelinerSales\ApiClient\PipelinerClientException;
 13: 
 14: /**
 15:  * A conventient wrapper for building the filter string used in queries.
 16:  *
 17:  * For details, see the {@link
 18:  * http://workspace.pipelinersales.com/community/api/data/Querying_rest.html
 19:  * API documentation on querying}.
 20:  *
 21:  * All the magic methods can also be called statically, e.g.
 22:  * <code>
 23:  * Filter::equals("NAME", "Joe")->greaterThan("HEIGHT", 0)
 24:  * </code>
 25:  *
 26:  * For most operators (all except ll, rl, fl and their aliases), DateTime objects can be
 27:  * used as value.
 28:  *
 29:  * @method static Filter eq(string $fieldName, mixed $value) field <b>equals</b> value
 30:  * @method static Filter equals(string $fieldName, mixed $value) alias for {@see eq}
 31:  * @method static Filter ne(string $fieldName, mixed $value) field <b>does not equal</b> value
 32:  * @method static Filter doesNotEqual(string $fieldName, mixed $value) alias for {@see ne}
 33:  * @method static Filter gt(string $fieldName, mixed $value) field is <b>greater than</b> value
 34:  * @method static Filter greaterThan(string $fieldName, mixed $value) alias for {@see gt}
 35:  * @method static Filter lt(string $fieldName, mixed $value) field is <b>less than</b> value
 36:  * @method static Filter lessThan(string $fieldName, mixed $value) alias for {@see lt}
 37:  * @method static Filter ge(string $fieldName, mixed $value) field is <b>greater or equal</b> to value
 38:  * @method static Filter gte(string $fieldName, mixed $value) alias for {@see ge}
 39:  * @method static Filter greaterOrEqual(string $fieldName, mixed $value) alias for {@see ge}
 40:  * @method static Filter le(string $fieldName, mixed $value) field is <b>less or equal</b> to value
 41:  * @method static Filter lte(string $fieldName, mixed $value) alias for {@see le}
 42:  * @method static Filter lessOrEqual(string $fieldName, mixed $value) alias for {@see le}
 43:  * @method static Filter ll(string $fieldName, string $value) field starts with value
 44:  * @method static Filter startsWith(string $fieldName, string $value) alias for {@see ll}
 45:  * @method static Filter rl(string $fieldName, string $value) field ends with value
 46:  * @method static Filter endsWith(string $fieldName, string $value) alias for {@see rl}
 47:  * @method static Filter fl(string $fieldName, string $value) field contains value
 48:  * @method static Filter contains(string $fieldName, string $value) alias for {@see fl}
 49:  * @method static Filter raw(string $filterString) appends a separator &#40;|&#41; followed
 50:  * by the raw filter string to the current filter string
 51:  */
 52: class Filter
 53: {
 54: 
 55:     private $filterString;
 56:     private $dateTimeFormat;
 57:     private static $operators = array(
 58:         'eq' => 'eq',
 59:         'equals' => 'eq',
 60:         'ne' => 'ne',
 61:         'doesNotEqual' => 'ne',
 62:         'gt' => 'gt',
 63:         'greaterThan' => 'gt',
 64:         'lt' => 'lt',
 65:         'lessThan' => 'lt',
 66:         'ge' => 'ge',
 67:         'gte' => 'ge',
 68:         'greaterOrEqual' => 'ge',
 69:         'le' => 'le',
 70:         'lte' => 'le',
 71:         'lessOrEqual' => 'le',
 72:         'll' => 'll',
 73:         'startsWith' => 'll',
 74:         'rl' => 'rl',
 75:         'endsWith', 'rl',
 76:         'fl' => 'fl',
 77:         'contains' => 'fl',
 78:         'raw' => 'raw'
 79:     );
 80: 
 81:     public function __construct($filter = '', $dateTimeFormat = Defaults::DATE_FORMAT)
 82:     {
 83:         if ($filter instanceof Filter) {
 84:             $this->filterString = $filter->filterString;
 85:         } else {
 86:             $this->filterString = $filter;
 87:         }
 88:     }
 89: 
 90:     public function __call($name, $arguments)
 91:     {
 92:         if (isset(self::$operators[$name])) {
 93:             if (!empty($this->filterString)) {
 94:                 $this->filterString .= '|';
 95:             }
 96: 
 97:             if (self::$operators[$name] == 'raw') {
 98:                 $this->filterString .= $arguments[0];
 99:             } else {
100:                 if ($arguments[1] instanceof \DateTime) {
101:                     $dateTimeCopy = clone $arguments[1];
102:                     $dateTimeCopy->setTimezone(new \DateTimeZone('UTC'));
103:                     $arguments[1] = $dateTimeCopy->format($this->dateTimeFormat);
104:                 }
105: 
106:                 $this->filterString .= $arguments[0] . '::' . $arguments[1];
107:                 if (self::$operators[$name] != 'eq') {
108:                     $this->filterString .= '::' . self::$operators[$name];
109:                 }
110:             }
111: 
112:             return $this;
113:         } else {
114:             throw new PipelinerClientException('Invalid filter operator: ' . $name);
115:         }
116:     }
117: 
118:     public static function __callStatic($name, $arguments)
119:     {
120:         if (isset(self::$operators[$name])) {
121:             $filter = new Filter();
122:             $filter->$name($arguments[0], $arguments[1]);
123:             return $filter;
124:         } else {
125:             throw new PipelinerClientException('Invalid filter operator: ' . $name);
126:         }
127:     }
128: 
129:     /**
130:      * Returns the resulting filter string usable in query
131:      * @return string
132:      */
133:     public function getString()
134:     {
135:         return $this->filterString;
136:     }
137: }
138: 
API documentation generated by ApiGen