Skip to content

Commit f5f8a41

Browse files
committed
Transport Foundation
0 parents  commit f5f8a41

12 files changed

+217
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# API Transport
2+
3+
Foundation for json<>php data transport

composer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "cubex/api-transport",
3+
"description": "Cubex API Transport",
4+
"keywords": [
5+
"api",
6+
"cubex"
7+
],
8+
"homepage": "http://cubex.io",
9+
"license": "BSD-3-Clause",
10+
"authors": [
11+
{
12+
"name": "Brooke Bryan",
13+
"email": "[email protected]",
14+
"homepage": "http://www.bajb.net"
15+
}
16+
],
17+
"config": {
18+
"optimize-autoloader": true
19+
},
20+
"require": {
21+
"php": ">=7.2",
22+
"ext-json": "*",
23+
"packaged/context": "~1.3",
24+
"packaged/helpers": "~1|~2"
25+
},
26+
"require-dev": {
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Cubex\\ApiTransport\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"Cubex\\ApiTransport\\Tests\\": "tests/"
36+
}
37+
}
38+
}

src/Endpoints/AbstractEndpoint.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Endpoints;
3+
4+
use Cubex\ApiTransport\Permissions\ApiPermission;
5+
6+
abstract class AbstractEndpoint implements ApiEndpoint
7+
{
8+
/**
9+
* Permissions that are required to make a request to this endpoint
10+
*
11+
* @return ApiPermission[]
12+
*/
13+
public function getRequiredPermissions(): array
14+
{
15+
return [];
16+
}
17+
18+
/**
19+
* Permissions that may be used within the endpoint to provide functionality
20+
*
21+
* @return ApiPermission[]
22+
*/
23+
public function getPermissions(): array
24+
{
25+
return [];
26+
}
27+
28+
public function requiresAuthentication(): bool
29+
{
30+
return true;
31+
}
32+
}

src/Endpoints/ApiEndpoint.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Endpoints;
3+
4+
use Cubex\ApiTransport\Permissions\ApiPermission;
5+
6+
interface ApiEndpoint
7+
{
8+
const VERB_HEAD = 'HEAD';
9+
const VERB_GET = 'GET';
10+
const VERB_POST = 'POST';
11+
const VERB_PUT = 'PUT';
12+
const VERB_PATCH = 'PATCH';
13+
const VERB_DELETE = 'DELETE';
14+
const VERB_PURGE = 'PURGE';
15+
const VERB_OPTIONS = 'OPTIONS';
16+
const VERB_TRACE = 'TRACE';
17+
const VERB_CONNECT = 'CONNECT';
18+
19+
/**
20+
* @return string HTTP Verb e.g. GET, POST, DELETE, PATCH
21+
*/
22+
public function getVerb(): string;
23+
24+
/**
25+
* @return string Path the endpoint can be located
26+
*/
27+
public function getPath(): string;
28+
29+
/**
30+
* Payload Class
31+
*
32+
* @return null|string
33+
*/
34+
public function getPayloadClass(): ?string;
35+
36+
/**
37+
* Response Class
38+
*
39+
* @return string
40+
*/
41+
public function getResponseClass(): string;
42+
43+
/**
44+
* Permissions that are required to make a request to this endpoint
45+
*
46+
* @return ApiPermission[]
47+
*/
48+
public function getRequiredPermissions(): array;
49+
50+
/**
51+
* Permissions that may be used within the endpoint to provide functionality
52+
*
53+
* @return ApiPermission[]
54+
*/
55+
public function getPermissions(): array;
56+
57+
/**
58+
* @return bool
59+
*/
60+
public function requiresAuthentication(): bool;
61+
}

src/Exceptions/ApiException.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Exceptions;
3+
4+
interface ApiException extends \Throwable
5+
{
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Exceptions;
3+
4+
class GenericApiException extends \Exception implements ApiException
5+
{
6+
}

src/Payloads/AbstractPayload.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Payloads;
3+
4+
use Packaged\Context\Context;
5+
use Packaged\Helpers\Objects;
6+
use Packaged\Helpers\ValueAs;
7+
8+
abstract class AbstractPayload implements ApiPayload
9+
{
10+
public function isValid(): bool
11+
{
12+
return $this->_validate();
13+
}
14+
15+
protected function _validate(): bool
16+
{
17+
return true;
18+
}
19+
20+
public static function i()
21+
{
22+
return new static();
23+
}
24+
25+
/**
26+
* @param Context $c
27+
*
28+
* @return static
29+
* @throws \Exception
30+
*/
31+
public function fromContext(Context $c)
32+
{
33+
$json = ValueAs::obj(json_decode($c->request()->getContent()), new \stdClass());
34+
Objects::hydrate($this, $json);
35+
return $this;
36+
}
37+
}

src/Payloads/ApiPayload.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Payloads;
3+
4+
interface ApiPayload
5+
{
6+
7+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Permissions;
3+
4+
abstract class AbstractPermission implements ApiPermission
5+
{
6+
}

src/Permissions/ApiPermission.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Permissions;
3+
4+
interface ApiPermission
5+
{
6+
public function getKey(): string;
7+
}

src/Responses/AbstractResponse.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Responses;
3+
4+
abstract class AbstractResponse implements ApiResponse
5+
{
6+
7+
}

src/Responses/ApiResponse.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Cubex\ApiTransport\Responses;
3+
4+
interface ApiResponse
5+
{
6+
7+
}

0 commit comments

Comments
 (0)