Completed
Pull Request — master (#186)
by Andrew
07:13
created

AbstractEntity::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the DigitalOceanV2 library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace DigitalOceanV2\Entity;
13
14
/**
15
 * @author Antoine Corcy <[email protected]>
16
 * @author Graham Campbell <[email protected]>
17
 */
18
abstract class AbstractEntity
19
{
20
    /**
21
     * The model's attributes.
22
     *
23
     * @var array
24
     */
25
    protected $attributes = [];
26
27
    /**
28
     * @param \stdClass|array|null $parameters
29
     */
30
    public function __construct($parameters = null)
31
    {
32
        if (!$parameters) {
33
            return;
34
        }
35
36
        if ($parameters instanceof \stdClass) {
37
            $parameters = get_object_vars($parameters);
38
        }
39
40
        $this->build($parameters);
41
    }
42
43
    /**
44
     * @param array $parameters
45
     */
46
    public function build(array $parameters)
47
    {
48
        foreach ($parameters as $property => $value) {
49
            $property = static::convertToCamelCase($property);
50
51
            if (property_exists($this, $property)) {
52
                $this->$property = $value;
53
            }
54
        }
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function toArray()
61
    {
62
        $settings = [];
63
        $called = get_called_class();
64
65
        $reflection = new \ReflectionClass($called);
66
        $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
67
68
        foreach ($properties as $property) {
69
            $prop = $property->getName();
70
            if (isset($this->$prop) && $property->class == $called) {
71
                $settings[self::convertToSnakeCase($prop)] = $this->$prop;
72
            }
73
        }
74
75
        return $settings;
76
    }
77
78
    /**
79
     * @param string|null $date DateTime string
80
     *
81
     * @return string|null DateTime in ISO8601 format
82
     */
83
    protected static function convertDateTime($date)
84
    {
85
        if (!$date) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $date of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86
            return;
87
        }
88
89
        $date = new \DateTime($date);
90
        $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
91
92
        return $date->format(\DateTime::ISO8601);
93
    }
94
95
    /**
96
     * @param string $str Snake case string
97
     *
98
     * @return string Camel case string
99
     */
100
    protected static function convertToCamelCase($str)
101
    {
102
        $callback = function ($match) {
103
            return strtoupper($match[2]);
104
        };
105
106
        return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str));
107
    }
108
109
    /**
110
     * @param $str Camel case string
111
     *
112
     * @return string Snake case string
113
     */
114
    protected static function convertToSnakeCase($str)
115
    {
116
        return strtolower(implode('_', preg_split('/(?=[A-Z])/', $str)));
117
    }
118
119
    /**
120
     * Dynamically retrieve attributes on the model.
121
     *
122
     * @param  string  $key
123
     * @return mixed
124
     */
125
    public function __get($key)
126
    {
127
        return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
128
    }
129
130
    /**
131
     * Dynamically set attributes on the model.
132
     *
133
     * @param  string  $key
134
     * @param  mixed  $value
135
     * @return void
136
     */
137
    public function __set($key, $value)
138
    {
139
        $this->attributes[$key] = $value;
140
    }
141
}
142