setName('types:tree')
->setDescription('Show types and terms')
->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms')
->addOption('filter', 'f', InputOption::VALUE_REQUIRED, 'Filter types by ID or by name')
->setHelp("%command.name%
List all types of terms and terms.
Usage: types:tree [OPTIONS]");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new Client();
$configLoader = new ConfigLoader();
if (!isset($configLoader->getConfig()['auth']['token'])) {
$output->writeln('You must login.');
return;
}
$client->setAuthorization($configLoader->getConfig()['auth']['token']);
try {
$categoriesReponse = $client->getCategoriesTree();
$termsResponse = $client->getTermsTree();
foreach (array($categoriesReponse, $termsResponse) as $response) {
if ($response->hasError()) {
$output->writeln(sprintf(
'%s (%d)',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
}
$filter = $input->getOption('filter');
if (!empty($filter)) {
if (is_numeric($filter)) {
$filter = (int) $filter;
}
}
foreach ($termsResponse->getData() as $categoryId => $termTypes) {
$stop = false;
foreach ($termTypes as $termTypeId => $termType) {
if ($filter !== null) {
if (is_int($filter)) {
if ((int) $termTypeId === $filter) {
$stop = true;
} else {
continue;
}
} else {
if (0 === preg_match(sprintf('/%s/U', preg_quote($filter)), $termType['type'])) {
continue;
}
}
}
$output->writeln(sprintf('%3d %s', $termTypeId, $termType['type']));
if ($input->getOption('terms')) {
$isFirst = true;
foreach ($termType['terms'] as $termId => $term) {
$char = '|';
if ($isFirst) {
$isFirst = false;
$char = '`';
}
$output->writeln(sprintf(' %s- %4d %s', $char, $termId, $term));
}
$output->writeln('');
}
if ($stop) {
return;
}
}
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. %s', $e->getMessage()));
}
}
}