1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace As3\Modlr\Persister\MongoDb; |
4
|
|
|
|
5
|
|
|
use As3\Modlr\Exception\MetadataException; |
6
|
|
|
use As3\Modlr\Metadata\EntityMetadata; |
7
|
|
|
use As3\Modlr\Metadata\Interfaces\StorageLayerInterface; |
8
|
|
|
use As3\Modlr\Metadata\Interfaces\StorageMetadataFactoryInterface; |
9
|
|
|
use As3\Modlr\Util\EntityUtility; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Creates MongoDb storage Metadata instances for use with metadata drivers. |
13
|
|
|
* Is also responsible for validating storage objects. |
14
|
|
|
* |
15
|
|
|
* @author Jacob Bare <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
final class StorageMetadataFactory implements StorageMetadataFactoryInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityUtility |
21
|
|
|
*/ |
22
|
|
|
private $entityUtil; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param EntityUtility $entityUtl |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public function __construct(EntityUtility $entityUtil) |
30
|
|
|
{ |
31
|
|
|
$this->entityUtil = $entityUtil; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function createInstance(array $mapping) |
38
|
|
|
{ |
39
|
|
|
$persistence = new StorageMetadata(); |
40
|
|
|
|
41
|
|
|
if (isset($mapping['db'])) { |
42
|
|
|
$persistence->db = $mapping['db']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (isset($mapping['collection'])) { |
46
|
|
|
$persistence->collection = $mapping['collection']; |
47
|
|
|
} |
48
|
|
|
return $persistence; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
|
|
public function handleLoad(EntityMetadata $metadata) |
55
|
|
|
{ |
56
|
|
|
if (null === $metadata->persistence->collection) { |
|
|
|
|
57
|
|
|
$metadata->persistence->collection = $metadata->type; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function handleValidate(EntityMetadata $metadata) |
65
|
|
|
{ |
66
|
|
|
$this->validateIdStrategy($metadata); |
67
|
|
|
$this->validateDatabase($metadata); |
68
|
|
|
$this->validateCollectionNaming($metadata); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Validates that the collection naming is correct, based on entity format config. |
75
|
|
|
* |
76
|
|
|
* @param EntityMetadata $metadata |
77
|
|
|
* @throws MetadataException |
78
|
|
|
*/ |
79
|
|
|
private function validateCollectionNaming(EntityMetadata $metadata) |
80
|
|
|
{ |
81
|
|
|
$persistence = $metadata->persistence; |
82
|
|
|
if (false === $this->entityUtil->isEntityTypeValid($persistence->collection)) { |
|
|
|
|
83
|
|
|
throw MetadataException::invalidMetadata( |
84
|
|
|
$metadata->type, |
85
|
|
|
sprintf('The entity persistence collection "%s" is invalid based on the configured name format "%s"', |
86
|
|
|
$persistence->collection, |
|
|
|
|
87
|
|
|
$this->entityUtil->getRestConfig()->getEntityFormat() |
88
|
|
|
) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Validates that the proper database properties are set. |
95
|
|
|
* |
96
|
|
|
* @param EntityMetadata $metadata |
97
|
|
|
* @throws MetadataException |
98
|
|
|
*/ |
99
|
|
|
private function validateDatabase(EntityMetadata $metadata) |
100
|
|
|
{ |
101
|
|
|
$persistence = $metadata->persistence; |
102
|
|
|
if (false === $metadata->isChildEntity() && (empty($persistence->db) || empty($persistence->collection))) { |
|
|
|
|
103
|
|
|
throw MetadataException::invalidMetadata($metadata->type, 'The persistence database and collection names cannot be empty.'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Validates the proper id strategy. |
109
|
|
|
* |
110
|
|
|
* @param EntityMetadata $metadata |
111
|
|
|
* @throws MetadataException |
112
|
|
|
*/ |
113
|
|
|
private function validateIdStrategy(EntityMetadata $metadata) |
114
|
|
|
{ |
115
|
|
|
$persistence = $metadata->persistence; |
116
|
|
|
$validIdStrategies = ['object']; |
117
|
|
|
if (!in_array($persistence->idStrategy, $validIdStrategies)) { |
|
|
|
|
118
|
|
|
throw MetadataException::invalidMetadata($metadata->type, sprintf('The persistence id strategy "%s" is invalid. Valid types are "%s"', $persistence->idStrategy, implode('", "', $validIdStrategies))); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.