Skip to content

Commit 1fc7677

Browse files
committed
refactor API interfaces #10
1 parent 0b2c852 commit 1fc7677

File tree

5 files changed

+329
-62
lines changed

5 files changed

+329
-62
lines changed

src/Api/IArray.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,35 @@
1010
use phpsap\interfaces\Util\IJsonSerializable;
1111

1212
/**
13-
* Interface IElement
13+
* Interface IMember
1414
*
15-
* API elements are struct or table members and have no direction or optional flag
16-
* of their own.
15+
* API struct or table member.
1716
*
1817
* @package phpsap\interfaces\Api
1918
* @author Gregor J.
2019
* @license MIT
2120
*/
22-
interface IElement extends IJsonSerializable
21+
interface IMember extends IJsonSerializable
2322
{
2423
/**
25-
* API element that casts to PHP string.
24+
* API element that casts to PHP bool.
2625
*/
27-
public const TYPE_STRING = 'string';
26+
public const TYPE_BOOLEAN = 'bool';
2827

2928
/**
3029
* API element that casts to PHP int.
3130
*/
3231
public const TYPE_INTEGER = 'int';
3332

3433
/**
35-
* API element that casts to PHP bool.
34+
* API element that casts to PHP float.
3635
*/
37-
public const TYPE_BOOLEAN = 'bool';
36+
public const TYPE_FLOAT = 'float';
3837

3938
/**
40-
* API element that casts to PHP float.
39+
* API element that casts to PHP string.
4140
*/
42-
public const TYPE_FLOAT = 'float';
41+
public const TYPE_STRING = 'string';
4342

4443
/**
4544
* API element that casts to a hexadecimal encoded binary to a binary.
@@ -77,6 +76,22 @@ interface IElement extends IJsonSerializable
7776
*/
7877
public const JSON_NAME = 'name';
7978

79+
/**
80+
* API member constructor.
81+
* @param string $type Either bool or in or float or ...
82+
* @param string $name API struct or table member name.
83+
* @throws IInvalidArgumentException
84+
*/
85+
public function __construct(string $type, string $name);
86+
87+
/**
88+
* Create an instance of this class from an array.
89+
* @param array<string, string> $array Array containing the properties of this class.
90+
* @return IMember
91+
* @throws IInvalidArgumentException
92+
*/
93+
public static function create(array $array): IMember;
94+
8095
/**
8196
* The PHP type of the element.
8297
* @return string
@@ -90,10 +105,10 @@ public function getType(): string;
90105
public function getName(): string;
91106

92107
/**
93-
* Cast a given output value to the implemented value.
94-
* @param bool|int|float|string $value The output to typecast.
108+
* Cast a value according to this class.
109+
* @param float|bool|int|string $value The output to typecast.
95110
* @return bool|int|float|string|SapDateTime|SapDateInterval
96111
* @throws IInvalidArgumentException
97112
*/
98-
public function cast($value);
113+
public function cast(bool|int|float|string $value): bool|int|float|string|SapDateTime|SapDateInterval;
99114
}

src/Api/IStruct.php

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,116 @@
44

55
namespace phpsap\interfaces\Api;
66

7+
use phpsap\interfaces\exceptions\IArrayElementMissingException;
8+
use phpsap\interfaces\exceptions\IInvalidArgumentException;
9+
use phpsap\interfaces\Util\IJsonSerializable;
10+
711
/**
812
* Interface IStruct
913
*
10-
* API extend the logic of arrays to contain member elements.
14+
* API struct containing an associative array as named members.
1115
*
1216
* @package phpsap\interfaces\Api
1317
* @author Gregor J.
1418
* @license MIT
1519
*/
16-
interface IStruct extends IArray
20+
interface IStruct extends IJsonSerializable
1721
{
1822
/**
1923
* API element that casts to an associative array in PHP.
2024
*/
2125
public const TYPE_STRUCT = 'struct';
26+
27+
/**
28+
* API input element.
29+
*/
30+
public const DIRECTION_INPUT = 'input';
31+
32+
/**
33+
* API output element.
34+
*/
35+
public const DIRECTION_OUTPUT = 'output';
36+
37+
/**
38+
* JSON configuration key for type value.
39+
*/
40+
public const JSON_TYPE = 'type';
41+
42+
/**
43+
* JSON configuration key for name value.
44+
*/
45+
public const JSON_NAME = 'name';
46+
47+
/**
48+
* JSON configuration key for direction value.
49+
*/
50+
public const JSON_DIRECTION = 'direction';
51+
52+
/**
53+
* JSON configuration key for is optional flag.
54+
*/
55+
public const JSON_OPTIONAL = 'optional';
56+
57+
/**
58+
* JSON configuration key for members array.
59+
*/
60+
public const JSON_MEMBERS = 'members';
61+
62+
/**
63+
* Table constructor.
64+
* @param string $name API struct name.
65+
* @param string $direction Either input or output
66+
* @param bool $isOptional Is the API struct optional?
67+
* @param IMember[] $members Array of members of the struct.
68+
* @throws IInvalidArgumentException
69+
*/
70+
public function __construct(string $name, string $direction, bool $isOptional, array $members);
71+
72+
/**
73+
* Create an instance of this class from an array.
74+
* @param array<string, string|bool|array<string, string>> $array Array containing the properties of this class.
75+
* @return IStruct
76+
* @throws IInvalidArgumentException
77+
*/
78+
public static function create(array $array): IStruct;
79+
80+
/**
81+
* The PHP type of the element.
82+
* @return string
83+
*/
84+
public function getType(): string;
85+
86+
/**
87+
* The name of the element.
88+
* @return string
89+
*/
90+
public function getName(): string;
91+
92+
/**
93+
* Get the direction of the parameter.
94+
* interface.
95+
* @return string
96+
*/
97+
public function getDirection(): string;
98+
99+
/**
100+
* Is the element optional?
101+
* @return bool
102+
*/
103+
public function isOptional(): bool;
104+
105+
/**
106+
* Returns an array of members.
107+
* @return IMember[]
108+
*/
109+
public function getMembers(): array;
110+
111+
/**
112+
* Cast the values of a given array according to the members of this class.
113+
* @param array<string, mixed> $value The array to typecast.
114+
* @return array<string, mixed>
115+
* @throws IArrayElementMissingException
116+
* @throws IInvalidArgumentException
117+
*/
118+
public function cast(array $value): array;
22119
}

src/Api/ITable.php

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,121 @@
44

55
namespace phpsap\interfaces\Api;
66

7+
use phpsap\interfaces\exceptions\IArrayElementMissingException;
8+
use phpsap\interfaces\exceptions\IInvalidArgumentException;
9+
use phpsap\interfaces\Util\IJsonSerializable;
10+
711
/**
812
* Interface ITable
913
*
10-
* API extend the logic of arrays but contains rows of member elements.
14+
* API table containing an array of associative arrays as columns of a table.
1115
*
1216
* @package phpsap\interfaces\Api
1317
* @author Gregor J.
1418
* @license MIT
1519
*/
16-
interface ITable extends IArray
20+
interface ITable extends IJsonSerializable
1721
{
22+
/**
23+
* API element that casts to a PHP array of associative arrays.
24+
*/
25+
public const TYPE_TABLE = 'table';
26+
1827
/**
1928
* API table element.
2029
*/
2130
public const DIRECTION_TABLE = 'table';
2231

2332
/**
24-
* API element that casts to a PHP array of associative arrays.
33+
* API input element.
2534
*/
26-
public const TYPE_TABLE = 'table';
35+
public const DIRECTION_INPUT = 'input';
36+
37+
/**
38+
* API output element.
39+
*/
40+
public const DIRECTION_OUTPUT = 'output';
41+
42+
/**
43+
* JSON configuration key for type value.
44+
*/
45+
public const JSON_TYPE = 'type';
46+
47+
/**
48+
* JSON configuration key for name value.
49+
*/
50+
public const JSON_NAME = 'name';
51+
52+
/**
53+
* JSON configuration key for direction value.
54+
*/
55+
public const JSON_DIRECTION = 'direction';
56+
57+
/**
58+
* JSON configuration key for is optional flag.
59+
*/
60+
public const JSON_OPTIONAL = 'optional';
61+
62+
/**
63+
* JSON configuration key for members array.
64+
*/
65+
public const JSON_MEMBERS = 'members';
66+
67+
/**
68+
* Table constructor.
69+
* @param string $name API table name.
70+
* @param string $direction Either input or output or table
71+
* @param bool $isOptional Is the API table optional?
72+
* @param IMember[] $members Array of members as columns of the table.
73+
* @throws IInvalidArgumentException
74+
*/
75+
public function __construct(string $name, string $direction, bool $isOptional, array $members);
76+
77+
/**
78+
* Create an instance of this class from an array.
79+
* @param array<string, string|bool|array<string, string>> $array Array containing the properties of this class.
80+
* @return ITable
81+
* @throws IInvalidArgumentException
82+
*/
83+
public static function create(array $array): ITable;
84+
85+
/**
86+
* The PHP type of the element.
87+
* @return string
88+
*/
89+
public function getType(): string;
90+
91+
/**
92+
* The name of the element.
93+
* @return string
94+
*/
95+
public function getName(): string;
96+
97+
/**
98+
* Get the direction of the parameter.
99+
* interface.
100+
* @return string
101+
*/
102+
public function getDirection(): string;
103+
104+
/**
105+
* Is the element optional?
106+
* @return bool
107+
*/
108+
public function isOptional(): bool;
109+
110+
/**
111+
* Returns an array of an array of members.
112+
* @return IMember[]
113+
*/
114+
public function getMembers(): array;
115+
116+
/**
117+
* Cast the values of a given array according to the members of this class.
118+
* @param array<int, array<string, mixed>> $value The array to typecast.
119+
* @return array<int, array<string, mixed>>
120+
* @throws IArrayElementMissingException
121+
* @throws IInvalidArgumentException
122+
*/
123+
public function cast(array $value): array;
27124
}

0 commit comments

Comments
 (0)