|
| 1 | +--- |
| 2 | +title: Architecture and conventions |
| 3 | +menuTitle: Architecture |
| 4 | +subtitle: 'To properly create your API it is important to understand the conventions and the architecture 🚀' |
| 5 | +category: Architecture |
| 6 | +position: 11 |
| 7 | +--- |
| 8 | + |
| 9 | +API is built using these conventions: |
| 10 | + |
| 11 | +- **API object:** provides the API interface |
| 12 | +- **Environments:** provides user customization of the API |
| 13 | +- **Endpoints:** provides a way to call API endpoint and return response |
| 14 | + - **Options:** provides ability to send data to the API endpoint |
| 15 | +- **Response:** Holds the response data with defined "options". |
| 16 | + - **Entities:** Data transfer objects for responses. |
| 17 | + - **Transformers:** Transform classes that will transform any data (response array) to entity. |
| 18 | + |
| 19 | +## API |
| 20 | + |
| 21 | +> See [Start building / Usage](/start-building/create-api) |
| 22 | +
|
| 23 | +API is the main class that holds: |
| 24 | + |
| 25 | +- which environment we should use, |
| 26 | +- which endpoints are available |
| 27 | +- defines base headers for the environment |
| 28 | + |
| 29 | +### Conventions |
| 30 | + |
| 31 | +- Always return the type of the endpoint. |
| 32 | +- Use `MyEndpoint::class` syntax instead of strings. |
| 33 | +- Do not alter `__construct`, use Environments for custom data. |
| 34 | + |
| 35 | +#### Name |
| 36 | + |
| 37 | +- If you are using DDD use `Api` namespace in your domain. Like `Github\Api` (for private usage) |
| 38 | +- Name your class `GithubApi` (use Api suffix) |
| 39 | +- For endpoints use `camelCase` naming. |
| 40 | + |
| 41 | +### Example |
| 42 | + |
| 43 | +```php |
| 44 | +namespace MagellanoApi; |
| 45 | + |
| 46 | +use MagellanoApi\Endpoints\Units\UnitsEndpoint; |
| 47 | +use MagellanoApi\Endpoints\UnitsAvailabilities\UnitsAvailabilitiesEndpoint; |
| 48 | +use WrkFlow\ApiSdkBuilder\AbstractApi; |
| 49 | +use WrkFlow\ApiSdkBuilder\Headers\JsonHeaders; |
| 50 | + |
| 51 | +class MagellanoApi extends AbstractApi |
| 52 | +{ |
| 53 | + public function headers(): array |
| 54 | + { |
| 55 | + return [new JsonHeaders()]; |
| 56 | + } |
| 57 | + |
| 58 | + public function units(): UnitsEndpoint |
| 59 | + { |
| 60 | + return $this->makeEndpoint(UnitsEndpoint::class); |
| 61 | + } |
| 62 | + |
| 63 | + public function unitsAvailabilities(): UnitsAvailabilitiesEndpoint |
| 64 | + { |
| 65 | + return $this->makeEndpoint(UnitsAvailabilitiesEndpoint::class); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## ApiFactory |
| 71 | + |
| 72 | +> See [Start building / Usage](/start-building/use-it) |
| 73 | +
|
| 74 | +Used for passing PSR implementations for HTTP/s communication and container factory (for dependency injection). |
| 75 | + |
| 76 | +Factory is later used in your API to build endpoints, responses. |
| 77 | + |
| 78 | +## Transformers |
| 79 | + |
| 80 | +- Try to indicate within the name of class what is the input / output. |
| 81 | +- Always add `transform(IntType $object): OutType` function that will make the transformation. |
| 82 | +- Extend `AbstractJsonTransformer` if you are converting array to entity object. Contains helper method from `WorksWithJson` |
| 83 | + |
| 84 | +```php |
| 85 | +use WrkFlow\ApiSdkBuilder\Transformers\AbstractJsonTransformer; |
| 86 | + |
| 87 | +class JsonToUnitAvailabilityEntity extends AbstractJsonTransformer |
| 88 | +{ |
| 89 | + private const KEY_AVAILABILITY_STATUS = 'availabilityStatus'; |
| 90 | + |
| 91 | + public function transform(array $item): UnitAvailabilityEntity |
| 92 | + { |
| 93 | + $id = $this->getInt($item, 'ID'); |
| 94 | + $isAvailable = $this->getBool($item, 'isAvailable'); |
| 95 | + |
| 96 | + $availabilityStates = []; |
| 97 | + if (array_key_exists(self::KEY_AVAILABILITY_STATUS, $item) === true |
| 98 | + && is_array($item[self::KEY_AVAILABILITY_STATUS]) === true) { |
| 99 | + foreach ($item[self::KEY_AVAILABILITY_STATUS] as $item) { |
| 100 | + $availabilityStates[] = new UnitAvailabilityStateEntity( |
| 101 | + day: $item['day'], |
| 102 | + state: AvailabilityState::from($item['status']), |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return new UnitAvailabilityEntity( |
| 108 | + id: $id, |
| 109 | + isAvailable: $isAvailable, |
| 110 | + availabilityStates: $availabilityStates |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Entities |
| 117 | + |
| 118 | +Entities are Data transfer objects. We do place them in `Entities` namespace when it is used by more responses. Otherwise, it is located in same folder as the endpoint. |
| 119 | + |
| 120 | +The entity should be immutable. |
| 121 | + |
| 122 | +```php |
| 123 | +class UnitAvailabilityEntity |
| 124 | +{ |
| 125 | + /** |
| 126 | + * @param array<UnitAvailabilityStateEntity> $availabilityStates can be empty if hideDetails is true |
| 127 | + */ |
| 128 | + public function __construct( |
| 129 | + public readonly int $id, |
| 130 | + public readonly bool $isAvailable, |
| 131 | + public readonly array $availabilityStates |
| 132 | + ) { |
| 133 | + } |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Concerns (utils) |
| 138 | + |
| 139 | +### WorksWithJson |
| 140 | + |
| 141 | +Trait that allows you to access values from an array with type safe manner. |
| 142 | + |
| 143 | +```php |
| 144 | +/** |
| 145 | + * @param array<string, mixed> $data |
| 146 | + */ |
| 147 | +public function getInt(array $data, string $key): ?int; |
| 148 | + |
| 149 | +/** |
| 150 | + * @param array<string, mixed> $data |
| 151 | + */ |
| 152 | +public function getFloat(array $data, string $key): ?float; |
| 153 | + |
| 154 | +/** |
| 155 | + * @param array<string, mixed> $data |
| 156 | + */ |
| 157 | +public function getBool(array $data, string $key): ?bool; |
| 158 | + |
| 159 | +public function floatVal(mixed $value): ?float; |
| 160 | + |
| 161 | +public function intVal(mixed $value): ?int; |
| 162 | + |
| 163 | +public function boolVal(mixed $value): ?bool; |
| 164 | +``` |
0 commit comments