-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathExtractCardinalitiesCommand.php
68 lines (55 loc) · 2.4 KB
/
ExtractCardinalitiesCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator\Command;
use ApiPlatform\SchemaGenerator\CardinalitiesExtractor;
use ApiPlatform\SchemaGenerator\GoodRelationsBridge;
use ApiPlatform\SchemaGenerator\SchemaGeneratorConfiguration;
use EasyRdf\Graph as RdfGraph;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Extracts cardinalities.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class ExtractCardinalitiesCommand extends Command
{
protected function configure(): void
{
$this
->setName('extract-cardinalities')
->setDescription('Extract properties\' cardinality')
->addOption('vocabulary-file', 's', InputOption::VALUE_REQUIRED, 'The path or URL of the vocabulary RDF file to use.', SchemaGeneratorConfiguration::SCHEMA_ORG_URI)
->addOption('cardinality-file', 'g', InputOption::VALUE_REQUIRED, 'The path or URL of the OWL file containing the cardinality definitions.', SchemaGeneratorConfiguration::GOOD_RELATIONS_URI)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$vocabFile = $input->getOption('vocabulary-file');
$relations = [];
$graph = new RdfGraph();
$format = pathinfo($vocabFile, \PATHINFO_EXTENSION) ?: 'guess';
if (str_starts_with($vocabFile, 'http://') || str_starts_with($vocabFile, 'https://')) {
$graph->load($input->getOption('vocabulary-file'), $format);
} else {
$graph->parseFile($input->getOption('vocabulary-file'), $format);
}
$relations[] = $graph;
$cardinality = [new \SimpleXMLElement($input->getOption('cardinality-file'), 0, true)];
$goodRelationsBridge = new GoodRelationsBridge($cardinality);
$cardinalitiesExtractor = new CardinalitiesExtractor($goodRelationsBridge);
$result = $cardinalitiesExtractor->extract($relations);
$output->writeln(json_encode($result, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT));
return 0;
}
}