EndpointRule.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Aws\EndpointV2\Rule;
  3. use Aws\EndpointV2\Ruleset\RulesetStandardLibrary;
  4. use Aws\EndpointV2\Ruleset\RulesetEndpoint;
  5. class EndpointRule extends AbstractRule
  6. {
  7. /** @var array */
  8. private $endpoint;
  9. public function __construct(array $definition)
  10. {
  11. parent::__construct($definition);
  12. $this->endpoint = $definition['endpoint'];
  13. }
  14. /**
  15. * @return array
  16. */
  17. public function getEndpoint()
  18. {
  19. return $this->endpoint;
  20. }
  21. /**
  22. * If all the rule's conditions are met, return the resolved
  23. * endpoint object.
  24. *
  25. * @return RulesetEndpoint | null
  26. */
  27. public function evaluate(array $inputParameters, RulesetStandardLibrary $standardLibrary)
  28. {
  29. if ($this->evaluateConditions($inputParameters, $standardLibrary)) {
  30. return $this->resolve($inputParameters, $standardLibrary);
  31. }
  32. return false;
  33. }
  34. /**
  35. * Given input parameters, resolve an endpoint in its entirety.
  36. *
  37. * @return RulesetEndpoint
  38. */
  39. private function resolve(
  40. array $inputParameters,
  41. RulesetStandardLibrary $standardLibrary
  42. )
  43. {
  44. $uri = $standardLibrary->resolveValue($this->endpoint['url'], $inputParameters);
  45. $properties = isset($this->endpoint['properties'])
  46. ? $this->resolveProperties($this->endpoint['properties'], $inputParameters, $standardLibrary)
  47. : null;
  48. $headers = $this->resolveHeaders($inputParameters, $standardLibrary);
  49. return new RulesetEndpoint($uri, $properties, $headers);
  50. }
  51. /**
  52. * Recurse through an endpoint's `properties` attribute, resolving template
  53. * strings when found. Return the fully resolved attribute.
  54. *
  55. * @return array
  56. */
  57. private function resolveProperties(
  58. $properties,
  59. array $inputParameters,
  60. RulesetStandardLibrary $standardLibrary
  61. )
  62. {
  63. if (is_array($properties)) {
  64. $propertiesArr = [];
  65. foreach($properties as $key => $val) {
  66. $propertiesArr[$key] = $this->resolveProperties($val, $inputParameters, $standardLibrary);
  67. }
  68. return $propertiesArr;
  69. } elseif ($standardLibrary->isTemplate($properties)) {
  70. return $standardLibrary->resolveTemplateString($properties, $inputParameters);
  71. }
  72. return $properties;
  73. }
  74. /**
  75. * If present, iterate through an endpoint's headers attribute resolving
  76. * values along the way. Return the fully resolved attribute.
  77. *
  78. * @return array
  79. */
  80. private function resolveHeaders(
  81. array $inputParameters,
  82. RulesetStandardLibrary $standardLibrary
  83. )
  84. {
  85. $headers = isset($this->endpoint['headers']) ? $this->endpoint['headers'] : null;
  86. if (is_null($headers)) {
  87. return null;
  88. }
  89. $resolvedHeaders = [];
  90. foreach($headers as $headerName => $headerValues) {
  91. $resolvedValues = [];
  92. foreach($headerValues as $value) {
  93. $resolvedValue = $standardLibrary->resolveValue($value, $inputParameters, $standardLibrary);
  94. $resolvedValues[] = $resolvedValue;
  95. }
  96. $resolvedHeaders[$headerName] = $resolvedValues;
  97. }
  98. return $resolvedHeaders;
  99. }
  100. }