|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
|
6
|
|
|
use Doctrine\Common\Util\Debug; |
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
12
|
|
|
*/ |
|
13
|
|
|
class EntityDebugDumper |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param EntityInterface $entity |
|
17
|
|
|
* @param EntityManagerInterface $entityManager |
|
18
|
|
|
* @param int $level |
|
19
|
|
|
* |
|
20
|
|
|
* @return string |
|
21
|
|
|
* @throws \ReflectionException |
|
22
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
23
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
24
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
25
|
|
|
*/ |
|
26
|
1 |
|
public function dump(EntityInterface $entity, ?EntityManagerInterface $entityManager = null, int $level = 0): string |
|
27
|
|
|
{ |
|
28
|
1 |
|
$dump = []; |
|
29
|
1 |
|
$fieldMappings = []; |
|
30
|
1 |
|
if (null !== $entityManager) { |
|
31
|
|
|
$metaData = $entityManager->getClassMetadata(\get_class($entity)); |
|
32
|
|
|
$fieldMappings = $metaData->fieldMappings; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
1 |
|
foreach ($entity::getDoctrineStaticMeta()->getGetters() as $getter) { |
|
35
|
|
|
try { |
|
36
|
1 |
|
$got = $entity->$getter(); |
|
37
|
|
|
} catch (\TypeError $e) { |
|
38
|
|
|
$got = '( *TypeError*: ' . $e->getMessage() . ' )'; |
|
39
|
|
|
} |
|
40
|
1 |
|
$fieldName = \lcfirst(\preg_replace('%^(get|is)%', '', $getter)); |
|
41
|
1 |
|
if (\is_numeric($got) |
|
42
|
1 |
|
|| (isset($fieldMappings[$fieldName]) && 'decimal' === $fieldMappings[$fieldName]['type']) |
|
43
|
|
|
) { |
|
44
|
1 |
|
$dump[$getter] = (float)$got; |
|
45
|
1 |
|
continue; |
|
46
|
|
|
} |
|
47
|
1 |
|
if ($got instanceof \Doctrine\ORM\Proxy\Proxy) { |
|
48
|
|
|
$dump[$getter] = 'Proxy class '; |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
1 |
|
if ($got instanceof EntityInterface) { |
|
52
|
|
|
if ($level === 2) { |
|
53
|
|
|
$dump[$getter] = '(max depth of 2 reached)'; |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
$dump[$getter] = $this->dump($got, $entityManager, ++$level); |
|
57
|
|
|
continue; |
|
58
|
|
|
} |
|
59
|
1 |
|
if ($got instanceof Collection) { |
|
60
|
1 |
|
$dump[$getter] = []; |
|
61
|
1 |
|
foreach ($got as $item) { |
|
62
|
1 |
|
if ($item instanceof EntityInterface) { |
|
63
|
1 |
|
$dump[$getter][] = get_class($item) . ': ' . $item->getId(); |
|
|
|
|
|
|
64
|
1 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
throw new \RuntimeException('Got unexpected object ' . |
|
67
|
|
|
get_class($got) . |
|
68
|
|
|
' in collection from ' . |
|
69
|
|
|
$getter); |
|
70
|
|
|
} |
|
71
|
1 |
|
continue; |
|
72
|
|
|
} |
|
73
|
1 |
|
if (method_exists($got, '__toString')) { |
|
74
|
1 |
|
$dump[$getter] = $got->__toString(); |
|
75
|
1 |
|
continue; |
|
76
|
|
|
} |
|
77
|
1 |
|
$dump[$getter] = Debug::export($got, 2); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
return (string)print_r($dump, true); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|