Skip to content

Commit 0342e1f

Browse files
authored
Fix/array basic types (#14)
* fix type replacement * fix basic array types * allow infection plugins * fix composer.json * exclude console
1 parent 5654988 commit 0342e1f

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"converter"
2323
],
2424
"config": {
25-
"sort-packages": true
25+
"sort-packages": true,
26+
"allow-plugins": {
27+
"infection/extension-installer": true
28+
}
2629
},
2730
"require": {
2831
"php": "^7.4|^8.0",

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
parameters:
22
level: 9
33
paths: [ src ]
4+
excludePaths:
5+
- src/console.php
46
ignoreErrors:
57
- "#Cannot access offset '[a-z]+' on mixed.#"

src/Converter/AvscToJson.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,18 @@ private function convertAvro(array $avscArray): array
4747
/** @var string|mixed[] $items */
4848
$items = $avscArray['items'];
4949

50-
if (
51-
true === $this->isBasicType($items)
52-
|| (true === is_array($items) && true === $this->isBasicTypeArray($items))
53-
) {
54-
$jsonArray['items'] = $items;
50+
if (true === $this->isBasicType($items)) {
51+
$jsonArray['items'] = ['type' => $items];
52+
} elseif (true === is_array($items) && true === $this->isBasicTypeArray($items)) {
53+
$jsonArray['items'] = $this->getXOf($items, 'oneOf');
5554
} elseif (
5655
true === is_array($items)
5756
&& true === isset($items['type'])
5857
&& 'record' === $items['type']
5958
) {
6059
$jsonArray['items'] = $this->convertAvro($items);
6160
} elseif (true === is_array($items)) {
62-
$jsonArray['items'] = $this->getAnyOf($items);
61+
$jsonArray['items'] = $this->getXOf($items);
6362
}
6463
}
6564
if ('name' === $key && true === is_string($value)) {
@@ -99,7 +98,7 @@ private function convertAvroFieldsToJsonFields(array $avroFields): array
9998
'description' => $field['doc']
10099
];
101100
} elseif (true === is_array($fieldType)) {
102-
$fields[$field['name']] = $this->getAnyOf($fieldType);
101+
$fields[$field['name']] = $this->getXOf($fieldType);
103102
}
104103
}
105104

@@ -133,18 +132,18 @@ private function getRequiredFields(array $avroFields): array
133132
* @param mixed[] $types
134133
* @return mixed[]
135134
*/
136-
private function getAnyOf(array $types)
135+
private function getXOf(array $types, string $ofType = 'anyOf')
137136
{
138-
$anyOf = [];
137+
$xOf = [];
139138

140139
foreach ($types as $type) {
141140
if (false === is_string($type) && false === is_array($type)) {
142141
continue;
143142
}
144-
$anyOf['anyOf'][] = $this->getAnyOfType($type);
143+
$xOf[$ofType][] = $this->getAnyOfType($type);
145144
}
146145

147-
return $anyOf;
146+
return $xOf;
148147
}
149148

150149
/**

0 commit comments

Comments
 (0)