|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the ONGR package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
|
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 ONGR\ElasticsearchBundle\Mapping; |
|
13
|
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\Cache; |
|
15
|
|
|
use ONGR\ElasticsearchBundle\Result\ObjectIterator; |
|
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
17
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
18
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as SymfonyObjectNormalizer; |
|
19
|
|
|
|
|
20
|
|
|
class ObjectNormalizer extends SymfonyObjectNormalizer |
|
21
|
|
|
{ |
|
22
|
|
|
private $cache; |
|
23
|
|
|
private $converter; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct( |
|
26
|
|
|
Cache $cache, |
|
27
|
|
|
Converter $converter, |
|
28
|
|
|
NameConverterInterface $nameConverter = null, |
|
29
|
|
|
PropertyAccessorInterface $propertyAccessor = null |
|
30
|
|
|
) { |
|
31
|
|
|
parent::__construct(null, $nameConverter, $propertyAccessor, null, null, null, []); |
|
32
|
|
|
|
|
33
|
|
|
$this->cache = $cache; |
|
34
|
|
|
$this->converter = $converter; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$embeddedFields = $this->cache->fetch(DocumentParser::EMBEDDED_CACHED_FIELDS); |
|
40
|
|
|
|
|
41
|
|
|
$class = $embeddedFields[get_class($object)][$attribute] ?? null; |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
|
|
if ($class && is_array($value)) { |
|
45
|
|
|
$value = new ObjectIterator($class, $value, $this->converter, $this->serializer); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->propertyAccessor->setValue($object, $attribute, $value); |
|
49
|
|
|
} catch (NoSuchPropertyException $exception) { |
|
|
|
|
|
|
50
|
|
|
// Properties not found are ignored |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = []) |
|
55
|
|
|
{ |
|
56
|
|
|
$objFields = $this->cache->fetch(DocumentParser::OBJ_CACHED_FIELDS); |
|
57
|
|
|
|
|
58
|
|
|
if (!isset($objFields[is_object($classOrObject) ? get_class($classOrObject) : $classOrObject][$attribute])) { |
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return parent::isAllowedAttribute($classOrObject, $attribute, $format, $context); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
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.