1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Transformer; |
4
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\ClassUtils; |
6
|
|
|
use Nimble\ElasticBundle\Document; |
7
|
|
|
use Nimble\ElasticBundle\Transformer\Exception\TransformerAlreadyRegisteredException; |
8
|
|
|
use Nimble\ElasticBundle\Transformer\Exception\TransformerNotFoundException; |
9
|
|
|
|
10
|
|
|
class TransformerManager |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $transfomers = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param TransformerInterface $transformer |
19
|
|
|
* @param string $indexId |
20
|
|
|
* @param string $typeName |
21
|
|
|
*/ |
22
|
|
|
public function registerTransformer(TransformerInterface $transformer, $indexId, $typeName) |
23
|
|
|
{ |
24
|
|
|
$className = $transformer->getClass(); |
25
|
|
|
|
26
|
|
|
if (isset($this->transfomers[$className][$indexId][$typeName])) { |
27
|
|
|
throw new TransformerAlreadyRegisteredException($className, $indexId, $typeName); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$this->transfomers[$className][$indexId][$typeName] = $transformer; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param object $entity |
35
|
|
|
* @param string $indexId |
36
|
|
|
* @param string $typeName |
37
|
|
|
* @return TransformerInterface |
38
|
|
|
*/ |
39
|
|
|
protected function getTransformer($entity, $indexId, $typeName) |
40
|
|
|
{ |
41
|
|
|
$className = get_class($entity); |
42
|
|
|
|
43
|
|
|
$classKey = ClassUtils::findClassKey($className, $this->transfomers); |
44
|
|
|
|
45
|
|
|
if (!$classKey) { |
|
|
|
|
46
|
|
|
throw new TransformerNotFoundException($className, $indexId, $typeName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->transfomers[$classKey][$indexId][$typeName]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param object $entity |
54
|
|
|
* @param string $indexId |
55
|
|
|
* @param string $typeName |
56
|
|
|
* @return Document[] |
57
|
|
|
*/ |
58
|
|
|
public function transformToDocuments($entity, $indexId, $typeName) |
59
|
|
|
{ |
60
|
|
|
return $this->getTransformer($entity, $indexId, $typeName)->transformToDocuments($entity); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param object $entity |
65
|
|
|
* @param string $indexId |
66
|
|
|
* @param string $typeName |
67
|
|
|
* @return string[]|int[] |
68
|
|
|
*/ |
69
|
|
|
public function transformToIds($entity, $indexId, $typeName) |
70
|
|
|
{ |
71
|
|
|
return $this->getTransformer($entity, $indexId, $typeName)->transformToIds($entity); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: