1: <?php
2: namespace NGS\Client;
3:
4: require_once(__DIR__.'/../Utils.php');
5: require_once(__DIR__.'/../Name.php');
6: require_once(__DIR__.'/RestHttp.php');
7:
8: use NGS\Utils;
9: use NGS\Name;
10:
11: 12: 13: 14: 15: 16: 17:
18: class ApplicationProxy
19: {
20: const APPLICATION_URI = 'RestApplication.svc';
21:
22: protected $http;
23:
24: protected static $instance;
25:
26: 27: 28: 29: 30: 31:
32: public function __construct(RestHttp $http = null)
33: {
34: $this->http = $http !== null ? $http : RestHttp::instance();
35: }
36:
37: 38: 39: 40: 41:
42: public static function instance()
43: {
44: if(self::$instance === null)
45: self::$instance = new ApplicationProxy();
46: return self::$instance;
47: }
48:
49: 50: 51: 52: 53: 54: 55: 56:
57: public function get($command, array $expectedCode = array(200), $accept = 'application/json')
58: {
59: return
60: $this->http->sendRequest(
61: self::APPLICATION_URI.'/'.rawurlencode($command),
62: 'GET',
63: null,
64: $expectedCode,
65: $accept);
66: }
67:
68: 69: 70: 71: 72: 73: 74: 75: 76:
77: public function post(
78: $command,
79: array $data = null,
80: array $expectedCode = array(200),
81: $accept = 'application/json')
82: {
83: return
84: $this->postJson(
85: $command,
86: $data !== null ? json_encode($data) : null,
87: $expectedCode,
88: $accept
89: );
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99: 100:
101: public function postJson(
102: $command,
103: $data = null,
104: array $expectedCode = array(200),
105: $accept = 'application/json')
106: {
107: if(!is_string($data) && $data !== null)
108: throw new \InvalidArgumentException('Data must be encoded in json string or null. Data was "'.\NGS\Utils\gettype($data).'"');
109: return
110: $this->http->sendRequest(
111: self::APPLICATION_URI.'/'.rawurlencode($command),
112: 'POST',
113: $data,
114: $expectedCode,
115: $accept);
116: }
117: }
118: