|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Minotaur |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2017 Appertly |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Minotaur; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Helps with accessing fields on unknown values. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Getter |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Gets the MongoDB-style ID from an object. |
|
30
|
|
|
* |
|
31
|
|
|
* @param $object - The object |
|
32
|
|
|
* @return mixed The ID found or `null` |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public static function getId($object) |
|
35
|
|
|
{ |
|
36
|
1 |
|
if (is_array($object) || $object instanceof \ArrayAccess) { |
|
37
|
1 |
|
return $object['_id'] ?? null; |
|
38
|
1 |
|
} elseif (is_object($object)) { |
|
39
|
1 |
|
$values = get_object_vars($object); |
|
40
|
1 |
|
if ($values === null) { |
|
41
|
|
|
throw new \UnexpectedValueException('The values array is null'); |
|
42
|
|
|
} |
|
43
|
1 |
|
if (array_key_exists('_id', $values)) { |
|
44
|
1 |
|
return $values['_id']; |
|
45
|
1 |
|
} elseif (array_key_exists('id', $values)) { |
|
46
|
1 |
|
return $values['id']; |
|
47
|
1 |
|
} elseif (method_exists($object, 'getId') || method_exists($object, '__call')) { |
|
48
|
1 |
|
return $object->getId(); |
|
49
|
|
|
} elseif (method_exists($object, '__get')) { |
|
50
|
|
|
return $object->id; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Extracts any field from an object. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $object The object |
|
60
|
|
|
* @return mixed The value found or `null` |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public static function get($object, string $field) |
|
63
|
|
|
{ |
|
64
|
1 |
|
if (is_array($object) || $object instanceof \ArrayAccess) { |
|
65
|
1 |
|
return $object[$field] ?? null; |
|
66
|
1 |
|
} elseif (is_object($object)) { |
|
67
|
1 |
|
$values = get_object_vars($object); |
|
68
|
1 |
|
if ($values === null) { |
|
69
|
|
|
throw new \UnexpectedValueException('The values array is null'); |
|
70
|
|
|
} |
|
71
|
1 |
|
if (array_key_exists($field, $values)) { |
|
72
|
1 |
|
return $values[$field]; |
|
73
|
1 |
|
} elseif (method_exists($object, 'get' . ucfirst($field)) || method_exists($object, '__call')) { |
|
74
|
1 |
|
return call_user_func([$object, 'get' . ucfirst($field)]); |
|
75
|
1 |
|
} elseif (method_exists($object, '__get')) { |
|
76
|
|
|
return $object->id; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
1 |
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|