Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.62% covered (success)
97.62%
41 / 42
87.50% covered (warning)
87.50%
7 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Model
97.62% covered (success)
97.62%
41 / 42
87.50% covered (warning)
87.50%
7 / 8
20
0.00% covered (danger)
0.00%
0 / 1
 getModelExceptionClass
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUpdateParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResponseListName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResponseName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 toArray
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
4.01
 getUpdateArray
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getInitializedProps
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
 resetObject
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Util;
6
7use ReflectionClass;
8
9/**
10 * @ignore
11 * Models need to implement camelcase properties.
12 * These are converted from underscores from the api to camelCase for JSONMapper to assign them properly.
13 * The reasoning why. Well I didn't feel like having to write custom Renames for each attribute.
14 */
15abstract class Model implements ModelInterface
16{
17    public function getModelExceptionClass() : string
18    {
19        return $this::class.'Exception';
20    }
21
22    /**
23     * Flat array to specify array props that will be checked on whether they should be updated or not.
24     * @return array
25     */
26    public function getUpdateParams() : array
27    {
28        return [];
29    }
30
31    /**
32     * Get the list name of a model.
33     * For instance during a list api call, objects will be wrapped in a parent json.
34     * This allows us to conform with the api and target the specific element without statically defining them.
35     */
36    public function getResponseListName() : string
37    {
38        return rtrim($this->getResponseName(), 's').'s';
39    }
40
41    /**
42     * Get the wrapped response of an individual object
43     * This allows us to conform with the api and target the specific element without statically defining them.
44     */
45    public function getResponseName() : string
46    {
47        $classname = get_class($this);
48        if ($pos = strrpos($classname, '\\'))
49        {
50            $classname = substr($classname, $pos + 1);
51        }
52        return VultrUtil::convertCamelCaseToUnderscore($classname);
53    }
54
55    /**
56     * Will output an array to match the json that we had received originally from the response which is in object form.
57     */
58    public function toArray() : array
59    {
60        $reflection = new ReflectionClass($this);
61
62        $array = [];
63        foreach ($reflection->getProperties() as $property)
64        {
65            // Convert our camelcased properties to underscores.
66            $underscore_prop = VultrUtil::convertCamelCaseToUnderscore($property->getName());
67
68            /**
69             * PHP Versions 8.0 and below will throw an error if checking if its initialized on protected props
70             * Even though they are a child of this class. Its dumb.
71             */
72            if (version_compare(PHP_VERSION, '8.1', '<'))
73            {
74                $property->setAccessible(true);
75            }
76
77            if (!$property->isInitialized($this))
78            {
79                continue;
80            }
81
82            $method_name = 'get'.ucfirst($property->getName());
83            $value = $this->$method_name();
84
85            $array[$underscore_prop] =  $value;
86        }
87
88        return $array;
89    }
90
91    public function getUpdateArray() : array
92    {
93        $update = [];
94        $attributes = $this->toArray();
95        foreach ($this->getUpdateParams() as $param)
96        {
97            if (empty($attributes[$param])) continue;
98
99            $update[$param] = $attributes[$param];
100        }
101
102        return $update;
103    }
104
105    public function getInitializedProps() : array
106    {
107        $params = $this->toArray();
108        $class = new ReflectionClass($this);
109        foreach ($class->getProperties() as $property)
110        {
111            $attr = VultrUtil::convertCamelCaseToUnderscore($property->getName());
112            if (!array_key_exists($attr, $params))
113            {
114                continue;
115            }
116
117            // These are optional values that may be set in a response.
118            if ($property->hasDefaultValue() && $property->getDefaultValue() === $params[$attr])
119            {
120                unset($params[$attr]);
121                continue;
122            }
123        }
124
125        return $params;
126    }
127
128    /**
129     * Reset properties of the object to uninitialized state.
130     */
131    public function resetObject() : void
132    {
133        $reflection = new ReflectionClass($this);
134        foreach ($reflection->getProperties() as $property)
135        {
136            $name = $property->getName();
137            if ($property->hasDefaultValue())
138            {
139                $this->$name = $property->getDefaultValue();
140                continue;
141            }
142
143            unset($this->$name);
144        }
145    }
146}