1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\EventListener; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Metadata\PropertyMetadata; |
15
|
|
|
use Cubiche\Infrastructure\Doctrine\ODM\MongoDB\Event\RegisterDriverMetadataEventArgs; |
16
|
|
|
use Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\Metadata\Driver\XmlDriver; |
17
|
|
|
use Cubiche\Infrastructure\Model\Doctrine\ODM\MongoDB\Types\DynamicNativeValueObjectType; |
18
|
|
|
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs; |
19
|
|
|
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; |
20
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Event Listener Class. |
24
|
|
|
* |
25
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class EventListener |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @param RegisterDriverMetadataEventArgs $eventArgs |
32
|
|
|
*/ |
33
|
|
|
public function registerDriverMetadata(RegisterDriverMetadataEventArgs $eventArgs) |
34
|
|
|
{ |
35
|
|
|
$eventArgs->driverFactory()->registerXmlDriver(XmlDriver::class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param LoadClassMetadataEventArgs $eventArgs |
40
|
|
|
*/ |
41
|
|
|
public function postLoadClassMetadata(LoadClassMetadataEventArgs $eventArgs) |
42
|
|
|
{ |
43
|
|
|
$this->checkValueObjectType($eventArgs->getClassMetadata()); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ClassMetadata $classMetadata |
48
|
|
|
*/ |
49
|
|
|
protected function checkValueObjectType(ClassMetadata $classMetadata) |
50
|
|
|
{ |
51
|
|
|
foreach ($classMetadata->fieldMappings as $fieldName => $mapping) { |
52
|
|
|
if (isset($mapping['cubiche:valueobject'])) { |
53
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
54
|
|
|
$propertyMetadata = $mapping['cubiche:valueobject']; |
55
|
|
|
|
56
|
|
|
$type = str_replace('\\', '.', $propertyMetadata->getMetadata('type')); |
57
|
|
|
if (!Type::hasType($type)) { |
58
|
|
|
Type::registerType($type, DynamicNativeValueObjectType::class); |
59
|
|
|
Type::getType($type)->setTargetClass($propertyMetadata->getMetadata('type')); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$classMetadata->fieldMappings[$fieldName]['type'] = $type; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.