| 1: | <?php |
| 2: | namespace Worldline\Connect\Sdk; |
| 3: | |
| 4: | /** |
| 5: | * Class ApiResource |
| 6: | * |
| 7: | * @package Worldline\Connect\Sdk |
| 8: | */ |
| 9: | class ApiResource |
| 10: | { |
| 11: | /** |
| 12: | * @var ApiResource |
| 13: | */ |
| 14: | private $parent; |
| 15: | |
| 16: | /** |
| 17: | * @var array |
| 18: | */ |
| 19: | protected $context = array(); |
| 20: | |
| 21: | /** |
| 22: | * Creates a new proxy object for an API resource. |
| 23: | * |
| 24: | * @param ApiResource $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: | * Returns the client headers with this resource. |
| 45: | * |
| 46: | * @return string |
| 47: | */ |
| 48: | protected function getClientMetaInfo() |
| 49: | { |
| 50: | return $this->parent->getClientMetaInfo(); |
| 51: | } |
| 52: | |
| 53: | /** |
| 54: | * Converts a URI template to a fully qualified URI by replacing |
| 55: | * URI parameters ('{...}') by their corresponding value in |
| 56: | * $this->context. |
| 57: | * |
| 58: | * @param string $template The URL template to instantiate. |
| 59: | * @return string The URL in which the URI parameters have been replaced. |
| 60: | */ |
| 61: | protected function instantiateUri($template) |
| 62: | { |
| 63: | // We assume that API URLs follow the recommendations in |
| 64: | // RFC 1738, and therefore do not use unencoded { and }. |
| 65: | foreach ($this->context as $name => $value) { |
| 66: | $template = str_replace('{' . $name . '}', $value, $template); |
| 67: | } |
| 68: | return $template; |
| 69: | } |
| 70: | } |
| 71: |