1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\Factory; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Bridge\Elasticsearch\Exception\IndexNotFoundException; |
17
|
|
|
use ApiPlatform\Core\Bridge\Elasticsearch\Metadata\Document\DocumentMetadata; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Creates document's metadata using the mapping configuration. |
21
|
|
|
* |
22
|
|
|
* @experimental |
23
|
|
|
* |
24
|
|
|
* @author Baptiste Meyer <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
final class ConfiguredDocumentMetadataFactory implements DocumentMetadataFactoryInterface |
27
|
|
|
{ |
28
|
|
|
private $mapping; |
29
|
|
|
private $decorated; |
30
|
|
|
|
31
|
|
|
public function __construct(array $mapping, ?DocumentMetadataFactoryInterface $decorated = null) |
32
|
|
|
{ |
33
|
|
|
$this->mapping = $mapping; |
34
|
|
|
$this->decorated = $decorated; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function create(string $resourceClass): DocumentMetadata |
41
|
|
|
{ |
42
|
|
|
$documentMetadata = null; |
43
|
|
|
|
44
|
|
|
if ($this->decorated) { |
45
|
|
|
try { |
46
|
|
|
$documentMetadata = $this->decorated->create($resourceClass); |
47
|
|
|
} catch (IndexNotFoundException $e) { |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (null === $index = $this->mapping[$resourceClass] ?? null) { |
52
|
|
|
if ($documentMetadata) { |
53
|
|
|
return $documentMetadata; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
throw new IndexNotFoundException(sprintf('No index associated with the "%s" resource class.', $resourceClass)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$documentMetadata = $documentMetadata ?? new DocumentMetadata(); |
60
|
|
|
|
61
|
|
|
if (isset($index['index'])) { |
62
|
|
|
$documentMetadata = $documentMetadata->withIndex($index['index']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($index['type'])) { |
66
|
|
|
$documentMetadata = $documentMetadata->withType($index['type']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $documentMetadata; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|