GetJSONExtension.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace WebserviceBundle\Twig;
  3. use Buzz\Message\RequestInterface as HttpRequestInterface;
  4. use WebserviceBundle\Services\Webservice;
  5. use WebserviceBundle\Utils\HttpUtils;
  6. class GetJSONExtension extends \Twig_Extension
  7. {
  8. /**
  9. * @var Webservice
  10. */
  11. protected $webservice;
  12. /**
  13. * @param Webservice $webservice
  14. */
  15. public function __construct(Webservice $webservice)
  16. {
  17. $this->webservice = $webservice;
  18. }
  19. /**
  20. * @return array
  21. */
  22. public function getFunctions()
  23. {
  24. return array(
  25. new \Twig_SimpleFunction('get_json', array($this, 'getJSON')),
  26. );
  27. }
  28. /**
  29. * @param int $clientId
  30. *
  31. * @return array
  32. */
  33. public function getJSON($parameter = 'client', $filters = [])
  34. {
  35. $url = $this->webservice->buildUrl($parameter, $filters);
  36. $result = json_decode($this->webservice->makeGetRequest($url), true);
  37. if ($result) {
  38. return current(json_decode($this->webservice->makeGetRequest($url), true));
  39. }
  40. return [];
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function getName()
  46. {
  47. return 'get_json_extension';
  48. }
  49. }