| 1: | <?php |
| 2: | namespace Worldline\Acquiring\Sdk; |
| 3: | |
| 4: | /** |
| 5: | * Class ApiResource |
| 6: | * |
| 7: | * @package Worldline\Acquiring\Sdk |
| 8: | */ |
| 9: | class ApiResource |
| 10: | { |
| 11: | /** |
| 12: | * @var ApiResource|null |
| 13: | */ |
| 14: | private $parent; |
| 15: | |
| 16: | /** |
| 17: | * @var array |
| 18: | */ |
| 19: | protected $context = array(); |
| 20: | |
| 21: | /** |
| 22: | * Creates a new proxy object for a API resource. |
| 23: | * |
| 24: | * @param ApiResource|null $parent The parent resource. |
| 25: | * @param array $context An associative array that maps URI parameters to values. |
| 26: | */ |
| 27: | public function __construct(ApiResource $parent = null, $context = array()) |
| 28: | { |
| 29: | $this->parent = $parent; |
| 30: | $this->context = $context; |
| 31: | } |
| 32: | |
| 33: | /** |
| 34: | * Returns the connection associated with this resource. |
| 35: | * |
| 36: | * @return Communicator |
| 37: | */ |
| 38: | protected function getCommunicator() |
| 39: | { |
| 40: | return $this->parent->getCommunicator(); |
| 41: | } |
| 42: | |
| 43: | /** |
| 44: | * Converts a URI template to a fully qualified URI by replacing |
| 45: | * URI parameters ('{...}') by their corresponding value in |
| 46: | * $this->context. |
| 47: | * |
| 48: | * @param string $template The URL template to instantiate. |
| 49: | * @return string The URL in which the URI parameters have been replaced. |
| 50: | */ |
| 51: | protected function instantiateUri($template) |
| 52: | { |
| 53: | // We assume that API URLs follow the recommendations in |
| 54: | // RFC 1738, and therefore do not use unencoded { and }. |
| 55: | foreach ($this->context as $name => $value) { |
| 56: | $template = str_replace('{' . $name . '}', $value, $template); |
| 57: | } |
| 58: | return $template; |
| 59: | } |
| 60: | } |
| 61: |