Esempio n. 1
0
 /**
  * Reads and validate XML file.
  *
  * @param string $file
  *
  * @return EnumDescriptor[]
  *
  * @throws \RuntimeException
  */
 public function fromFile($file)
 {
     /** @var \DOMDocument $xmlDomDocument */
     if (!($xmlDomDocument = XmlUtils::loadFile($file, __DIR__ . '/../xsd/enums.xsd'))) {
         throw new \RuntimeException(sprintf('Invalid enums xml file "%s".', $file));
     }
     /** @var array $xmlData */
     if (!($xmlData = XmlUtils::convertDomElementToArray($xmlDomDocument->firstChild))) {
         throw new \RuntimeException('Invalid enum DOM object.');
     }
     $namespace = $xmlData['enums']['namespace'];
     $filePath = substr($file, 0, -strlen(basename($file)) - 1);
     $enumsPath = str_replace(':', '/', $namespace);
     // invalid enum file location
     if (substr($filePath, -strlen($enumsPath)) !== $enumsPath) {
         throw new \RuntimeException(sprintf('Invalid enums xml directory "%s". Expected sub-directory "%s".', $filePath, $enumsPath));
     }
     // get language options
     $languages = [];
     foreach ($xmlData['enums'] as $key => $value) {
         if (substr($key, -8) == '-options') {
             $languages[$key] = $value;
         }
     }
     $enums = [];
     if (isset($xmlData['enums']['enum'])) {
         $xmlData['enums']['enum'] = $this->fixArray($xmlData['enums']['enum'], 'name');
         foreach ($xmlData['enums']['enum'] as $enum) {
             $enumId = EnumId::fromString(sprintf('%s:%s', $namespace, $enum['name']));
             // duplicate schema
             if (array_key_exists($enumId->toString(), $enums)) {
                 throw new \RuntimeException(sprintf('Duplicate enum "%s" in file "%s".', $enumId->toString(), $file));
             }
             $enums[] = $this->parse(array_merge($enum, $languages, ['namespace' => $namespace]));
         }
     }
     return $enums;
 }
Esempio n. 2
0
 /**
  * @param string $file
  *
  * @return array
  *
  * @throws \RuntimeException
  */
 private function getXmlData($file)
 {
     /** @var \DOMDocument $xmlDomDocument */
     if (!($xmlDomDocument = XmlUtils::loadFile($file, __DIR__ . '/../xsd/schema.xsd'))) {
         throw new \RuntimeException(sprintf('Invalid schema xml file "%s".', $file));
     }
     /** @var array $xmlData */
     if (!($xmlData = XmlUtils::convertDomElementToArray($xmlDomDocument->firstChild))) {
         throw new \RuntimeException('Invalid schema DOM object.');
     }
     $schemaId = SchemaId::fromString($xmlData['schema']['id']);
     $filePath = substr($file, 0, -strlen(basename($file)) - 1);
     $schemaPath = str_replace([':', '//'], ['/', '/'], $schemaId->getCurie());
     // invalid schema file location
     if (substr($filePath, -strlen($schemaPath)) !== $schemaPath) {
         throw new \RuntimeException(sprintf('Invalid schema xml directory "%s". Expected sub-directory "%s".', $filePath, $schemaPath));
     }
     // validate version to file
     if (basename($file) != 'latest.xml' && basename($file) != sprintf('%s.xml', $schemaId->getVersion()->toString())) {
         throw new \RuntimeException(sprintf('Invalid schema xml file "%s" name. Expected name "%s.xml".', $file, $schemaId->getVersion()->toString()));
     }
     return $xmlData;
 }