BillogramApiQuery

Builds queries and fetches pages of remote objects.

Defined (1)

The class is defined in the following location(s).

/Billogram/Api/Query.php  
  1. class Query 
  2. private $typeClass; 
  3. private $filter = array(); 
  4. private $countCached = null; 
  5. private $pageSize = 100; 
  6. private $order = array(); 
  7. private $api; 
  8.  
  9. /** 
  10. * Initiated with the Billogram API object and the parent model as 
  11. * $typeClass. 
  12. * @param $api 
  13. * @param $typeClass 
  14. */ 
  15. public function __construct($api, $typeClass) 
  16. $this->api = $api; 
  17. $this->typeClass = $typeClass; 
  18.  
  19. /** 
  20. * Makes a GET request to the API with parameters for page size,  
  21. * page number, filtering and order values. Returns the API response. 
  22. * @param int $pageNumber 
  23. * @return mixed 
  24. */ 
  25. private function makeQuery($pageNumber = 1) 
  26. $params = array( 
  27. 'page_size' => $this->pageSize,  
  28. 'page' => $pageNumber 
  29. ); 
  30. $params = array_merge($params, $this->filter); 
  31. $params = array_merge($params, $this->order); 
  32. $response = $this->api->get($this->typeClass->url(), $params); 
  33. $this->countCached = $response->meta->total_count; 
  34.  
  35. return $response; 
  36.  
  37. /** 
  38. * Sets which field to order on. $orderDirection can be 'asc' or 'desc'. 
  39. * @param $orderField 
  40. * @param $orderDirection 
  41. * @return $this 
  42. */ 
  43. public function order($orderField, $orderDirection) 
  44. $this->order = array( 
  45. 'order_field' => $orderField,  
  46. 'order_direction' => $orderDirection 
  47. ); 
  48.  
  49. return $this; 
  50.  
  51. /** 
  52. * Sets the page size. 
  53. * @param $pageSize 
  54. * @return $this 
  55. */ 
  56. public function pageSize($pageSize) 
  57. $this->pageSize = $pageSize; 
  58.  
  59. return $this; 
  60.  
  61. /** 
  62. * Total amount of objects matched by the current query, reading this 
  63. * may cause a remote request. 
  64. * @return null 
  65. */ 
  66. public function count() 
  67. if ($this->countCached === null) { 
  68. $pageSize = $this->pageSize; 
  69. $this->pageSize = 1; 
  70. $this->makeQuery(1); 
  71. $this->pageSize = $pageSize; 
  72.  
  73. return $this->countCached; 
  74.  
  75. /** 
  76. * Total number of pages required for all objects based on current pagesize,  
  77. * reading this may cause a remote request. 
  78. * @return float 
  79. */ 
  80. public function totalPages() 
  81. return ceil($this->count() / $this->pageSize); 
  82.  
  83. /** 
  84. * Sets up filtering rules for the query. 
  85. * @param null $filterType 
  86. * @param null $filterField 
  87. * @param null $filterValue 
  88. * @return $this 
  89. */ 
  90. public function makeFilter( 
  91. $filterType = null,  
  92. $filterField = null,  
  93. $filterValue = null 
  94. ) { 
  95. if ($filterType === null && 
  96. $filterField === null && 
  97. $filterValue === null) 
  98. $this->filter = array(); 
  99. else 
  100. $this->filter = array( 
  101. 'filter_type' => $filterType,  
  102. 'filter_field' => $filterField,  
  103. 'filter_value' => $filterValue 
  104. ); 
  105.  
  106. return $this; 
  107.  
  108. /** 
  109. * Removes any previous filtering rules. 
  110. * @return $this 
  111. */ 
  112. public function removeFilter() 
  113. $this->filter = array(); 
  114.  
  115. return $this; 
  116.  
  117. /** 
  118. * Filter by a specific field and an exact value. 
  119. * @param $filterField 
  120. * @param $filterValue 
  121. * @return $this 
  122. */ 
  123. public function filterField($filterField, $filterValue) 
  124. return $this->makeFilter('field', $filterField, $filterValue); 
  125.  
  126. /** 
  127. * Filter by a specific field and looks for prefix matches. 
  128. * @param $filterField 
  129. * @param $filterValue 
  130. * @return $this 
  131. */ 
  132. public function filterPrefix($filterField, $filterValue) 
  133. return $this->makeFilter('field-prefix', $filterField, $filterValue); 
  134.  
  135. /** 
  136. * Filter by a specific field and looks for substring matches. 
  137. * @param $filterField 
  138. * @param $filterValue 
  139. * @return $this 
  140. */ 
  141. public function filterSearch($filterField, $filterValue) 
  142. return $this->makeFilter('field-search', $filterField, $filterValue); 
  143.  
  144. /** 
  145. * Filter on a special query. 
  146. * @param $filterField 
  147. * @param $filterValue 
  148. * @return $this 
  149. */ 
  150. public function filterSpecial($filterField, $filterValue) 
  151. return $this->makeFilter('special', $filterField, $filterValue); 
  152.  
  153. /** 
  154. * Filter by a full data search (exact meaning depends on object type). 
  155. * @param $searchTerms 
  156. * @return $this 
  157. */ 
  158. public function search($searchTerms) 
  159. return $this->makeFilter('special', 'search', $searchTerms); 
  160.  
  161. /** 
  162. * Fetch objects for the one-based page number. 
  163. * @param $pageNumber 
  164. * @return array 
  165. */ 
  166. public function getPage($pageNumber) 
  167. $response = $this->makeQuery($pageNumber); 
  168. $className = $this->typeClass->objectClass; 
  169. $objects = array(); 
  170. if (!isset($response->data) || !$response->data) 
  171. return array(); 
  172. foreach ($response->data as $object) { 
  173. $objects[] = new $className($this->api, $this->typeClass, $object); 
  174.  
  175. return $objects;