|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: peter |
|
5
|
|
|
* Date: 22.10.18 |
|
6
|
|
|
* Time: 21:28 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Maslosoft\Mangan\Helpers; |
|
10
|
|
|
|
|
11
|
|
|
use Maslosoft\Mangan\Events\Event; |
|
12
|
|
|
use Maslosoft\Mangan\Events\UnknownDocumentType; |
|
13
|
|
|
use Maslosoft\Mangan\Exceptions\TransformatorException; |
|
14
|
|
|
use function array_key_exists; |
|
15
|
|
|
use function get_class; |
|
16
|
|
|
use function gettype; |
|
17
|
|
|
use function is_object; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This class will try to do something on unknown documents |
|
22
|
|
|
* or will panic. |
|
23
|
|
|
* |
|
24
|
|
|
* @package Maslosoft\Mangan\Helpers |
|
25
|
|
|
*/ |
|
26
|
|
|
class UnknownDocumentTypePanicker |
|
27
|
|
|
{ |
|
28
|
|
|
public static function tryHandle(&$data, $parent, $parentField) |
|
29
|
|
|
{ |
|
30
|
|
|
$className = ''; |
|
31
|
|
|
$handled = false; |
|
32
|
|
|
if(!empty($parent)) |
|
33
|
|
|
{ |
|
34
|
|
|
$event = new UnknownDocumentType(); |
|
35
|
|
|
$event->setData($data); |
|
36
|
|
|
$event->parent = $parent; |
|
37
|
|
|
$event->field = $parentField; |
|
38
|
|
|
|
|
39
|
|
|
$handled = Event::handled($parent, UnknownDocumentType::EventName, $event); |
|
40
|
|
|
if($handled) |
|
41
|
|
|
{ |
|
42
|
|
|
$data = $event->getData(); |
|
43
|
|
|
if(empty($data['_class'])) |
|
44
|
|
|
{ |
|
45
|
|
|
$handled = false; |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
|
|
$className = $data['_class']; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
if(!$handled) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$params = []; |
|
56
|
|
|
if(!empty($parent) && is_object($parent)) |
|
57
|
|
|
{ |
|
58
|
|
|
$params[] = sprintf('on model `%s`', get_class($parent)); |
|
59
|
|
|
} |
|
60
|
|
|
if(!empty($parentField)) |
|
61
|
|
|
{ |
|
62
|
|
|
$params[] = sprintf('on field `%s`', $parentField); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if(array_key_exists('data', $data)) |
|
66
|
|
|
{ |
|
67
|
|
|
$params[] = sprintf('got type `%s`', gettype($data['data'])); |
|
68
|
|
|
|
|
69
|
|
|
if (is_object($data['data'])) |
|
70
|
|
|
{ |
|
71
|
|
|
$params[] = sprintf('instance of `%s`', get_class($data['data'])); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
throw new TransformatorException('Could not determine document type ' . implode(', ', $params)); |
|
75
|
|
|
} |
|
76
|
|
|
return $className; |
|
77
|
|
|
} |
|
78
|
|
|
} |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.