|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Testing; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Util\Debug; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
|
8
|
|
|
|
|
9
|
|
|
class EntityDebugDumper |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param EntityInterface $entity |
|
13
|
|
|
* @param EntityManagerInterface $entityManager |
|
14
|
|
|
* @param int $level |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
18
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
|
19
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function dump(EntityInterface $entity, ?EntityManagerInterface $entityManager = null, int $level = 0): string |
|
22
|
|
|
{ |
|
23
|
1 |
|
$dump = []; |
|
24
|
1 |
|
$fieldMappings = []; |
|
25
|
1 |
|
if (null !== $entityManager) { |
|
26
|
|
|
$metaData = $entityManager->getClassMetadata(\get_class($entity)); |
|
27
|
|
|
$fieldMappings = $metaData->fieldMappings; |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
1 |
|
foreach ($entity->getGetters() as $getter) { |
|
30
|
1 |
|
$got = $entity->$getter(); |
|
31
|
1 |
|
$fieldName = \lcfirst(\preg_replace('%^(get|is)%', '', $getter)); |
|
32
|
1 |
|
if (\is_numeric($got) |
|
33
|
1 |
|
|| (isset($fieldMappings[$fieldName]) && 'decimal' === $fieldMappings[$fieldName]['type']) |
|
34
|
|
|
) { |
|
35
|
1 |
|
$dump[$getter] = (float)$got; |
|
36
|
1 |
|
continue; |
|
37
|
|
|
} |
|
38
|
1 |
|
if ($got instanceof \Doctrine\ORM\Proxy\Proxy) { |
|
39
|
|
|
$dump[$getter] = 'Proxy class '; |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
1 |
|
if (method_exists($got, '__toString')) { |
|
43
|
|
|
$dump[$getter] = $got->__toString(); |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
1 |
|
if (\is_object($got) && $got instanceof EntityInterface) { |
|
47
|
|
|
if ($level === 2) { |
|
48
|
|
|
$dump[$getter] = '(max depth of 2 reached)'; |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
$dump[$getter] = $this->dump($got, $entityManager, ++$level); |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
1 |
|
$dump[$getter] = Debug::export($got, 2); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return (string)print_r($dump, true); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|