Passed
Push — master ( 1b5140...aad147 )
by RN
02:02
created

Utils   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 23
c 1
b 1
f 0
dl 0
loc 60
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decamelize() 0 2 1
A turnObject() 0 23 4
A turnObjects() 0 9 2
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
            $sourceReflection = new \ReflectionObject($sourceObject);
33
            $destinationReflection = new \ReflectionObject($destination);
34
            $sourceProperties = $sourceReflection->getProperties();
35
            
36
            foreach ($sourceProperties as $sourceProperty) {
37
                $sourceProperty->setAccessible(true);
38
                $name = $sourceProperty->getName();
39
                $value = $sourceProperty->getValue($sourceObject);
40
                
41
                if ($destinationReflection->hasProperty($name)) {
42
                    $propDest = $destinationReflection->getProperty($name);
43
                    $propDest->setAccessible(true);
44
                    $propDest->setValue($destination,$value);
45
                } else {
46
                    $destination->$name = $value;
47
                }
48
            }
49
        }
50
        return $destination;
51
    }
52
    
53
     /**
54
     * Turn the stadClass object to the type of calling Model
55
     *
56
     * @param String $destination
57
     * @param Object $sourceObject
58
     * @return Object $destination
59
     *
60
     * @author RN Kushwaha <[email protected]>
61
     * @since v0.0.5
62
     */
63
    public function turnObjects($destination, $data)
64
    {
65
        $destination = new $destination();
66
        if(count($data)){
67
            $destinationReflection = new \ReflectionObject($destination);
0 ignored issues
show
Unused Code introduced by
The assignment to $destinationReflection is dead and can be removed.
Loading history...
68
            $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

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