wowstack /
php-dbc-parser
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Wowstack\Dbc; |
||
| 6 | |||
| 7 | use Symfony\Component\Yaml\Yaml; |
||
| 8 | use Wowstack\Dbc\MappingField as Mappings; |
||
| 9 | use Wowstack\Dbc\MappingField\MappingFieldInterface; |
||
| 10 | use Wowstack\Dbc\MappingField\MappingException; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Implements the column to field type maps for reading DBC files. |
||
| 14 | */ |
||
| 15 | class Mapping |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var MappingFieldInterface[] |
||
| 19 | */ |
||
| 20 | protected $fields = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | protected $fieldCount = 0; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | protected $fieldSize = 0; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool |
||
| 34 | */ |
||
| 35 | protected $hasStrings = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Create an instance. |
||
| 39 | * |
||
| 40 | * @param array $mapping |
||
| 41 | * |
||
| 42 | * @throws MappingException |
||
| 43 | */ |
||
| 44 | 23 | public function __construct(array $mapping = []) |
|
| 45 | { |
||
| 46 | 23 | $fields = isset($mapping['fields']) ? $mapping['fields'] : []; |
|
| 47 | |||
| 48 | 23 | foreach ($fields as $fieldName => $fieldData) { |
|
| 49 | 23 | $this->add($fieldName, $fieldData); |
|
| 50 | 23 | $this->fieldCount += $this->fields[$fieldName]->getTotalCount(); |
|
| 51 | 23 | $this->fieldSize += $this->fields[$fieldName]->getTotalSize(); |
|
| 52 | |||
| 53 | 23 | if ('string' === $this->fields[$fieldName]->getType() || |
|
| 54 | 23 | 'localized_string' === $this->fields[$fieldName]->getType()) { |
|
| 55 | 23 | $this->hasStrings = true; |
|
| 56 | } |
||
| 57 | } |
||
| 58 | 21 | } |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Adds a field type to the mapping list. |
||
| 62 | * |
||
| 63 | * @param string $name |
||
| 64 | * @param array $parameters |
||
| 65 | * |
||
| 66 | * @throws MappingException |
||
| 67 | */ |
||
| 68 | 23 | public function add(string $name, array $parameters) |
|
| 69 | { |
||
| 70 | 23 | if (!isset($parameters['type'])) { |
|
| 71 | 1 | throw new MappingException('Field definition is missing a type.'); |
|
| 72 | } |
||
| 73 | |||
| 74 | 23 | switch ($parameters['type']) { |
|
| 75 | 23 | case 'float': |
|
| 76 | 16 | $field = new Mappings\FloatField($name, $parameters); |
|
| 77 | 16 | break; |
|
| 78 | 23 | case 'localized_string': |
|
| 79 | 16 | $field = new Mappings\LocalizedStringField($name, $parameters); |
|
| 80 | 16 | break; |
|
| 81 | 23 | case 'char': |
|
| 82 | 1 | $field = new Mappings\SignedCharField($name, $parameters); |
|
| 83 | 1 | break; |
|
| 84 | 23 | case 'int': |
|
| 85 | 16 | $field = new Mappings\SignedIntegerField($name, $parameters); |
|
| 86 | 16 | break; |
|
| 87 | 23 | case 'string': |
|
| 88 | 5 | $field = new Mappings\StringField($name, $parameters); |
|
| 89 | 5 | break; |
|
| 90 | 23 | case 'uchar': |
|
| 91 | 1 | $field = new Mappings\UnsignedCharField($name, $parameters); |
|
| 92 | 1 | break; |
|
| 93 | 23 | case 'uint': |
|
| 94 | 23 | $field = new Mappings\UnsignedIntegerField($name, $parameters); |
|
| 95 | 23 | break; |
|
| 96 | 10 | case 'foreign_key': |
|
| 97 | 9 | $field = new Mappings\ForeignKeyField($name, $parameters); |
|
| 98 | 9 | break; |
|
| 99 | default: |
||
| 100 | 1 | throw new MappingException('Unknown field type specified'); |
|
| 101 | } |
||
| 102 | |||
| 103 | 23 | $this->fields[$name] = $field; |
|
| 104 | 23 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the amount of fields in the mapping. |
||
| 108 | * |
||
| 109 | * @return int |
||
| 110 | */ |
||
| 111 | 16 | public function getFieldCount(): int |
|
| 112 | { |
||
| 113 | 16 | return $this->fieldCount; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the actual amount of columns in the mapping. |
||
| 118 | * |
||
| 119 | * @return int |
||
| 120 | */ |
||
| 121 | 5 | public function getFieldSize(): int |
|
| 122 | { |
||
| 123 | 5 | return $this->fieldSize; |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns the mapping type for a field. |
||
| 128 | * |
||
| 129 | * @param string $name |
||
| 130 | * |
||
| 131 | * @return string |
||
| 132 | */ |
||
| 133 | 1 | public function getFieldType(string $name): string |
|
| 134 | { |
||
| 135 | 1 | return $this->fields[$name]->getType(); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Create an instance with a mapping from file. |
||
| 140 | * |
||
| 141 | * @param string $yaml path to YAML file |
||
| 142 | * |
||
| 143 | * @return Mapping |
||
| 144 | * |
||
| 145 | * @throws MappingException |
||
| 146 | */ |
||
| 147 | 23 | public static function fromYAML(string $yaml): Mapping |
|
| 148 | { |
||
| 149 | 23 | return new self(Yaml::parseFile($yaml)); |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns if a string field is part of the mapping. |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 11 | public function hasStrings(): bool |
|
| 158 | { |
||
| 159 | 11 | return $this->hasStrings; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns a list of field names. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 2 | public function getFieldNames(): array |
|
| 168 | { |
||
| 169 | 2 | $fieldNames = []; |
|
| 170 | |||
| 171 | 2 | foreach ($this->fields as $field) { |
|
| 172 | 2 | $fieldList = $field->getParsedFields(); |
|
| 173 | 2 | foreach ($fieldList as $fieldName => $fieldData) { |
|
| 174 | 2 | $fieldNames[] = $fieldName; |
|
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | 2 | return $fieldNames; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the resulting parsed field data. |
||
| 183 | * |
||
| 184 | * @var array |
||
| 185 | * |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | 5 | public function getParsedFields(): array |
|
| 189 | { |
||
| 190 | 5 | $parsedFields = []; |
|
| 191 | |||
| 192 | 5 | foreach ($this->fields as $field) { |
|
| 193 | 5 | $fieldList = $field->getParsedFields(); |
|
| 194 | 5 | foreach ($fieldList as $fieldName => $fieldData) { |
|
| 195 | 5 | $parsedFields[$fieldName] = $fieldData; |
|
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | 5 | return $parsedFields; |
|
| 200 | } |
||
| 201 | } |
||
| 202 |