AbstractLocation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace GuzzleHttp\Command\Guzzle\RequestLocation;
  3. use GuzzleHttp\Command\CommandInterface;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. use GuzzleHttp\Command\Guzzle\Parameter;
  6. use Psr\Http\Message\RequestInterface;
  7. abstract class AbstractLocation implements RequestLocationInterface
  8. {
  9. /** @var string */
  10. protected $locationName;
  11. /**
  12. * Set the name of the location
  13. *
  14. * @param $locationName
  15. */
  16. public function __construct($locationName)
  17. {
  18. $this->locationName = $locationName;
  19. }
  20. /**
  21. * @param CommandInterface $command
  22. * @param RequestInterface $request
  23. * @param Parameter $param
  24. * @return RequestInterface
  25. */
  26. public function visit(
  27. CommandInterface $command,
  28. RequestInterface $request,
  29. Parameter $param
  30. ) {
  31. return $request;
  32. }
  33. /**
  34. * @param CommandInterface $command
  35. * @param RequestInterface $request
  36. * @param Operation $operation
  37. * @return RequestInterface
  38. */
  39. public function after(
  40. CommandInterface $command,
  41. RequestInterface $request,
  42. Operation $operation
  43. ) {
  44. return $request;
  45. }
  46. /**
  47. * Prepare (filter and set desired name for request item) the value for
  48. * request.
  49. *
  50. * @param mixed $value
  51. * @param Parameter $param
  52. *
  53. * @return array|mixed
  54. */
  55. protected function prepareValue($value, Parameter $param)
  56. {
  57. return is_array($value)
  58. ? $this->resolveRecursively($value, $param)
  59. : $param->filter($value);
  60. }
  61. /**
  62. * Recursively prepare and filter nested values.
  63. *
  64. * @param array $value Value to map
  65. * @param Parameter $param Parameter related to the current key.
  66. *
  67. * @return array Returns the mapped array
  68. */
  69. protected function resolveRecursively(array $value, Parameter $param)
  70. {
  71. foreach ($value as $name => &$v) {
  72. switch ($param->getType()) {
  73. case 'object':
  74. if ($subParam = $param->getProperty($name)) {
  75. $key = $subParam->getWireName();
  76. $value[$key] = $this->prepareValue($v, $subParam);
  77. if ($name != $key) {
  78. unset($value[$name]);
  79. }
  80. } elseif ($param->getAdditionalProperties() instanceof Parameter) {
  81. $v = $this->prepareValue($v, $param->getAdditionalProperties());
  82. }
  83. break;
  84. case 'array':
  85. if ($items = $param->getItems()) {
  86. $v = $this->prepareValue($v, $items);
  87. }
  88. break;
  89. }
  90. }
  91. return $param->filter($value);
  92. }
  93. }