Completed
Push — master ( c894bb...4464ce )
by Yassir
11s
created

AbstractEntity::toArray()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 3
nop 0
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
     * @param \stdClass|array|null $parameters
22
     */
23
    public function __construct($parameters = null)
24
    {
25
        if (!$parameters) {
26
            return;
27
        }
28
29
        if ($parameters instanceof \stdClass) {
30
            $parameters = get_object_vars($parameters);
31
        }
32
33
        $this->build($parameters);
34
    }
35
36
    /**
37
     * @param array $parameters
38
     */
39
    public function build(array $parameters)
40
    {
41
        foreach ($parameters as $property => $value) {
42
            $property = static::convertToCamelCase($property);
43
44
            if (property_exists($this, $property)) {
45
                $this->$property = $value;
46
            }
47
        }
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function toArray()
54
    {
55
        $settings = [];
56
        $called = get_called_class();
57
58
        $reflection = new \ReflectionClass($called);
59
        $properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
60
61
        foreach ($properties as $property) {
62
            $prop = $property->getName();
63
            if (isset($this->$prop) && $property->class == $called) {
64
                $settings[self::convertToSnakeCase($prop)] = $this->$prop;
65
            }
66
        }
67
68
        return $settings;
69
    }
70
71
    /**
72
     * @param string|null $date DateTime string
73
     *
74
     * @return string|null DateTime in ISO8601 format
75
     */
76
    protected static function convertDateTime($date)
77
    {
78
        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...
79
            return;
80
        }
81
82
        $date = new \DateTime($date);
83
        $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
84
85
        return $date->format(\DateTime::ISO8601);
86
    }
87
88
    /**
89
     * @param string $str Snake case string
90
     *
91
     * @return string Camel case string
92
     */
93
    protected static function convertToCamelCase($str)
94
    {
95
        $callback = function ($match) {
96
            return strtoupper($match[2]);
97
        };
98
99
        return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str));
100
    }
101
102
    /**
103
     * @param $str Camel case string
104
     *
105
     * @return string Snake case string
106
     */
107
    protected static function convertToSnakeCase($str)
108
    {
109
        return strtolower(implode('_', preg_split('/(?=[A-Z])/', $str)));
110
    }
111
}
112