1 | <?php |
||
9 | use hiapi\commands\BaseCommand; |
||
10 | |||
11 | /** |
||
12 | * Class Endpoint |
||
13 | * |
||
14 | * @author Dmytro Naumenko <[email protected]> |
||
15 | * |
||
16 | * @psalm-suppress PropertyNotSetInConstructor |
||
17 | * @psalm-immutable |
||
18 | */ |
||
19 | final class Endpoint |
||
20 | { |
||
21 | public string $name; |
||
|
|||
22 | public ?string $description; |
||
23 | |||
24 | /** @psalm-var ?class-string */ |
||
25 | public ?string $definedBy; |
||
26 | /** @psalm-var Tenant::CLI|Tenant::WEB */ |
||
27 | public int $tenantMask; |
||
28 | /** @psalm-var Collection|class-string<BaseCommand> */ |
||
29 | public $inputType; |
||
30 | /** @var \Closure[]|callable[] */ |
||
31 | public array $middlewares = []; |
||
32 | /** @psalm-var list<string> */ |
||
33 | public array $permissions = []; |
||
34 | /** @psalm-var Collection|class-string */ |
||
35 | public $returnType; |
||
36 | |||
37 | /** |
||
38 | * // TODO |
||
39 | */ |
||
40 | public $examples; |
||
41 | |||
42 | public static function fromConfig(EndpointConfigurationInterface $config): self |
||
43 | { |
||
44 | $self = new self(); |
||
45 | |||
46 | Assert::notEmpty($config['name'], 'Endpoint MUST have a name'); |
||
47 | $self->name = $config['name']; |
||
48 | $self->definedBy = $config['definitionClassName'] ?? null; |
||
49 | $self->permissions = !empty($config['permission']) ? [$config['permission']] : []; |
||
50 | $self->tenantMask = $config['tenantMask'] ?? Tenant::CLI; |
||
51 | |||
52 | Assert::notEmpty($config['inputType'], 'Endpoint input definition is required'); |
||
53 | $self->inputType = $config['inputType']; |
||
54 | |||
55 | Assert::notEmpty($config['returnType'], 'Endpoint return definition is required'); |
||
56 | $self->returnType = $config['returnType']; |
||
57 | |||
58 | Assert::isArray($config['middlewares'] ?? []); |
||
59 | $self->middlewares = $config['middlewares'] ?? []; |
||
60 | |||
61 | $self->examples = $config['examples'] ?? null; |
||
62 | $self->description = $config['description'] ?? null; |
||
63 | |||
64 | return $self; |
||
65 | } |
||
66 | |||
67 | private function __construct() |
||
68 | { |
||
69 | } |
||
70 | } |
||
71 |