-
Notifications
You must be signed in to change notification settings - Fork 22
/
ApiAwareTrait.php
34 lines (26 loc) · 1 KB
/
ApiAwareTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
namespace Payum\Core;
use Payum\Core\Exception\LogicException;
use Payum\Core\Exception\UnsupportedApiException;
use function is_object;
trait ApiAwareTrait
{
/**
* @var mixed
*/
protected $api;
protected string|object|null $apiClass;
public function setApi($api): void
{
if (empty($this->apiClass)) {
throw new LogicException('You must configure apiClass in __constructor method of the class the trait is applied to.');
}
if (is_string($this->apiClass) && ! (class_exists($this->apiClass) || interface_exists($this->apiClass))) {
throw new LogicException(sprintf('Api class not found or invalid class. "%s", $this->apiClass', $this->apiClass));
}
if (! $api instanceof $this->apiClass) {
throw new UnsupportedApiException(sprintf('Not supported api given. It must be an instance of %s', is_object($this->apiClass) ? $this->apiClass::class : $this->apiClass));
}
$this->api = $api;
}
}