PageRenderTime 78ms CodeModel.GetById 52ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Semantics3/Products.php

https://github.com/benwagle/Negotiatus
PHP | 204 lines | 179 code | 15 blank | 10 comment | 5 complexity | 585e819143cb7ed2e2e53297159a2db6 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class Semantics3_Products extends Api_Connector {
  3. private $_query_result = array();
  4. private $_data_query = array();
  5. /**
  6. * set the products search fields
  7. *
  8. * This function sets the products search fields
  9. */
  10. public function products_field(){
  11. $args = func_get_args();
  12. array_unshift($args, "products");
  13. call_user_func_array("self::add", $args);
  14. return;
  15. }
  16. /**
  17. * set the categories search fields
  18. *
  19. * This function sets the categories search fields
  20. */
  21. public function categories_field($field_name, $field_value){
  22. $args = func_get_args();
  23. array_unshift($args, "categories");
  24. call_user_func_array("self::add", $args);
  25. return;
  26. }
  27. public function offers_field(){
  28. $args = func_get_args();
  29. array_unshift($args, "offers");
  30. call_user_func_array("self::add", $args);
  31. return;
  32. }
  33. public function add(){
  34. $args = func_get_args();
  35. $endpoint = array_shift($args);
  36. if (!array_key_exists($endpoint, $this->_data_query)){
  37. $this->_data_query[$endpoint] = array();
  38. }
  39. $this->_data_query[$endpoint] = array_merge_recursive((array)$this->_data_query[$endpoint], call_user_func_array("self::_nest_arguments", $args) );
  40. }
  41. public function remove(){
  42. $args = func_get_args();
  43. $endpoint = array_shift($args);
  44. $query = &$this->_data_query[$endpoint];
  45. foreach ($args as $value){
  46. if (array_key_exists($value,(array)$query)){
  47. if (end($args) == $value){
  48. unset($query[$value]);
  49. }
  50. else
  51. $query = &$query[$value];
  52. }
  53. else {
  54. throw new Semantics3_ParameterError("Attempted to detele something which didn't exist.");
  55. }
  56. }
  57. }
  58. /**
  59. * get the categories from the API
  60. *
  61. * This function calls the API and returns the categories based on the query
  62. */
  63. public function get_categories(){
  64. $this->_query_result = parent::run_query("categories",json_encode($this->_data_query["categories"]));
  65. return $this->_query_result;
  66. }
  67. public function get_offers(){
  68. $this->_query_result = parent::run_query("offers",json_encode($this->_data_query["offers"]));
  69. return $this->_query_result;
  70. }
  71. private function _nest_arguments(){
  72. $args = func_get_args();
  73. $query_key = $args[0];
  74. if (count($args) == 2){
  75. $query[$query_key] = $args[1];
  76. }
  77. else if (count($args) > 2) {
  78. unset($args[0]);
  79. $args = array_values($args);
  80. $query[$query_key] = call_user_func_array("self::_nest_arguments", $args);
  81. }
  82. return $query;
  83. }
  84. /**
  85. * set the sitedetails fields
  86. *
  87. * This function sets the sitedetails fields
  88. */
  89. public function sitedetails(){
  90. $args = func_get_args();
  91. array_unshift($args, "sitedetails");
  92. call_user_func_array("self::products_field", $args);
  93. }
  94. /**
  95. * set the latestoffers fields
  96. *
  97. * This function sets the latestoffers fields
  98. */
  99. public function latestoffers($field_name, $field_value1, $field_value2){
  100. $args = array("sitedetails", $field_name, $field_value1, $field_value2);
  101. call_user_func_array("self::products_field", $args);
  102. }
  103. public function limit($limit){
  104. $args = array("limit", $limit);
  105. call_user_func_array("self::products_field", $args);
  106. }
  107. public function offset($offset){
  108. $args = array("offset", $offset);
  109. call_user_func_array("self::products_field", $args);
  110. }
  111. public function sort_list($sort_field, $sort_value){
  112. $args = array("sort", $sort_field, $sort_value);
  113. call_user_func_array("self::products_field", $args);
  114. }
  115. private function _get_products_field(){
  116. # Throw exception if no product field
  117. return json_encode($this->_data_query["products"]);
  118. }
  119. public function clear_query(){
  120. $this->_data_query = array();
  121. $this->_query_result = array();
  122. }
  123. public function iterate_products(){
  124. if (!array_key_exists('total_results_count', $this->_query_result) || $this->_query_result['offset'] >= $this->_query_result['total_results_count'])
  125. return 0;
  126. $limit = 10;
  127. if (array_key_exists('limit', $this->_data_query["products"]))
  128. $limit = $this->_data_query["products"]['limit'];
  129. $this->_data_query["products"]['offset'] += $limit;
  130. $this->get_products();
  131. }
  132. public function all_products(){
  133. return array_key_exists('results', $this->_query_result) ? $this->_query_result['results'] : 0;
  134. }
  135. public function query_json($endpoint, $query_json){
  136. $this->_query_result = parent::run_query($endpoint,$query_json);
  137. return $this->_query_result;
  138. }
  139. public function get_query_json($endpoint = null){
  140. if ($endpoint == null){
  141. throw new Semantics3_ParameterError("Query Endpoint was not defined. You need to provide one. Eg: products");
  142. }
  143. return json_encode($this->_data_query[$endpoint]);
  144. }
  145. public function get_query($endpoint = null){
  146. if ($endpoint == null){
  147. throw new Semantics3_ParameterError("Query Endpoint was not defined. You need to provide one. Eg: products");
  148. }
  149. return $this->_data_query[$endpoint];
  150. }
  151. public function get_products(){
  152. $this->_query_result = parent::run_query("products",json_encode($this->_data_query["products"]));
  153. return $this->_query_result;
  154. }
  155. public function query($endpoint, $query_arr = array()){
  156. $this->_query_result = parent::run_query($endpoint,json_encode($query_arr));
  157. return $this->_query_result;
  158. }
  159. }