Completed
Push — master ( 41d0c8...5d03eb )
by Graham
688:52 queued 607:19
created

AbstractEntity::__set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 0
loc 6
rs 9.4286
c 3
b 0
f 1
cc 2
eloc 3
nc 2
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
     * @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
     * @param string|null $date DateTime string
52
     *
53
     * @return string|null DateTime in ISO8601 format
54
     */
55
    protected static function convertDateTime($date)
56
    {
57
        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...
58
            return;
59
        }
60
61
        $date = new \DateTime($date);
62
        $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
63
64
        return $date->format(\DateTime::ISO8601);
65
    }
66
67
    /**
68
     * @param string $str Snake case string
69
     *
70
     * @return string Camel case string
71
     */
72
    protected static function convertToCamelCase($str)
73
    {
74
        $callback = function ($match) {
75
            return strtoupper($match[2]);
76
        };
77
78
        return lcfirst(preg_replace_callback('/(^|_)([a-z])/', $callback, $str));
79
    }
80
}
81