Passed
Push — master ( cce6fa...ba0439 )
by Vladislav
11:52 queued 09:54
created

AbstractParameters   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 13
eloc 32
dl 0
loc 64
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequiredBetweenField() 0 4 1
B fetchArray() 0 43 11
A setRequiredField() 0 4 1
1
<?php
2
namespace Carpenstar\ByBitAPI\Core\Objects;
3
4
use Carpenstar\ByBitAPI\Core\Interfaces\IParametersInterface;
5
6
abstract class AbstractParameters implements IParametersInterface
7
{
8
    protected array $requiredFields = [];
9
10
    protected array $requiredBetweenFields = [];
11
12
    /**
13
     * @return array
14
     */
15
    public function fetchArray(): array
16
    {
17
        $entity = $this;
18
        $entityMethods = get_class_methods($this);
19
        $params = [];
20
21
        array_walk($entityMethods, function ($method) use (&$entity, &$params) {
22
            if (substr($method, 0, 3) == 'get') {
23
                $entityProperty = lcfirst(substr($method, 3));
24
                if (isset($entity->$entityProperty)) {
25
                    $params[$entityProperty] = (string)$entity->$method();
26
27
                    $propIndex = array_search($entityProperty, $entity->requiredFields, true);
28
                    if($propIndex > -1 && !empty($params[$entityProperty])) {
29
                        unset($entity->requiredFields[$propIndex]);
30
                    }
31
32
                    if (!empty($entity->requiredBetweenFields)) {
33
                        foreach ($entity->requiredBetweenFields as $index => $condition) {
34
                            if (in_array($entityProperty, $condition)) {
35
                                unset($entity->requiredBetweenFields[$index]);
36
                                break;
37
                            }
38
                        }
39
                    }
40
                }
41
            }
42
        });
43
44
        if (!empty($this->requiredFields)) {
45
            throw new \Exception("You must specify the following parameters: " . implode(',', $this->requiredFields));
46
        }
47
48
        if (!empty($this->requiredBetweenFields)) {
49
            $paramsString = '';
50
            foreach ($this->requiredBetweenFields as $fieldArray) {
51
                $paramsString .= implode(' or ', $fieldArray);
52
            }
53
            $params = $paramsString;
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
54
55
            throw new \Exception("One of two parameters must be specified {$paramsString}");
56
        }
57
        return $params;
58
    }
59
60
    protected function setRequiredField(string $fieldName): self
61
    {
62
        $this->requiredFields[] = $fieldName;
63
        return $this;
64
    }
65
66
    protected function setRequiredBetweenField(string $fieldOne, string $fieldTwo): self
67
    {
68
        $this->requiredBetweenFields[] = [$fieldOne, $fieldTwo];
69
        return $this;
70
    }
71
}