1
|
|
|
<?php |
2
|
|
|
namespace Carpenstar\ByBitAPI\Core\Objects; |
3
|
|
|
|
4
|
|
|
use Carpenstar\ByBitAPI\Core\Exceptions\SDKException; |
5
|
|
|
use Carpenstar\ByBitAPI\Core\Helpers\DateTimeHelper; |
6
|
|
|
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface; |
7
|
|
|
|
8
|
|
|
abstract class AbstractParameters implements IParametersInterface |
9
|
|
|
{ |
10
|
|
|
protected array $requiredFields = []; |
11
|
|
|
|
12
|
|
|
protected array $requiredBetweenFields = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public function array(): array |
18
|
|
|
{ |
19
|
|
|
$entity = $this; |
20
|
|
|
$entityMethods = get_class_methods($this); |
21
|
|
|
$params = []; |
22
|
|
|
|
23
|
|
|
array_walk($entityMethods, function ($method) use (&$entity, &$params) { |
24
|
|
|
if (substr($method, 0, 3) == 'get') { |
25
|
|
|
$entityProperty = lcfirst(substr($method, 3)); |
26
|
|
|
if (isset($entity->$entityProperty)) { |
27
|
|
|
|
28
|
|
|
if ($entity->$method() instanceof \DateTime) { |
29
|
|
|
$ePropertyVal = DateTimeHelper::makeTimestampFromDateString($entity->$method()->format("Y-m-d H:i:s")); |
30
|
|
|
} else { |
31
|
|
|
$ePropertyVal = (string)$entity->$method(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$params[$entityProperty] = $ePropertyVal; |
35
|
|
|
|
36
|
|
|
$propIndex = array_search($entityProperty, $entity->requiredFields, true); |
37
|
|
|
|
38
|
|
|
if($propIndex > -1) { |
39
|
|
|
unset($entity->requiredFields[$propIndex]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (!empty($entity->requiredBetweenFields)) { |
43
|
|
|
foreach ($entity->requiredBetweenFields as $index => $condition) { |
44
|
|
|
if (in_array($entityProperty, $condition)) { |
45
|
|
|
unset($entity->requiredBetweenFields[$index]); |
46
|
|
|
break; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
if (!empty($this->requiredFields)) { |
55
|
|
|
throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_FIELD_TEXT, implode(',', $this->requiredFields))); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!empty($this->requiredBetweenFields)) { |
59
|
|
|
$paramsString = ''; |
60
|
|
|
foreach ($this->requiredBetweenFields as $fieldArray) { |
61
|
|
|
$paramsString .= implode(' or ', $fieldArray); |
62
|
|
|
} |
63
|
|
|
$params = $paramsString; |
|
|
|
|
64
|
|
|
|
65
|
|
|
throw new SDKException(sprintf(SDKException::EXCEPTION_REQUIRED_SPECIFY_BETWEEN_FIELDS, $paramsString)); |
66
|
|
|
} |
67
|
|
|
return $params; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function setRequiredField(string $fieldName): self |
71
|
|
|
{ |
72
|
|
|
$this->requiredFields[] = $fieldName; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
protected function setRequiredBetweenField(string $fieldOne, string $fieldTwo): self |
77
|
|
|
{ |
78
|
|
|
$this->requiredBetweenFields[] = [$fieldOne, $fieldTwo]; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
} |