|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UCD\Console\Application\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
use UCD\Database; |
|
12
|
|
|
use UCD\Exception\UnexpectedValueException; |
|
13
|
|
|
use UCD\Unicode\Character\Collection; |
|
14
|
|
|
use UCD\Unicode\Character\Properties\General\Block; |
|
15
|
|
|
use UCD\Unicode\Character\Properties\General\GeneralCategory; |
|
16
|
|
|
use UCD\Unicode\Character\Properties\General\Script; |
|
17
|
|
|
use UCD\Unicode\CodepointAssigned; |
|
18
|
|
|
|
|
19
|
|
|
class PropertiesCommand extends RepositoryUtilisingCommand |
|
20
|
|
|
{ |
|
21
|
|
|
const OPTION_FROM = 'from'; |
|
22
|
|
|
const ARGUMENT_PROPERTY_TYPE = 'property-type'; |
|
23
|
|
|
const ARGUMENT_SEARCH_BY = 'value'; |
|
24
|
|
|
const COMMAND_NAME = 'properties'; |
|
25
|
|
|
const PROPERTY_BLOCK = 'block'; |
|
26
|
|
|
const PROPERTY_CATEGORY = 'category'; |
|
27
|
|
|
const PROPERTY_SCRIPT = 'script'; |
|
28
|
|
|
|
|
29
|
|
|
protected function configure() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->setName(self::COMMAND_NAME); |
|
32
|
|
|
$this->setDescription('List codepoints by property'); |
|
33
|
|
|
$this->setDefinition($this->createInputDefinition()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param InputInterface $input |
|
38
|
|
|
* @param OutputInterface $output |
|
39
|
|
|
* @return int |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
42
|
|
|
{ |
|
43
|
|
|
$start = microtime(true); |
|
44
|
|
|
$propertyType = $input->getArgument(self::ARGUMENT_PROPERTY_TYPE); |
|
45
|
|
|
$searchBy = $input->getArgument(self::ARGUMENT_SEARCH_BY); |
|
46
|
|
|
$from = $input->getOption(self::OPTION_FROM); |
|
47
|
|
|
$repository = $this->getRepositoryByName($from); |
|
48
|
|
|
$db = new Database($repository); |
|
49
|
|
|
$characters = $this->resolveCodepoints($db, $propertyType, $searchBy); |
|
50
|
|
|
|
|
51
|
|
|
$output->writeln(sprintf('<info>%s "%s"</info>', ucfirst($propertyType), $searchBy)); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($characters as $character) { |
|
54
|
|
|
$codepoint = $character->getCodepoint(); |
|
55
|
|
|
$properties = $character->getGeneralProperties(); |
|
56
|
|
|
$names = $properties->getNames(); |
|
57
|
|
|
$primary = $names->getPrimary(); |
|
58
|
|
|
$message = sprintf('%s: %s - %s', $codepoint, $primary, $codepoint->toUTF8()); |
|
59
|
|
|
$output->writeln($message); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$output->writeln(sprintf('Memory peak: %.5f MB', memory_get_peak_usage() / 1048576)); |
|
63
|
|
|
$output->writeln(sprintf('Took: %.5f seconds', microtime(true) - $start)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param Database $db |
|
68
|
|
|
* @param string $propertyType |
|
69
|
|
|
* @param string $searchBy |
|
70
|
|
|
* @return Collection|CodepointAssigned[] |
|
71
|
|
|
* @throws UnexpectedValueException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function resolveCodepoints(Database $db, $propertyType, $searchBy) |
|
74
|
|
|
{ |
|
75
|
|
|
switch ($propertyType) { |
|
76
|
|
|
case self::PROPERTY_BLOCK: |
|
77
|
|
|
$block = Block::fromValue($searchBy); |
|
78
|
|
|
return $db->getByBlock($block); |
|
79
|
|
|
case self::PROPERTY_CATEGORY: |
|
80
|
|
|
$category = GeneralCategory::fromValue($searchBy); |
|
81
|
|
|
return $db->getByCategory($category); |
|
82
|
|
|
case self::PROPERTY_SCRIPT: |
|
83
|
|
|
$script = Script::fromValue($searchBy); |
|
84
|
|
|
return $db->getByScript($script); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw new UnexpectedValueException(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return InputDefinition |
|
92
|
|
|
*/ |
|
93
|
|
|
private function createInputDefinition() |
|
94
|
|
|
{ |
|
95
|
|
|
$propertyType = new InputArgument( |
|
96
|
|
|
self::ARGUMENT_PROPERTY_TYPE, |
|
97
|
|
|
InputArgument::REQUIRED, |
|
98
|
|
|
'Property type. Choose from: block, category, script' |
|
99
|
|
|
); |
|
100
|
|
|
|
|
101
|
|
|
$searchBy = new InputArgument( |
|
102
|
|
|
self::ARGUMENT_SEARCH_BY, |
|
103
|
|
|
InputArgument::REQUIRED, |
|
104
|
|
|
'What to search by' |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
$repositoryNames = $this->getRepositoryNames(); |
|
108
|
|
|
$namesList = implode(', ', $repositoryNames); |
|
109
|
|
|
|
|
110
|
|
|
$from = new InputOption( |
|
111
|
|
|
self::OPTION_FROM, |
|
112
|
|
|
null, |
|
113
|
|
|
InputOption::VALUE_OPTIONAL, |
|
114
|
|
|
sprintf('Repository from which codepoints should be resolved. Choose from: %s', $namesList), |
|
115
|
|
|
array_shift($repositoryNames) |
|
116
|
|
|
); |
|
117
|
|
|
|
|
118
|
|
|
return new InputDefinition([$propertyType, $searchBy, $from]); |
|
119
|
|
|
} |
|
120
|
|
|
} |