Webservice.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace WebserviceBundle\Services;
  3. class Webservice
  4. {
  5. protected $serviceContainer;
  6. public function __construct($serviceContainer)
  7. {
  8. $this->serviceContainer = $serviceContainer;
  9. }
  10. /**
  11. * @param string $webservice
  12. * @param array $params
  13. * @return array
  14. */
  15. public function getChoices($webservice, $params = array())
  16. {
  17. $choices = array();
  18. $objectsJson = $this->getArray($webservice, $params);
  19. foreach ($objectsJson as $object) {
  20. $choices[$object['name']] = $object['id'];
  21. }
  22. return $choices;
  23. }
  24. /**
  25. * @param string $webservice
  26. * @param array $params
  27. * @return array
  28. */
  29. public function getArray($webservice, $params = array())
  30. {
  31. $objectsJson = array();
  32. if ($this->serviceContainer->hasParameter($webservice)) {
  33. $url = $this->serviceContainer->getParameter($webservice);
  34. $url .= '?filters[qb-criteria]';
  35. foreach ($params as $param => $value) {
  36. $url .= "&filters[{$param}]=$value";
  37. }
  38. $objectsJson = json_decode(file_get_contents($url), true);
  39. }
  40. return $objectsJson;
  41. }
  42. /**
  43. * @param string $webservice
  44. * @param array $filters
  45. * @param array $order_by
  46. * @param integer $limit
  47. * @param integer $offset
  48. * @return array
  49. */
  50. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  51. {
  52. if ($this->serviceContainer->hasParameter($webservice)) {
  53. $url = $this->serviceContainer->getParameter($webservice) . "?";
  54. if($filters) {
  55. $url .= http_build_query(array('filters' => $filters));
  56. }
  57. if($order_by) {
  58. $url .= http_build_query(array('order_by' => $order_by));
  59. }
  60. if($limit) {
  61. $url .= "&limit={$limit}";
  62. }
  63. if($offset) {
  64. $url .= "&offset={$offset}";
  65. }
  66. $data = json_decode(file_get_contents($url), true);
  67. //$data['url'] = $url;
  68. return $data;
  69. }
  70. return array();
  71. }
  72. }