1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bobbyshaw\WatsonVisualRecognition\Commands; |
4
|
|
|
|
5
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Message\ClassifiersResponse; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
10
|
|
|
use Bobbyshaw\WatsonVisualRecognition\Classifier; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Command to get a list of classifiers |
14
|
|
|
* |
15
|
|
|
* @package Bobbyshaw\WatsonVisualRecognition\Commands |
16
|
|
|
* @author Tom Robertshaw <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class GetClassifiersCommand extends BaseCommand |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Configure command |
22
|
|
|
*/ |
23
|
2 |
|
protected function configure() |
24
|
|
|
{ |
25
|
2 |
|
parent::configure(); |
26
|
|
|
|
27
|
2 |
|
$this |
28
|
2 |
|
->setName('classifiers:get') |
29
|
2 |
|
->setDescription('Get Classifiers') |
30
|
2 |
|
->addOption( |
31
|
2 |
|
'api-verbose', |
32
|
2 |
|
'-a', |
33
|
2 |
|
InputOption::VALUE_NONE, |
34
|
|
|
'Enable verbose API request' |
35
|
2 |
|
); |
36
|
2 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute command |
40
|
|
|
* |
41
|
|
|
* @param InputInterface $input |
42
|
|
|
* @param OutputInterface $output |
43
|
|
|
* @return void |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
|
|
$config = [ |
49
|
2 |
|
'username' => $input->getArgument('username'), |
50
|
2 |
|
'password' => $input->getArgument('password'), |
51
|
2 |
|
]; |
52
|
|
|
|
53
|
2 |
|
if ($version = $input->getOption('version-date')) { |
54
|
2 |
|
$config['version'] = $version; |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
$this->client->initialize($config); |
58
|
|
|
|
59
|
2 |
|
$params = []; |
60
|
2 |
|
if ($verbose = $input->getOption('api-verbose')) { |
61
|
1 |
|
$params['verbose'] = $verbose; |
62
|
1 |
|
} |
63
|
2 |
|
$request = $this->client->getClassifiers($params); |
64
|
|
|
|
65
|
|
|
/** @var ClassifiersResponse $response */ |
66
|
2 |
|
$response = $request->send(); |
67
|
|
|
|
68
|
2 |
|
$classifiers = $response->getClassifiers(); |
69
|
|
|
|
70
|
2 |
|
$tableRows = []; |
71
|
|
|
/** @var Classifier $classifier */ |
72
|
2 |
|
foreach ($classifiers as $classifier) { |
73
|
2 |
|
$row = [$classifier->getId(), $classifier->getName()]; |
74
|
2 |
|
if ($verbose) { |
75
|
1 |
|
$row = array_merge($row, [$classifier->getOwner(), $classifier->getCreated()->format('Y-m-d H:i:s')]); |
76
|
1 |
|
} |
77
|
|
|
|
78
|
2 |
|
$tableRows[] = $row; |
79
|
2 |
|
} |
80
|
|
|
|
81
|
2 |
|
$headers = ['ID', 'Name']; |
82
|
2 |
|
if ($verbose) { |
83
|
1 |
|
$headers = array_merge($headers, ['Owner', 'Created At']); |
84
|
1 |
|
} |
85
|
|
|
|
86
|
2 |
|
$table = new Table($output); |
87
|
2 |
|
$table->setHeaders($headers)->setRows($tableRows)->render(); |
88
|
2 |
|
} |
89
|
|
|
} |
90
|
|
|
|