Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
46.15% |
6 / 13 |
CRAP | |
81.30% |
100 / 123 |
EntitySchema\Services\SchemaConverter\SchemaConverter | |
0.00% |
0 / 1 |
|
46.15% |
6 / 13 |
68.01 | |
81.30% |
100 / 123 |
getFullViewSchemaData | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
getMonolingualNameBadgeData | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
getFullArraySchemaData | |
100.00% |
1 / 1 |
5 | |
100.00% |
14 / 14 |
|||
getPersistenceSchemaData | |
100.00% |
1 / 1 |
5 | |
100.00% |
14 / 14 |
|||
getSchemaID | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getIdFromSchema | |
0.00% |
0 / 1 |
5.93 | |
66.67% |
6 / 9 |
|||
getSchemaText | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
getSchemaTextFromSchema | |
0.00% |
0 / 1 |
5.68 | |
70.00% |
7 / 10 |
|||
getNameBadgesFromSchema | |
100.00% |
1 / 1 |
2 | |
100.00% |
8 / 8 |
|||
getSchemaLanguages | |
0.00% |
0 / 1 |
4.16 | |
78.57% |
11 / 14 |
|||
getLabelFromSchema | |
0.00% |
0 / 1 |
8.14 | |
71.43% |
10 / 14 |
|||
getDescriptionFromSchema | |
0.00% |
0 / 1 |
8.14 | |
71.43% |
10 / 14 |
|||
getAliasGroupFromSchema | |
0.00% |
0 / 1 |
8.14 | |
71.43% |
10 / 14 |
<?php | |
namespace EntitySchema\Services\SchemaConverter; | |
use DomainException; | |
use LogicException; | |
/** | |
* Convert schema data for different uses from the persistence format | |
* | |
* @license GPL-2.0-or-later | |
*/ | |
class SchemaConverter { | |
/** | |
* @param string $schemaJSON | |
* @param string[] $preferredLanguages Name badges for these language codes will always be present, | |
* even if there is no data for them, and they will also be ordered before other languages. | |
* | |
* @return FullViewSchemaData | |
* | |
* @throws LogicException | |
*/ | |
public function getFullViewSchemaData( | |
$schemaJSON, | |
array $preferredLanguages | |
): FullViewSchemaData { | |
$schema = json_decode( $schemaJSON, true ); | |
return new FullViewSchemaData( | |
$this->getNameBadgesFromSchema( $schema, $preferredLanguages ), | |
$this->getSchemaTextFromSchema( $schema ) | |
); | |
} | |
/** | |
* @param string $schemaData | |
* @param string $langCode | |
* | |
* @return NameBadge | |
* | |
*/ | |
public function getMonolingualNameBadgeData( $schemaData, $langCode ): NameBadge { | |
$schema = json_decode( $schemaData, true ); | |
return new NameBadge( | |
$this->getLabelFromSchema( $schema, $langCode ), | |
$this->getDescriptionFromSchema( $schema, $langCode ), | |
$this->getAliasGroupFromSchema( $schema, $langCode ) | |
); | |
} | |
public function getFullArraySchemaData( $schemaJSON ): FullArraySchemaData { | |
$schema = json_decode( $schemaJSON, true ); | |
$data = [ | |
'labels' => [], | |
'descriptions' => [], | |
'aliases' => [], | |
'schemaText' => $this->getSchemaTextFromSchema( $schema ), | |
]; | |
foreach ( $this->getSchemaLanguages( $schema ) as $languageCode ) { | |
$label = $this->getLabelFromSchema( $schema, $languageCode ); | |
if ( $label ) { | |
$data['labels'][$languageCode] = $label; | |
} | |
$description = $this->getDescriptionFromSchema( $schema, $languageCode ); | |
if ( $description ) { | |
$data['descriptions'][$languageCode] = $description; | |
} | |
$aliases = $this->getAliasGroupFromSchema( $schema, $languageCode ); | |
if ( $aliases ) { | |
$data['aliases'][$languageCode] = $aliases; | |
} | |
} | |
return new FullArraySchemaData( $data ); | |
} | |
public function getPersistenceSchemaData( $schemaJSON ): PersistenceSchemaData { | |
$schema = json_decode( $schemaJSON, true ); | |
$persistenceSchemaData = new PersistenceSchemaData(); | |
$persistenceSchemaData->schemaText = $this->getSchemaTextFromSchema( $schema ); | |
foreach ( $this->getSchemaLanguages( $schema ) as $languageCode ) { | |
$label = $this->getLabelFromSchema( $schema, $languageCode ); | |
if ( $label ) { | |
$persistenceSchemaData->labels[$languageCode] = $label; | |
} | |
$description = $this->getDescriptionFromSchema( $schema, $languageCode ); | |
if ( $description ) { | |
$persistenceSchemaData->descriptions[$languageCode] = $description; | |
} | |
$aliases = $this->getAliasGroupFromSchema( $schema, $languageCode ); | |
if ( $aliases ) { | |
$persistenceSchemaData->aliases[$languageCode] = $aliases; | |
} | |
} | |
return $persistenceSchemaData; | |
} | |
public function getSchemaID( $schemaJSON ) { | |
return $this->getIdFromSchema( json_decode( $schemaJSON, true ) ); | |
} | |
private function getIdFromSchema( array $schema ) { | |
if ( !isset( $schema['serializationVersion'] ) ) { | |
return null; | |
} | |
switch ( $schema['serializationVersion'] ) { | |
case '1.0': | |
case '2.0': | |
case '3.0': | |
return $schema['id'] ?? null; | |
default: | |
throw new DomainException( | |
'Unknown schema serialization version ' . $schema['serializationVersion'] | |
); | |
} | |
} | |
/** | |
* @param string $schemaJSON | |
* | |
* @return string | |
*/ | |
public function getSchemaText( $schemaJSON ) { | |
$schema = json_decode( $schemaJSON, true ); | |
return $this->getSchemaTextFromSchema( $schema ); | |
} | |
private function getSchemaTextFromSchema( array $schema ) { | |
if ( !isset( $schema['serializationVersion'] ) ) { | |
return ''; | |
} | |
switch ( $schema['serializationVersion'] ) { | |
case '1.0': | |
case '2.0': | |
return $schema['schema'] ?? ''; | |
case '3.0': | |
return $schema['schemaText'] ?? ''; | |
default: | |
throw new DomainException( | |
'Unknown schema serialization version ' . $schema['serializationVersion'] | |
); | |
} | |
} | |
/** | |
* @param array $schema | |
* @param string[] $preferredLanguages | |
* | |
* @return NameBadge[] | |
* | |
* @throws DomainException | |
*/ | |
private function getNameBadgesFromSchema( array $schema, array $preferredLanguages ): array { | |
$langs = $this->getSchemaLanguages( $schema, $preferredLanguages ); | |
$nameBadges = []; | |
foreach ( $langs as $langCode ) { | |
$nameBadges[$langCode] = new NameBadge( | |
$this->getLabelFromSchema( $schema, $langCode ), | |
$this->getDescriptionFromSchema( $schema, $langCode ), | |
$this->getAliasGroupFromSchema( $schema, $langCode ) | |
); | |
} | |
return $nameBadges; | |
} | |
/** | |
* @param array $schema | |
* @param string[] $preferredLanguages | |
* | |
* @return string[] | |
*/ | |
private function getSchemaLanguages( $schema, array $preferredLanguages = [] ) { | |
$langs = $preferredLanguages; | |
if ( !empty( $schema['labels'] ) ) { | |
$langs = array_merge( | |
$langs, | |
array_keys( $schema['labels'] ) | |
); | |
} | |
if ( !empty( $schema['descriptions'] ) ) { | |
$langs = array_merge( | |
$langs, | |
array_keys( $schema['descriptions'] ) | |
); | |
} | |
if ( !empty( $schema['aliases'] ) ) { | |
$langs = array_merge( | |
$langs, | |
array_keys( $schema['aliases'] ) | |
); | |
} | |
return array_unique( $langs ); | |
} | |
/** | |
* @param array $schema | |
* @param string $langCode | |
* | |
* @return string | |
* | |
* @throws DomainException | |
*/ | |
private function getLabelFromSchema( array $schema, $langCode ) { | |
if ( empty( $schema['labels'] ) ) { | |
return ''; | |
} | |
if ( !isset( $schema['serializationVersion'] ) ) { | |
return ''; | |
} | |
switch ( $schema['serializationVersion'] ) { | |
case '1.0': | |
if ( isset( $schema['labels'][$langCode] ) ) { | |
return $schema['labels'][$langCode]['value']; | |
} | |
return ''; | |
case '2.0': | |
case '3.0': | |
return $schema['labels'][$langCode] ?? ''; | |
default: | |
throw new DomainException( | |
'Unknown schema serialization version ' . $schema['serializationVersion'] | |
); | |
} | |
} | |
/** | |
* @param array $schema | |
* @param string $langCode | |
* | |
* @return string | |
* | |
* @throws DomainException | |
*/ | |
private function getDescriptionFromSchema( array $schema, $langCode ) { | |
if ( empty( $schema['descriptions'] ) ) { | |
return ''; | |
} | |
if ( !isset( $schema['serializationVersion'] ) ) { | |
return ''; | |
} | |
switch ( $schema['serializationVersion'] ) { | |
case '1.0': | |
if ( isset( $schema['descriptions'][$langCode] ) ) { | |
return $schema['descriptions'][$langCode]['value']; | |
} | |
return ''; | |
case '2.0': | |
case '3.0': | |
return $schema['descriptions'][$langCode] ?? ''; | |
default: | |
throw new DomainException( | |
'Unknown schema serialization version ' . $schema['serializationVersion'] | |
); | |
} | |
} | |
/** | |
* @param array $schema | |
* @param string $langCode | |
* | |
* @return array | |
* | |
* @throws DomainException | |
*/ | |
private function getAliasGroupFromSchema( array $schema, $langCode ) { | |
if ( empty( $schema['aliases'] ) ) { | |
return []; | |
} | |
if ( !isset( $schema['serializationVersion'] ) ) { | |
return []; | |
} | |
switch ( $schema['serializationVersion'] ) { | |
case '1.0': | |
if ( isset( $schema['aliases'][$langCode] ) ) { | |
return array_column( $schema['aliases'][$langCode], 'value' ); | |
} | |
return []; | |
case '2.0': | |
case '3.0': | |
return $schema['aliases'][$langCode] ?? []; | |
default: | |
throw new DomainException( | |
'Unknown schema serialization version ' . $schema['serializationVersion'] | |
); | |
} | |
} | |
} |