-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApiTrait.php
113 lines (107 loc) · 4.19 KB
/
ApiTrait.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace phpsap\saprfc\Traits;
use phpsap\classes\Api\Element;
use phpsap\classes\Api\Struct;
use phpsap\classes\Api\Table;
use phpsap\classes\Api\Value;
use phpsap\exceptions\SapLogicException;
use phpsap\interfaces\Api\IElement;
use phpsap\interfaces\Api\IStruct;
use phpsap\interfaces\Api\ITable;
use phpsap\interfaces\Api\IValue;
use function array_key_exists;
use function is_array;
use function sprintf;
/**
* Trait ApiTrait
* @package phpsap\saprfc
* @author Gregor J.
* @license MIT
*/
trait ApiTrait
{
/**
* Create either Value, Struct or Table from a given remote function parameter
* or return value.
* @param string $name The name of the parameter or return value.
* @param string $type The type of the parameter or return value.
* @param string $direction The direction indicating whether it's a parameter or
* return value.
* @param array $def The complete API value defintion from the module.
* @return Value|Struct|Table
*/
private function createApiValue($name, $type, $direction, $def)
{
$optional = $def['optional'];
if ($type === ITable::TYPE_TABLE) {
return new Table($name, $direction, $optional, $this->createMembers($def));
}
if ($type === IStruct::TYPE_STRUCT) {
return new Struct($name, $direction, $optional, $this->createMembers($def));
}
return new Value($type, $name, $direction, $optional);
}
/**
* Create either struct or table members from the def array of the remote function API.
* @param array $def The complete API value defintion.
* @return \phpsap\classes\Api\Element[] An array of IElement compatible objects.
* @throws \phpsap\exceptions\SapLogicException In case a datatype is missing in the mappings array.
*/
private function createMembers($def): array
{
$result = [];
if (array_key_exists('typedef', $def) && is_array($def['typedef'])) {
foreach ($def['typedef'] as $name => $member) {
$result[] = new Element($this->mapType($member['type']), $name);
}
}
return $result;
}
/**
* Convert SAP Netweaver RFC types into PHP/SAP types.
* @param string $type The remote function parameter type.
* @return string The PHP/SAP internal data type.
* @throws \phpsap\exceptions\SapLogicException
*/
private function mapType($type): string
{
$mapping = [
'RFCTYPE_DATE' => IElement::TYPE_DATE,
'RFCTYPE_TIME' => IElement::TYPE_TIME,
'RFCTYPE_INT' => IElement::TYPE_INTEGER,
'RFCTYPE_NUM' => IElement::TYPE_INTEGER,
'RFCTYPE_INT1' => IElement::TYPE_INTEGER,
'RFCTYPE_INT2' => IElement::TYPE_INTEGER,
'RFCTYPE_BCD' => IElement::TYPE_FLOAT,
'RFCTYPE_FLOAT' => IElement::TYPE_FLOAT,
'RFCTYPE_CHAR' => IElement::TYPE_STRING,
'RFCTYPE_STRING' => IElement::TYPE_STRING,
'RFCTYPE_BYTE' => IElement::TYPE_HEXBIN,
'RFCTYPE_XSTRING' => IElement::TYPE_HEXBIN,
'RFCTYPE_STRUCTURE' => IStruct::TYPE_STRUCT,
'RFCTYPE_TABLE' => ITable::TYPE_TABLE
];
if (!array_key_exists($type, $mapping)) {
throw new SapLogicException(sprintf('Unknown SAP Netweaver RFC type \'%s\'!', $type));
}
return $mapping[$type];
}
/**
* Convert SAP Netweaver RFC directions into PHP/SAP directions.
* @param string $direction The remote function parameter direction.
* @return string The PHP/SAP internal direction.
* @throws \phpsap\exceptions\SapLogicException
*/
private function mapDirection($direction): string
{
$mapping = [
'RFC_EXPORT' => IValue::DIRECTION_OUTPUT,
'RFC_IMPORT' => IValue::DIRECTION_INPUT,
'RFC_TABLES' => ITable::DIRECTION_TABLE
];
if (!array_key_exists($direction, $mapping)) {
throw new SapLogicException(sprintf('Unknown SAP Netweaver RFC direction \'%s\'!', $direction));
}
return $mapping[$direction];
}
}