Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • Operation
  • Parameter
  • SchemaFormatter
  • SchemaValidator
  • ServiceDescription
  • ServiceDescriptionLoader

Interfaces

  • OperationInterface
  • ServiceDescriptionInterface
  • ValidatorInterface
  • Overview
  • Namespace
  • Class
  • Tree
  • Todo
  1: <?php
  2: 
  3: namespace Guzzle\Service\Description;
  4: 
  5: use Guzzle\Common\Exception\InvalidArgumentException;
  6: 
  7: /**
  8:  * A ServiceDescription stores service information based on a service document
  9:  */
 10: class ServiceDescription implements ServiceDescriptionInterface
 11: {
 12:     /**
 13:      * @var array Array of {@see OperationInterface} objects
 14:      */
 15:     protected $operations = array();
 16: 
 17:     /**
 18:      * @var array Array of API models
 19:      */
 20:     protected $models = array();
 21: 
 22:     /**
 23:      * @var string Name of the API
 24:      */
 25:     protected $name;
 26: 
 27:     /**
 28:      * @var string API version
 29:      */
 30:     protected $apiVersion;
 31: 
 32:     /**
 33:      * @var string Summary of the API
 34:      */
 35:     protected $description;
 36: 
 37:     /**
 38:      * @var array Any extra API data
 39:      */
 40:     protected $extraData = array();
 41: 
 42:     /**
 43:      * @var ServiceDescriptionLoader Factory used in factory method
 44:      */
 45:     protected static $descriptionLoader;
 46: 
 47:     /**
 48:      * @var string baseUrl/basePath
 49:      */
 50:     protected $baseUrl;
 51: 
 52:     /**
 53:      * {@inheritdoc}
 54:      * @param string|array $config  File to build or array of operation information
 55:      * @param array        $options Service description factory options
 56:      *
 57:      * @return self
 58:      */
 59:     public static function factory($config, array $options = array())
 60:     {
 61:         // @codeCoverageIgnoreStart
 62:         if (!self::$descriptionLoader) {
 63:             self::$descriptionLoader = new ServiceDescriptionLoader();
 64:         }
 65:         // @codeCoverageIgnoreEnd
 66: 
 67:         return self::$descriptionLoader->load($config, $options);
 68:     }
 69: 
 70:     /**
 71:      * Create a new ServiceDescription
 72:      *
 73:      * @param array $config Array of configuration data
 74:      */
 75:     public function __construct(array $config = array())
 76:     {
 77:         $this->fromArray($config);
 78:     }
 79: 
 80:     /**
 81:      * Serialize the service description
 82:      *
 83:      * @return string
 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:      * Unserialize the service description
109:      *
110:      * @param string|array $json JSON data
111:      */
112:     public function unserialize($json)
113:     {
114:         $this->operations = array();
115:         $this->fromArray(json_decode($json, true));
116:     }
117: 
118:     /**
119:      * {@inheritdoc}
120:      */
121:     public function getBaseUrl()
122:     {
123:         return $this->baseUrl;
124:     }
125: 
126:     /**
127:      * Set the baseUrl of the description
128:      *
129:      * @param string $baseUrl Base URL of each operation
130:      *
131:      * @return self
132:      */
133:     public function setBaseUrl($baseUrl)
134:     {
135:         $this->baseUrl = $baseUrl;
136: 
137:         return $this;
138:     }
139: 
140:     /**
141:      * {@inheritdoc}
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:      * {@inheritdoc}
154:      */
155:     public function hasOperation($name)
156:     {
157:         return isset($this->operations[$name]);
158:     }
159: 
160:     /**
161:      * {@inheritdoc}
162:      */
163:     public function getOperation($name)
164:     {
165:         // Lazily retrieve and build operations
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:      * Add a operation to the service description
179:      *
180:      * @param OperationInterface $operation Operation to add
181:      *
182:      * @return self
183:      */
184:     public function addOperation(OperationInterface $operation)
185:     {
186:         $this->operations[$operation->getName()] = $operation->setServiceDescription($this);
187: 
188:         return $this;
189:     }
190: 
191:     /**
192:      * {@inheritdoc}
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:      * {@inheritdoc}
209:      */
210:     public function getModels()
211:     {
212:         // Ensure all models are converted into parameter objects
213:         foreach (array_keys($this->models) as $id) {
214:             $this->getModel($id);
215:         }
216: 
217:         return $this->models;
218:     }
219: 
220:     /**
221:      * {@inheritdoc}
222:      */
223:     public function hasModel($id)
224:     {
225:         return isset($this->models[$id]);
226:     }
227: 
228:     /**
229:      * Add a model to the service description
230:      *
231:      * @param Parameter $model Model to add
232:      *
233:      * @return self
234:      */
235:     public function addModel(Parameter $model)
236:     {
237:         $this->models[$model->getName()] = $model;
238: 
239:         return $this;
240:     }
241: 
242:     /**
243:      * {@inheritdoc}
244:      */
245:     public function getApiVersion()
246:     {
247:         return $this->apiVersion;
248:     }
249: 
250:     /**
251:      * {@inheritdoc}
252:      */
253:     public function getName()
254:     {
255:         return $this->name;
256:     }
257: 
258:     /**
259:      * {@inheritdoc}
260:      */
261:     public function getDescription()
262:     {
263:         return $this->description;
264:     }
265: 
266:     /**
267:      * {@inheritdoc}
268:      */
269:     public function getData($key)
270:     {
271:         return isset($this->extraData[$key]) ? $this->extraData[$key] : null;
272:     }
273: 
274:     /**
275:      * {@inheritdoc}
276:      */
277:     public function setData($key, $value)
278:     {
279:         $this->extraData[$key] = $value;
280: 
281:         return $this;
282:     }
283: 
284:     /**
285:      * Initialize the state from an array
286:      *
287:      * @param array $config Configuration data
288:      * @throws InvalidArgumentException
289:      */
290:     protected function fromArray(array $config)
291:     {
292:         // Keep a list of default keys used in service descriptions that is later used to determine extra data keys
293:         $defaultKeys = array('name', 'models', 'apiVersion', 'baseUrl', 'description');
294:         // Pull in the default configuration values
295:         foreach ($defaultKeys as $key) {
296:             if (isset($config[$key])) {
297:                 $this->{$key} = $config[$key];
298:             }
299:         }
300: 
301:         // Account for the Swagger name for Guzzle's baseUrl
302:         if (isset($config['basePath'])) {
303:             $this->baseUrl = $config['basePath'];
304:         }
305: 
306:         // Ensure that the models and operations properties are always arrays
307:         $this->models = (array) $this->models;
308:         $this->operations = (array) $this->operations;
309: 
310:         // We want to add operations differently than adding the other properties
311:         $defaultKeys[] = 'operations';
312: 
313:         // Create operations for each operation
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:         // Get all of the additional properties of the service description and store them in a data array
325:         foreach (array_diff(array_keys($config), $defaultKeys) as $key) {
326:             $this->extraData[$key] = $config[$key];
327:         }
328:     }
329: }
330: 
php-coveralls API documentation generated by ApiGen 2.8.0