123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace WebserviceBundle\Services;
- class Webservice
- {
- protected $serviceContainer;
-
- public function __construct($serviceContainer)
- {
- $this->serviceContainer = $serviceContainer;
- }
- /**
- * @param string $webservice
- * @param array $params
- * @return array
- */
- public function getChoices($webservice, $params = array())
- {
- $choices = array();
- $results = $this->getArray($webservice, $params);
- foreach ($results as $row) {
- $choices[$row['name']] = $row['id'];
- }
- return $choices;
- }
-
- /**
- * @param string $webservice
- * @param array $params
- * @return array
- */
- public function getArray($webservice, $params = array())
- {
- $results = array();
- if ($this->serviceContainer->hasParameter($webservice)) {
- $url = $this->serviceContainer->getParameter($webservice);
- $url .= '?filters[qb-criteria]';
- foreach ($params as $param => $value) {
- $url .= "&filters[{$param}]=$value";
- }
- try {
- $results = json_decode(file_get_contents($url), true);
- } catch (\Exception $ex) {
-
- }
- }
-
- return $results;
- }
- /**
- * @param string $webservice
- * @param array $filters
- * @param array $order_by
- * @param integer $limit
- * @param integer $offset
- * @return array
- */
- public function getData($webservice, $filters = array(), $order_by = array(), $limit = null, $offset = null)
- {
- if ($this->serviceContainer->hasParameter($webservice)) {
- $url = $this->serviceContainer->getParameter($webservice) . "?";
-
- if($filters) {
- $url .= http_build_query(array('filters' => $filters));
- }
-
- if($order_by) {
- $url .= http_build_query(array('order_by' => $order_by));
- }
-
- if($limit) {
- $url .= "&limit={$limit}";
- }
-
- if($offset) {
- $url .= "&offset={$offset}";
- }
- $data = array();
- try {
- $data = json_decode(file_get_contents($url), true);
- } catch (\Exception $ex) {
- }
- //$data['url'] = $url;
- return $data;
- }
- return array();
- }
-
- /**
- * @param string $webservice
- * @param int $id
- * @return string
- */
- public function getById($webservice, $id)
- {
- $result = $this->getArray($webservice, array(
- 'id' => $id
- ));
-
- return isset($result[0]) ? "{$result[0]['id']} - {$result[0]['name']}" : $id;
- }
- }
|