-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConverterAware.php
48 lines (42 loc) · 1018 Bytes
/
ConverterAware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Qubus\Inheritance
*
* @link https://github.com/QubusPHP/inheritance
* @copyright 2022
* @author Joshua Parker <[email protected]>
* @license https://opensource.org/licenses/mit-license.php MIT License
*
* @since 2.0.1
*/
declare(strict_types=1);
namespace Qubus\Inheritance;
use function get_object_vars;
use function is_array;
trait ConverterAware
{
/**
* Takes an array and turns it into an object.
*
* @param array $array Array of data.
* @return object
*/
public function toObject(array $array): object
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = $this->toObject($value);
}
}
return (object) $array;
}
/**
* Takes an object and turns it into an array.
*
* @param object $object Object data.
*/
public function toArray(object $object): array
{
return get_object_vars($object);
}
}