Utils::turnObjects()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
eloc 4
c 2
b 1
f 0
nc 2
nop 2
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * The Helper class
4
 *
5
 * @author RN Kushwaha <[email protected]>
6
 *
7
 * @since v0.0.5 <Date: 9th May, 2019>
8
 */
9
10
namespace Dolphin\Utils;
11
12
class Utils
13
{
14
    public function decamelize($string) {
15
        return strtolower(preg_replace(['/([a-z\d])([A-Z])/', '/([^_])([A-Z][a-z])/'], '$1_$2', $string));
16
    }
17
18
    /**
19
     * Turn the stadClass object to the type of calling Model
20
     *
21
     * @param String $destination
22
     * @param Object $sourceObject
23
     * @return Object $destination
24
     *
25
     * @author RN Kushwaha <[email protected]>
26
     * @since v0.0.5
27
     */
28
    public function turnObject($destination, $sourceObject)
29
    {
30
        $destination = new $destination();
31
        if(!is_object($sourceObject)){
32
            return $destination;
33
        }
34
35
        $sourceReflection = new \ReflectionObject($sourceObject);
36
        $destinationReflection = new \ReflectionObject($destination);
37
        $sourceProperties = $sourceReflection->getProperties();
38
39
        foreach ($sourceProperties as $sourceProperty) {
40
            $sourceProperty->setAccessible(true);
41
            $name = $sourceProperty->getName();
42
            $value = $sourceProperty->getValue($sourceObject);
43
44
            if ($destinationReflection->hasProperty($name)) {
45
                $propDest = $destinationReflection->getProperty($name);
46
                $propDest->setAccessible(true);
47
                $propDest->setValue($destination,$value);
48
            } else {
49
                $destination->$name = $value;
50
            }
51
        }
52
53
        return $destination;
54
    }
55
56
     /**
57
     * Turn the stadClass object to the type of calling Model
58
     *
59
     * @param String $destination
60
     * @param Object $sourceObject
61
     * @return Object $destination
62
     *
63
     * @author RN Kushwaha <[email protected]>
64
     * @since v0.0.5
65
     */
66
    public function turnObjects($destination, $data)
67
    {
68
        $destination = new $destination();
69
        if((!is_null($data)) && count($data)){
70
            $destination->data = json_decode(json_encode($data, true));
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $options of json_encode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $destination->data = json_decode(json_encode($data, /** @scrutinizer ignore-type */ true));
Loading history...
71
        }
72
73
        return $destination;
74
    }
75
}
76