AbstractParameters   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 29
c 1
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

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