Passed
Push — main ( ff735f...1e203e )
by Daryl
02:54
created

Base::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Clubdeuce\Tessitura\Base;
4
5
/**
6
 * Base class for Tessitura API resources.
7
 *
8
 * Provides a foundation for API resources with explicit getter methods
9
 * for better IDE support and type safety. Magic methods are still supported
10
 * for backward compatibility but are deprecated for common properties.
11
 *
12
 * @package Clubdeuce\Tessitura\Base
13
 */
14
class Base
15
{
16
    /**
17
     * Holds the state of the object.
18
     *
19
     * This property is used to store the values of the object's properties
20
     * that are set through the constructor or other methods.
21
     *
22
     * @var mixed[]
23
     */
24
    protected array $extraArgs = [];
25
26
    /**
27
     * Base constructor.
28
     *
29
     * Initializes the object with the provided arguments.
30
     *
31
     * @param mixed[] $args
32
     */
33 76
    public function __construct(array $args = [])
34
    {
35 76
        $this->setState($args);
36
    }
37
38
    /**
39
     * Sets the state of the object based on the provided arguments.
40
     *
41
     * This method updates the object's properties based on the keys in the
42
     * provided array. If a key corresponds to a property that exists in the
43
     * object, it will be set. Otherwise, it will be stored in the `_extraArgs`
44
     * array for later use.
45
     *
46
     * @param mixed[] $args
47
     */
48 76
    protected function setState(array $args = []): void
49
    {
50 76
        foreach ($args as $key => $value) {
51 72
            $property = "_{$key}";
52
53 72
            if (property_exists($this, $property)) {
54 12
                $this->{$property} = $value;
55
56 12
                continue;
57
            }
58
59 61
            if (property_exists($this, $key)) {
60 21
                $this->{$key} = $value;
61
            }
62
63 61
            $this->extraArgs[$key] = $value;
64
        }
65
    }
66
67
    /**
68
     * Parses the provided arguments and fills in defaults.
69
     *
70
     * For each key in $defaults that is not present in $args, the default value is added to $args.
71
     *
72
     * @param mixed[] $args     The arguments to parse.
73
     * @param mixed[] $defaults The default values to use for missing arguments.
74
     * @return mixed[] The resulting array with defaults filled in.
75
     */
76 24
    public function parseArgs(array $args = [], array $defaults = []): array
77
    {
78 24
        foreach ($defaults as $key => $value) {
79 24
            if (!array_key_exists($key, $args)) {
80 24
                $args[$key] = $value;
81
            }
82
        }
83
84 24
        return $args;
85
    }
86
87
    /**
88
     * Returns all extra arguments as an associative array.
89
     *
90
     * @return mixed[] The array of extra arguments.
91
     */
92 8
    public function extraArgs(): array
93
    {
94 8
        return $this->extraArgs;
95
    }
96
}
97