1: <?php
2:
3: namespace Guzzle\Service\Description;
4:
5: use Guzzle\Common\Exception\InvalidArgumentException;
6:
7: 8: 9:
10: class ServiceDescription implements ServiceDescriptionInterface
11: {
12: 13: 14:
15: protected $operations = array();
16:
17: 18: 19:
20: protected $models = array();
21:
22: 23: 24:
25: protected $name;
26:
27: 28: 29:
30: protected $apiVersion;
31:
32: 33: 34:
35: protected $description;
36:
37: 38: 39:
40: protected = array();
41:
42: 43: 44:
45: protected static $descriptionLoader;
46:
47: 48: 49:
50: protected $baseUrl;
51:
52: 53: 54: 55: 56: 57: 58:
59: public static function factory($config, array $options = array())
60: {
61:
62: if (!self::$descriptionLoader) {
63: self::$descriptionLoader = new ServiceDescriptionLoader();
64: }
65:
66:
67: return self::$descriptionLoader->load($config, $options);
68: }
69:
70: 71: 72: 73: 74:
75: public function __construct(array $config = array())
76: {
77: $this->fromArray($config);
78: }
79:
80: 81: 82: 83: 84:
85: public function serialize()
86: {
87: $result = array(
88: 'name' => $this->name,
89: 'apiVersion' => $this->apiVersion,
90: 'baseUrl' => $this->baseUrl,
91: 'description' => $this->description
92: ) + $this->extraData;
93: $result['operations'] = array();
94: foreach ($this->getOperations() as $name => $operation) {
95: $result['operations'][$operation->getName() ?: $name] = $operation->toArray();
96: }
97: if (!empty($this->models)) {
98: $result['models'] = array();
99: foreach ($this->models as $id => $model) {
100: $result['models'][$id] = $model instanceof Parameter ? $model->toArray(): $model;
101: }
102: }
103:
104: return json_encode(array_filter($result));
105: }
106:
107: 108: 109: 110: 111:
112: public function unserialize($json)
113: {
114: $this->operations = array();
115: $this->fromArray(json_decode($json, true));
116: }
117:
118: 119: 120:
121: public function getBaseUrl()
122: {
123: return $this->baseUrl;
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function setBaseUrl($baseUrl)
134: {
135: $this->baseUrl = $baseUrl;
136:
137: return $this;
138: }
139:
140: 141: 142:
143: public function getOperations()
144: {
145: foreach (array_keys($this->operations) as $name) {
146: $this->getOperation($name);
147: }
148:
149: return $this->operations;
150: }
151:
152: 153: 154:
155: public function hasOperation($name)
156: {
157: return isset($this->operations[$name]);
158: }
159:
160: 161: 162:
163: public function getOperation($name)
164: {
165:
166: if (!isset($this->operations[$name])) {
167: return null;
168: }
169:
170: if (!($this->operations[$name] instanceof Operation)) {
171: $this->operations[$name] = new Operation($this->operations[$name], $this);
172: }
173:
174: return $this->operations[$name];
175: }
176:
177: 178: 179: 180: 181: 182: 183:
184: public function addOperation(OperationInterface $operation)
185: {
186: $this->operations[$operation->getName()] = $operation->setServiceDescription($this);
187:
188: return $this;
189: }
190:
191: 192: 193:
194: public function getModel($id)
195: {
196: if (!isset($this->models[$id])) {
197: return null;
198: }
199:
200: if (!($this->models[$id] instanceof Parameter)) {
201: $this->models[$id] = new Parameter($this->models[$id] + array('name' => $id), $this);
202: }
203:
204: return $this->models[$id];
205: }
206:
207: 208: 209:
210: public function getModels()
211: {
212:
213: foreach (array_keys($this->models) as $id) {
214: $this->getModel($id);
215: }
216:
217: return $this->models;
218: }
219:
220: 221: 222:
223: public function hasModel($id)
224: {
225: return isset($this->models[$id]);
226: }
227:
228: 229: 230: 231: 232: 233: 234:
235: public function addModel(Parameter $model)
236: {
237: $this->models[$model->getName()] = $model;
238:
239: return $this;
240: }
241:
242: 243: 244:
245: public function getApiVersion()
246: {
247: return $this->apiVersion;
248: }
249:
250: 251: 252:
253: public function getName()
254: {
255: return $this->name;
256: }
257:
258: 259: 260:
261: public function getDescription()
262: {
263: return $this->description;
264: }
265:
266: 267: 268:
269: public function getData($key)
270: {
271: return isset($this->extraData[$key]) ? $this->extraData[$key] : null;
272: }
273:
274: 275: 276:
277: public function setData($key, $value)
278: {
279: $this->extraData[$key] = $value;
280:
281: return $this;
282: }
283:
284: 285: 286: 287: 288: 289:
290: protected function fromArray(array $config)
291: {
292:
293: $defaultKeys = array('name', 'models', 'apiVersion', 'baseUrl', 'description');
294:
295: foreach ($defaultKeys as $key) {
296: if (isset($config[$key])) {
297: $this->{$key} = $config[$key];
298: }
299: }
300:
301:
302: if (isset($config['basePath'])) {
303: $this->baseUrl = $config['basePath'];
304: }
305:
306:
307: $this->models = (array) $this->models;
308: $this->operations = (array) $this->operations;
309:
310:
311: $defaultKeys[] = 'operations';
312:
313:
314: if (isset($config['operations'])) {
315: foreach ($config['operations'] as $name => $operation) {
316: if (!($operation instanceof Operation) && !is_array($operation)) {
317: throw new InvalidArgumentException('Invalid operation in service description: '
318: . gettype($operation));
319: }
320: $this->operations[$name] = $operation;
321: }
322: }
323:
324:
325: foreach (array_diff(array_keys($config), $defaultKeys) as $key) {
326: $this->extraData[$key] = $config[$key];
327: }
328: }
329: }
330: