Passed
Push — main ( 394e0a...ff735f )
by Daryl
03:03
created

Base::parseArgs()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 3
nc 3
nop 2
crap 3
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 72
    public function __construct(array $args = [])
34
    {
35 72
        $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 72
    protected function setState(array $args = []): void
49
    {
50 72
        foreach ($args as $key => $value) {
51 68
            $property = "_{$key}";
52
53 68
            if (property_exists($this, $property)) {
54 12
                $this->{$property} = $value;
55
56 12
                continue;
57
            }
58
59 57
            if (property_exists($this, $key)) {
60 21
                $this->{$key} = $value;
61
            }
62
63 57
            $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
    /**
98
     * Get ID value if it exists
99
     *
100
     * @return int The ID value or 0 if not found
101
     */
102
    public function getId(): int
103
    {
104
        $id = $this->extraArgs()['id'] ?? 0;
105
106
        return intval($id);
107
    }
108
109
    /**
110
     * Get name value if it exists
111
     *
112
     * @return string The name value or empty string if not found
113
     */
114
    public function getName(): string
115
    {
116
        $name = $this->extraArgs()['name'] ?? '';
117
118
        return (string)$name;
119
    }
120
121
    /**
122
     * Get description value if it exists
123
     *
124
     * @return string The description value or empty string if not found
125
     */
126
    public function getDescription(): string
127
    {
128
        $description = $this->extraArgs()['description'] ?? '';
129
130
        return (string)$description;
131
    }
132
}
133