Operation.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle;
  3. use GuzzleHttp\Command\ToArrayInterface;
  4. /**
  5. * Guzzle operation
  6. */
  7. class Operation implements ToArrayInterface
  8. {
  9. /** @var array Parameters */
  10. private $parameters = [];
  11. /** @var Parameter Additional parameters schema */
  12. private $additionalParameters;
  13. /** @var DescriptionInterface */
  14. private $description;
  15. /** @var array Config data */
  16. private $config;
  17. /**
  18. * Builds an Operation object using an array of configuration data.
  19. *
  20. * - name: (string) Name of the command
  21. * - httpMethod: (string) HTTP method of the operation
  22. * - uri: (string) URI template that can create a relative or absolute URL
  23. * - parameters: (array) Associative array of parameters for the command.
  24. * Each value must be an array that is used to create {@see Parameter}
  25. * objects.
  26. * - summary: (string) This is a short summary of what the operation does
  27. * - notes: (string) A longer description of the operation.
  28. * - documentationUrl: (string) Reference URL providing more information
  29. * about the operation.
  30. * - responseModel: (string) The model name used for processing response.
  31. * - deprecated: (bool) Set to true if this is a deprecated command
  32. * - errorResponses: (array) Errors that could occur when executing the
  33. * command. Array of hashes, each with a 'code' (the HTTP response code),
  34. * 'phrase' (response reason phrase or description of the error), and
  35. * 'class' (a custom exception class that would be thrown if the error is
  36. * encountered).
  37. * - data: (array) Any extra data that might be used to help build or
  38. * serialize the operation
  39. * - additionalParameters: (null|array) Parameter schema to use when an
  40. * option is passed to the operation that is not in the schema
  41. *
  42. * @param array $config Array of configuration data
  43. * @param DescriptionInterface $description Service description used to resolve models if $ref tags are found
  44. * @throws \InvalidArgumentException
  45. */
  46. public function __construct(array $config = [], DescriptionInterface $description = null)
  47. {
  48. static $defaults = [
  49. 'name' => '',
  50. 'httpMethod' => '',
  51. 'uri' => '',
  52. 'responseModel' => null,
  53. 'notes' => '',
  54. 'summary' => '',
  55. 'documentationUrl' => null,
  56. 'deprecated' => false,
  57. 'data' => [],
  58. 'parameters' => [],
  59. 'additionalParameters' => null,
  60. 'errorResponses' => []
  61. ];
  62. $this->description = $description === null ? new Description([]) : $description;
  63. if (isset($config['extends'])) {
  64. $config = $this->resolveExtends($config['extends'], $config);
  65. }
  66. $this->config = $config + $defaults;
  67. // Account for the old style of using responseClass
  68. if (isset($config['responseClass'])) {
  69. $this->config['responseModel'] = $config['responseClass'];
  70. }
  71. $this->resolveParameters();
  72. }
  73. /**
  74. * @return array
  75. */
  76. public function toArray()
  77. {
  78. return $this->config;
  79. }
  80. /**
  81. * Get the service description that the operation belongs to
  82. *
  83. * @return Description
  84. */
  85. public function getServiceDescription()
  86. {
  87. return $this->description;
  88. }
  89. /**
  90. * Get the params of the operation
  91. *
  92. * @return Parameter[]
  93. */
  94. public function getParams()
  95. {
  96. return $this->parameters;
  97. }
  98. /**
  99. * Get additionalParameters of the operation
  100. *
  101. * @return Parameter|null
  102. */
  103. public function getAdditionalParameters()
  104. {
  105. return $this->additionalParameters;
  106. }
  107. /**
  108. * Check if the operation has a specific parameter by name
  109. *
  110. * @param string $name Name of the param
  111. *
  112. * @return bool
  113. */
  114. public function hasParam($name)
  115. {
  116. return isset($this->parameters[$name]);
  117. }
  118. /**
  119. * Get a single parameter of the operation
  120. *
  121. * @param string $name Parameter to retrieve by name
  122. *
  123. * @return Parameter|null
  124. */
  125. public function getParam($name)
  126. {
  127. return isset($this->parameters[$name])
  128. ? $this->parameters[$name]
  129. : null;
  130. }
  131. /**
  132. * Get the HTTP method of the operation
  133. *
  134. * @return string|null
  135. */
  136. public function getHttpMethod()
  137. {
  138. return $this->config['httpMethod'];
  139. }
  140. /**
  141. * Get the name of the operation
  142. *
  143. * @return string|null
  144. */
  145. public function getName()
  146. {
  147. return $this->config['name'];
  148. }
  149. /**
  150. * Get a short summary of what the operation does
  151. *
  152. * @return string|null
  153. */
  154. public function getSummary()
  155. {
  156. return $this->config['summary'];
  157. }
  158. /**
  159. * Get a longer text field to explain the behavior of the operation
  160. *
  161. * @return string|null
  162. */
  163. public function getNotes()
  164. {
  165. return $this->config['notes'];
  166. }
  167. /**
  168. * Get the documentation URL of the operation
  169. *
  170. * @return string|null
  171. */
  172. public function getDocumentationUrl()
  173. {
  174. return $this->config['documentationUrl'];
  175. }
  176. /**
  177. * Get the name of the model used for processing the response.
  178. *
  179. * @return string
  180. */
  181. public function getResponseModel()
  182. {
  183. return $this->config['responseModel'];
  184. }
  185. /**
  186. * Get whether or not the operation is deprecated
  187. *
  188. * @return bool
  189. */
  190. public function getDeprecated()
  191. {
  192. return $this->config['deprecated'];
  193. }
  194. /**
  195. * Get the URI that will be merged into the generated request
  196. *
  197. * @return string
  198. */
  199. public function getUri()
  200. {
  201. return $this->config['uri'];
  202. }
  203. /**
  204. * Get the errors that could be encountered when executing the operation
  205. *
  206. * @return array
  207. */
  208. public function getErrorResponses()
  209. {
  210. return $this->config['errorResponses'];
  211. }
  212. /**
  213. * Get extra data from the operation
  214. *
  215. * @param string $name Name of the data point to retrieve or null to
  216. * retrieve all of the extra data.
  217. *
  218. * @return mixed|null
  219. */
  220. public function getData($name = null)
  221. {
  222. if ($name === null) {
  223. return $this->config['data'];
  224. } elseif (isset($this->config['data'][$name])) {
  225. return $this->config['data'][$name];
  226. } else {
  227. return null;
  228. }
  229. }
  230. /**
  231. * @param $name
  232. * @param array $config
  233. * @return array
  234. */
  235. private function resolveExtends($name, array $config)
  236. {
  237. if (!$this->description->hasOperation($name)) {
  238. throw new \InvalidArgumentException('No operation named ' . $name);
  239. }
  240. // Merge parameters together one level deep
  241. $base = $this->description->getOperation($name)->toArray();
  242. $result = $config + $base;
  243. if (isset($base['parameters']) && isset($config['parameters'])) {
  244. $result['parameters'] = $config['parameters'] + $base['parameters'];
  245. }
  246. return $result;
  247. }
  248. /**
  249. * Process the description and extract the parameter config
  250. *
  251. * @return void
  252. */
  253. private function resolveParameters()
  254. {
  255. // Parameters need special handling when adding
  256. foreach ($this->config['parameters'] as $name => $param) {
  257. if (!is_array($param)) {
  258. throw new \InvalidArgumentException(
  259. "Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
  260. );
  261. }
  262. $param['name'] = $name;
  263. $this->parameters[$name] = new Parameter(
  264. $param,
  265. ['description' => $this->description]
  266. );
  267. }
  268. if ($this->config['additionalParameters']) {
  269. if (is_array($this->config['additionalParameters'])) {
  270. $this->additionalParameters = new Parameter(
  271. $this->config['additionalParameters'],
  272. ['description' => $this->description]
  273. );
  274. } else {
  275. $this->additionalParameters = $this->config['additionalParameters'];
  276. }
  277. }
  278. }
  279. }