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)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $destination; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|