mdiyakov /
DoctrineSolrBundle
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Mdiyakov\DoctrineSolrBundle\Schema; |
||||
| 4 | |||||
| 5 | use Mdiyakov\DoctrineSolrBundle\Exception\SchemaConfigException; |
||||
| 6 | use Mdiyakov\DoctrineSolrBundle\Schema\Field\ConfigEntityField; |
||||
| 7 | use Mdiyakov\DoctrineSolrBundle\Schema\Field\DocumentUniqueField; |
||||
| 8 | use Mdiyakov\DoctrineSolrBundle\Schema\Field\Entity\Field; |
||||
| 9 | use Mdiyakov\DoctrineSolrBundle\Schema\Field\Entity\FieldFactory; |
||||
| 10 | |||||
| 11 | class Schema |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * @var string |
||||
| 15 | */ |
||||
| 16 | private $name; |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * @var |
||||
| 20 | */ |
||||
| 21 | private $configEntityFields; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * @var Field |
||||
| 25 | */ |
||||
| 26 | private $entityPrimaryKeyField; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * @var ConfigEntityField |
||||
| 30 | */ |
||||
| 31 | private $discriminatorConfigField; |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * @var DocumentUniqueField |
||||
| 35 | */ |
||||
| 36 | private $documentUniqueField; |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * @var Field[] |
||||
| 40 | */ |
||||
| 41 | private $fields = []; |
||||
| 42 | |||||
| 43 | /** |
||||
| 44 | * @var string |
||||
| 45 | */ |
||||
| 46 | private $client; |
||||
| 47 | |||||
| 48 | /** |
||||
| 49 | * @var Field[] |
||||
| 50 | */ |
||||
| 51 | private $suggesterFieldMap = []; |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * @param string $name |
||||
| 55 | * @param string $client |
||||
| 56 | * @param string[] $documentUniqueFieldConfig |
||||
| 57 | * @param string[][] $fieldsConfig |
||||
| 58 | * @param string[][] $configEntityFields |
||||
| 59 | * @throws \Exception |
||||
| 60 | */ |
||||
| 61 | public function __construct($name, $client, $documentUniqueFieldConfig, $fieldsConfig, $configEntityFields) |
||||
| 62 | { |
||||
| 63 | $this->name = $name; |
||||
| 64 | $this->client = $client; |
||||
| 65 | $this->documentUniqueField = new DocumentUniqueField($documentUniqueFieldConfig, $this); |
||||
| 66 | $fieldFactory = new FieldFactory(); |
||||
| 67 | |||||
| 68 | foreach ($fieldsConfig as $fieldConfig) { |
||||
| 69 | $field = $fieldFactory->buildField($fieldConfig); |
||||
| 70 | $this->fields[$field->getEntityFieldName()] = $field; |
||||
| 71 | if ($field->getSuggester()) { |
||||
| 72 | $this->suggesterFieldMap[$field->getSuggester()] = $field; |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | if ($field->isPrimaryKey()) { |
||||
| 76 | if ($this->getEntityPrimaryKeyField()) { |
||||
| 77 | throw new SchemaConfigException('You have already defined one field as "primary key". It can be only one primary field in schema'); |
||||
| 78 | } |
||||
| 79 | $this->entityPrimaryKeyField = $field; |
||||
| 80 | } |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | if (!$this->getEntityPrimaryKeyField()) { |
||||
| 84 | throw new SchemaConfigException('You have to define one field as "entity_primary_key" with true value'); |
||||
| 85 | } |
||||
| 86 | |||||
| 87 | foreach ($configEntityFields as $configField) { |
||||
| 88 | $configField = new ConfigEntityField( |
||||
| 89 | $configField['config_field_name'], |
||||
| 90 | $configField['document_field_name'], |
||||
| 91 | $configField['discriminator'], |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 92 | $configField['priority'] |
||||
|
0 ignored issues
–
show
$configField['priority'] of type string is incompatible with the type integer expected by parameter $priority of Mdiyakov\DoctrineSolrBun...ityField::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 93 | ); |
||||
| 94 | $this->configEntityFields[$configField->getConfigFieldName()] = $configField; |
||||
| 95 | |||||
| 96 | if ($configField->isDiscriminator()) { |
||||
| 97 | if ($this->getDiscriminatorConfigField()) { |
||||
| 98 | throw new SchemaConfigException('You have already defined one config field as "discriminator". It can be only one discriminator config field in schema'); |
||||
| 99 | } |
||||
| 100 | $this->discriminatorConfigField = $configField; |
||||
| 101 | } |
||||
| 102 | } |
||||
| 103 | |||||
| 104 | if (!$this->getDiscriminatorConfigField()) { |
||||
| 105 | throw new SchemaConfigException( |
||||
| 106 | 'You have to define one config field in schema with flag "discriminator" having true value' |
||||
| 107 | ); |
||||
| 108 | } |
||||
| 109 | } |
||||
| 110 | |||||
| 111 | /** |
||||
| 112 | * return Field[] |
||||
| 113 | */ |
||||
| 114 | public function getFields() |
||||
| 115 | { |
||||
| 116 | return $this->fields; |
||||
| 117 | } |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @return string |
||||
| 121 | */ |
||||
| 122 | public function getName() |
||||
| 123 | { |
||||
| 124 | return $this->name; |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * @return ConfigEntityField[] |
||||
| 129 | */ |
||||
| 130 | public function getConfigEntityFields() |
||||
| 131 | { |
||||
| 132 | return $this->configEntityFields; |
||||
| 133 | } |
||||
| 134 | |||||
| 135 | /** |
||||
| 136 | * @return Field |
||||
| 137 | */ |
||||
| 138 | public function getEntityPrimaryKeyField() |
||||
| 139 | { |
||||
| 140 | return $this->entityPrimaryKeyField; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * @return ConfigEntityField |
||||
| 145 | */ |
||||
| 146 | public function getDiscriminatorConfigField() |
||||
| 147 | { |
||||
| 148 | return $this->discriminatorConfigField; |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | /** |
||||
| 152 | * @param string $entityFieldName |
||||
| 153 | * @return Field |
||||
| 154 | * @throws SchemaConfigException |
||||
| 155 | */ |
||||
| 156 | public function getFieldByEntityFieldName($entityFieldName) |
||||
| 157 | { |
||||
| 158 | if (!array_key_exists($entityFieldName, $this->fields)) { |
||||
| 159 | throw new SchemaConfigException( |
||||
| 160 | sprintf('Schema %s does not contain "%s" entity_field_name', $this->getName(), $entityFieldName) |
||||
| 161 | ); |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | return $this->fields[$entityFieldName]; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * @return DocumentUniqueField |
||||
| 169 | */ |
||||
| 170 | public function getDocumentUniqueField() |
||||
| 171 | { |
||||
| 172 | return $this->documentUniqueField; |
||||
| 173 | } |
||||
| 174 | |||||
| 175 | /** |
||||
| 176 | * @return string|null |
||||
| 177 | */ |
||||
| 178 | public function getClient() |
||||
| 179 | { |
||||
| 180 | return $this->client; |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | /** |
||||
| 184 | * @param $configFieldName |
||||
| 185 | * @return mixed |
||||
| 186 | */ |
||||
| 187 | public function getConfigFieldByName($configFieldName) |
||||
| 188 | { |
||||
| 189 | if (!array_key_exists($configFieldName, $this->configEntityFields)) { |
||||
| 190 | throw new SchemaConfigException( |
||||
| 191 | sprintf('Schema %s does not contain "%s" config field', $this->getName(), $configFieldName) |
||||
| 192 | ); |
||||
| 193 | } |
||||
| 194 | |||||
| 195 | return $this->configEntityFields[$configFieldName]; |
||||
| 196 | } |
||||
| 197 | |||||
| 198 | /** |
||||
| 199 | * @param string $suggester |
||||
| 200 | * @return Field |
||||
| 201 | */ |
||||
| 202 | public function getFieldBySuggester($suggester) |
||||
| 203 | { |
||||
| 204 | if (!array_key_exists($suggester, $this->suggesterFieldMap)) { |
||||
| 205 | throw new SchemaConfigException( |
||||
| 206 | sprintf('Schema %s does not support suggestion "%s"', $this->getName(), $suggester) |
||||
| 207 | ); |
||||
| 208 | } |
||||
| 209 | |||||
| 210 | return $this->suggesterFieldMap[$suggester]; |
||||
| 211 | } |
||||
| 212 | } |