Webservice.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. if ($this->serviceContainer->hasParameter($webservice)) {
  19. $url = $this->serviceContainer->getParameter($webservice);
  20. $objectsJson = json_decode(file_get_contents($url), true);
  21. foreach ($objectsJson as $object) {
  22. $choices["{$object['name']} - {$object['external_id']}"] = $object['id'];
  23. }
  24. }
  25. return $choices;
  26. }
  27. /**
  28. * @param string $webservice
  29. * @param array $filters
  30. * @param array $order_by
  31. * @param integer $limit
  32. * @param integer $offset
  33. * @return array
  34. */
  35. public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
  36. {
  37. if ($this->serviceContainer->hasParameter($webservice)) {
  38. $url = $this->serviceContainer->getParameter($webservice) . "?";
  39. if($filters) {
  40. $url .= http_build_query(array('filters' => $filters));
  41. }
  42. if($order_by) {
  43. $url .= http_build_query(array('order_by' => $order_by));
  44. }
  45. if($limit) {
  46. $url .= "&limit={$limit}";
  47. }
  48. if($offset) {
  49. $url .= "&offset={$offset}";
  50. }
  51. $data = json_decode(file_get_contents($url), true);
  52. //$data['url'] = $url;
  53. return $data;
  54. }
  55. return array();
  56. }
  57. }