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
|
|
|
$propertyType = $input->getArgument(self::ARGUMENT_PROPERTY_TYPE); |
44
|
|
|
$searchBy = $input->getArgument(self::ARGUMENT_SEARCH_BY); |
45
|
|
|
$from = $input->getOption(self::OPTION_FROM); |
46
|
|
|
$repository = $this->getRepositoryByName($from); |
47
|
|
|
$db = new Database($repository); |
48
|
|
|
$characters = $this->resolveCodepoints($db, $propertyType, $searchBy); |
49
|
|
|
|
50
|
|
|
foreach ($characters as $character) { |
51
|
|
|
$codepoint = $character->getCodepoint(); |
52
|
|
|
$properties = $character->getGeneralProperties(); |
53
|
|
|
$names = $properties->getNames(); |
54
|
|
|
$primary = $names->getPrimary(); |
55
|
|
|
$message = sprintf('%s: %s - %s', $codepoint, $primary, $codepoint->toUTF8()); |
56
|
|
|
$output->writeln($message); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Database $db |
62
|
|
|
* @param string $propertyType |
63
|
|
|
* @param string $searchBy |
64
|
|
|
* @return Collection|CodepointAssigned[] |
65
|
|
|
* @throws UnexpectedValueException |
66
|
|
|
*/ |
67
|
|
|
private function resolveCodepoints(Database $db, $propertyType, $searchBy) |
68
|
|
|
{ |
69
|
|
|
switch ($propertyType) { |
70
|
|
|
case self::PROPERTY_BLOCK: |
71
|
|
|
$block = Block::fromValue($searchBy); |
72
|
|
|
return $db->getByBlock($block); |
73
|
|
|
case self::PROPERTY_CATEGORY: |
74
|
|
|
$category = GeneralCategory::fromValue($searchBy); |
75
|
|
|
return $db->getByCategory($category); |
76
|
|
|
case self::PROPERTY_SCRIPT: |
77
|
|
|
$script = Script::fromValue($searchBy); |
78
|
|
|
return $db->getByScript($script); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
throw new UnexpectedValueException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return InputDefinition |
86
|
|
|
*/ |
87
|
|
View Code Duplication |
private function createInputDefinition() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$propertyType = new InputArgument( |
90
|
|
|
self::ARGUMENT_PROPERTY_TYPE, |
91
|
|
|
InputArgument::REQUIRED, |
92
|
|
|
'Property type. Choose from: block, category, script' |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
$searchBy = new InputArgument( |
96
|
|
|
self::ARGUMENT_SEARCH_BY, |
97
|
|
|
InputArgument::REQUIRED, |
98
|
|
|
'What to search by' |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
$repositoryNames = $this->getRepositoryNames(); |
102
|
|
|
$namesList = implode(', ', $repositoryNames); |
103
|
|
|
|
104
|
|
|
$from = new InputOption( |
105
|
|
|
self::OPTION_FROM, |
106
|
|
|
null, |
107
|
|
|
InputOption::VALUE_OPTIONAL, |
108
|
|
|
sprintf('Repository from which codepoints should be resolved. Choose from: %s', $namesList), |
109
|
|
|
array_shift($repositoryNames) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return new InputDefinition([$propertyType, $searchBy, $from]); |
113
|
|
|
} |
114
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.