GetJSONExtension.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 string
  32. */
  33. public function getJSON($parameter = 'client', $filters = [])
  34. {
  35. $url = $this->webservice->buildUrl($parameter, $filters);
  36. return current(json_decode($this->webservice->makeGetRequest($url), true));
  37. }
  38. /**
  39. * @return string
  40. */
  41. public function getName()
  42. {
  43. return 'get_json_extension';
  44. }
  45. }