Operation.php 8.2 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. *
  45. * @throws \InvalidArgumentException
  46. */
  47. public function __construct(array $config = [], DescriptionInterface $description = null)
  48. {
  49. static $defaults = [
  50. 'name' => '',
  51. 'httpMethod' => '',
  52. 'uri' => '',
  53. 'responseModel' => null,
  54. 'notes' => '',
  55. 'summary' => '',
  56. 'documentationUrl' => null,
  57. 'deprecated' => false,
  58. 'data' => [],
  59. 'parameters' => [],
  60. 'additionalParameters' => null,
  61. 'errorResponses' => [],
  62. ];
  63. $this->description = $description === null ? new Description([]) : $description;
  64. if (isset($config['extends'])) {
  65. $config = $this->resolveExtends($config['extends'], $config);
  66. }
  67. $this->config = $config + $defaults;
  68. // Account for the old style of using responseClass
  69. if (isset($config['responseClass'])) {
  70. $this->config['responseModel'] = $config['responseClass'];
  71. }
  72. $this->resolveParameters();
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function toArray()
  78. {
  79. return $this->config;
  80. }
  81. /**
  82. * Get the service description that the operation belongs to
  83. *
  84. * @return Description
  85. */
  86. public function getServiceDescription()
  87. {
  88. return $this->description;
  89. }
  90. /**
  91. * Get the params of the operation
  92. *
  93. * @return Parameter[]
  94. */
  95. public function getParams()
  96. {
  97. return $this->parameters;
  98. }
  99. /**
  100. * Get additionalParameters of the operation
  101. *
  102. * @return Parameter|null
  103. */
  104. public function getAdditionalParameters()
  105. {
  106. return $this->additionalParameters;
  107. }
  108. /**
  109. * Check if the operation has a specific parameter by name
  110. *
  111. * @param string $name Name of the param
  112. *
  113. * @return bool
  114. */
  115. public function hasParam($name)
  116. {
  117. return isset($this->parameters[$name]);
  118. }
  119. /**
  120. * Get a single parameter of the operation
  121. *
  122. * @param string $name Parameter to retrieve by name
  123. *
  124. * @return Parameter|null
  125. */
  126. public function getParam($name)
  127. {
  128. return isset($this->parameters[$name])
  129. ? $this->parameters[$name]
  130. : null;
  131. }
  132. /**
  133. * Get the HTTP method of the operation
  134. *
  135. * @return string|null
  136. */
  137. public function getHttpMethod()
  138. {
  139. return $this->config['httpMethod'];
  140. }
  141. /**
  142. * Get the name of the operation
  143. *
  144. * @return string|null
  145. */
  146. public function getName()
  147. {
  148. return $this->config['name'];
  149. }
  150. /**
  151. * Get a short summary of what the operation does
  152. *
  153. * @return string|null
  154. */
  155. public function getSummary()
  156. {
  157. return $this->config['summary'];
  158. }
  159. /**
  160. * Get a longer text field to explain the behavior of the operation
  161. *
  162. * @return string|null
  163. */
  164. public function getNotes()
  165. {
  166. return $this->config['notes'];
  167. }
  168. /**
  169. * Get the documentation URL of the operation
  170. *
  171. * @return string|null
  172. */
  173. public function getDocumentationUrl()
  174. {
  175. return $this->config['documentationUrl'];
  176. }
  177. /**
  178. * Get the name of the model used for processing the response.
  179. *
  180. * @return string
  181. */
  182. public function getResponseModel()
  183. {
  184. return $this->config['responseModel'];
  185. }
  186. /**
  187. * Get whether or not the operation is deprecated
  188. *
  189. * @return bool
  190. */
  191. public function getDeprecated()
  192. {
  193. return $this->config['deprecated'];
  194. }
  195. /**
  196. * Get the URI that will be merged into the generated request
  197. *
  198. * @return string
  199. */
  200. public function getUri()
  201. {
  202. return $this->config['uri'];
  203. }
  204. /**
  205. * Get the errors that could be encountered when executing the operation
  206. *
  207. * @return array
  208. */
  209. public function getErrorResponses()
  210. {
  211. return $this->config['errorResponses'];
  212. }
  213. /**
  214. * Get extra data from the operation
  215. *
  216. * @param string $name Name of the data point to retrieve or null to
  217. * retrieve all of the extra data.
  218. *
  219. * @return mixed|null
  220. */
  221. public function getData($name = null)
  222. {
  223. if ($name === null) {
  224. return $this->config['data'];
  225. } elseif (isset($this->config['data'][$name])) {
  226. return $this->config['data'][$name];
  227. } else {
  228. return null;
  229. }
  230. }
  231. /**
  232. * @return array
  233. */
  234. private function resolveExtends($name, array $config)
  235. {
  236. if (!$this->description->hasOperation($name)) {
  237. throw new \InvalidArgumentException('No operation named '.$name);
  238. }
  239. // Merge parameters together one level deep
  240. $base = $this->description->getOperation($name)->toArray();
  241. $result = $config + $base;
  242. if (isset($base['parameters']) && isset($config['parameters'])) {
  243. $result['parameters'] = $config['parameters'] + $base['parameters'];
  244. }
  245. return $result;
  246. }
  247. /**
  248. * Process the description and extract the parameter config
  249. *
  250. * @return void
  251. */
  252. private function resolveParameters()
  253. {
  254. // Parameters need special handling when adding
  255. foreach ($this->config['parameters'] as $name => $param) {
  256. if (!is_array($param)) {
  257. throw new \InvalidArgumentException(
  258. "Parameters must be arrays, {$this->config['name']}.$name is ".gettype($param)
  259. );
  260. }
  261. $param['name'] = $name;
  262. $this->parameters[$name] = new Parameter(
  263. $param,
  264. ['description' => $this->description]
  265. );
  266. }
  267. if ($this->config['additionalParameters']) {
  268. if (is_array($this->config['additionalParameters'])) {
  269. $this->additionalParameters = new Parameter(
  270. $this->config['additionalParameters'],
  271. ['description' => $this->description]
  272. );
  273. } else {
  274. $this->additionalParameters = $this->config['additionalParameters'];
  275. }
  276. }
  277. }
  278. }