1 | <?php |
||
52 | class JustGivingClient |
||
53 | { |
||
54 | /** |
||
55 | * The clients that have been instantiated. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $clients = []; |
||
60 | |||
61 | /** |
||
62 | * The client to execute the HTTP requests. |
||
63 | * |
||
64 | * @var ClientInterface |
||
65 | */ |
||
66 | protected $httpClient; |
||
67 | |||
68 | /** |
||
69 | * JustGivingClient constructor. |
||
70 | * |
||
71 | * @param ClientInterface $client |
||
72 | */ |
||
73 | 75 | public function __construct($client) |
|
77 | |||
78 | /** |
||
79 | * Allow API classes to be called as properties. Return a singleton client class. |
||
80 | * |
||
81 | * @param string $property |
||
82 | * @return mixed |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | 66 | public function __get($property) |
|
86 | { |
||
87 | 66 | $class = __NAMESPACE__ . '\\ResourceClients\\' . ucfirst($property) . 'Client'; |
|
88 | |||
89 | 66 | if ( ! class_exists($class)) { |
|
90 | 1 | throw new ClassNotFoundException($class); |
|
91 | } |
||
92 | |||
93 | 65 | $this->clients[$class] = isset($this->clients[$class]) |
|
94 | 14 | ? $this->clients[$class] |
|
95 | 65 | : new $class($this->httpClient, $this); |
|
96 | |||
97 | 65 | return $this->clients[$class]; |
|
98 | } |
||
100 |