This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Objects.php
128 lines (111 loc) · 2.76 KB
/
Objects.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php declare (strict_types = 1);
namespace Wavevision\Utils;
use Nette\SmartObject;
use function get_class;
use function implode;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
use function method_exists;
use function ucfirst;
class Objects
{
use SmartObject;
/**
* @return mixed
*/
public static function get(object $object, string $property)
{
return $object->{self::name('get', $property)}();
}
/**
* @return mixed
*/
public static function getNested(object $object, string ...$properties)
{
foreach ($properties as $property) {
$object = self::get($object, $property);
if (!is_object($object)) {
return $object;
}
}
return $object;
}
public static function getClassName(object $object): string
{
return Strings::getClassName(get_class($object));
}
/**
* @return mixed|null
*/
public static function getIfNotNull(?object $object, string $property)
{
return self::ifNotNull(
$object,
function (object $object) use ($property) {
return self::get($object, $property);
}
);
}
public static function getNamespace(object $object): string
{
return Strings::getNamespace(get_class($object));
}
public static function hasGetter(object $object, string $property): bool
{
return method_exists($object, self::name('get', $property));
}
public static function hasSetter(object $object, string $property): bool
{
return method_exists($object, self::name('set', $property));
}
/**
* @return mixed|null
*/
public static function ifNotNull(?object $object, callable $callable)
{
return $object ? $callable($object) : null;
}
/**
* @param mixed $value
* @return mixed
*/
public static function set(object $object, string $property, $value)
{
return $object->{self::name('set', $property)}($value);
}
/**
* @param array<mixed> $keys
* @param array<mixed> $extra
* @return array<mixed>
*/
public static function toArray(object $object, array $keys, array $extra = []): array
{
$values = [];
foreach ($keys as $key => $name) {
if (is_array($name)) {
$values[is_string($key) ? $key : implode('.', $name)] = self::getNested($object, ...$name);
} else {
$values[$name] = self::get($object, $name);
}
}
foreach ($extra as $key => $value) {
$values[$key] = is_callable($value) ? $value(self::get($object, $key)) : $value;
}
return $values;
}
/**
* @param array<mixed> $attributes
*/
public static function copyAttributes(object $source, object $destination, array $attributes): void
{
foreach ($attributes as $attribute) {
Objects::set($destination, $attribute, Objects::get($source, $attribute));
}
}
private static function name(string $prefix, string $name): string
{
return $prefix . ucfirst($name);
}
}