|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Slince/China package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Slince <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace China\Command\Dashboard; |
|
12
|
|
|
|
|
13
|
|
|
use China\Region\Location\AddressInterface; |
|
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
17
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
18
|
|
|
|
|
19
|
|
|
class ShowRegionCommand extends DashboardCommand |
|
20
|
|
|
{ |
|
21
|
|
|
protected static $types = [ |
|
22
|
|
|
AddressInterface::TYPE_PROVINCE => '省', |
|
23
|
|
|
AddressInterface::TYPE_CITY => '市', |
|
24
|
|
|
AddressInterface::TYPE_AREA => '区 ', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function configure() |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setName('dashboard:region'); |
|
33
|
|
|
$this->addOption('parent', 'p', InputOption::VALUE_OPTIONAL); |
|
34
|
|
|
$this->setDescription('展示我国省市县信息'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
41
|
|
|
{ |
|
42
|
|
|
$regionService = $this->getChina()->getRegion(); |
|
43
|
|
|
$style = new SymfonyStyle($input, $output); |
|
44
|
|
|
$headers = ['Code', '名称', '类型']; |
|
45
|
|
|
|
|
46
|
|
|
if ($parentName = $input->getOption('parent')) { |
|
47
|
|
|
$parent = $regionService->findByName($parentName) ?: $regionService->findByCode($parentName); |
|
48
|
|
|
if ($parent === false) { |
|
49
|
|
|
throw new \InvalidArgumentException(sprintf('你提供的上级地区“%s”似乎是不存在的', $parentName)); |
|
50
|
|
|
} |
|
51
|
|
|
$regions = $parent->getChildren()->toArray(); |
|
52
|
|
|
} else { |
|
53
|
|
|
$regions = $regionService->getProvinces()->toArray(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$rows = array_map(function(AddressInterface $address){ |
|
57
|
|
|
return [ |
|
58
|
|
|
$address->getCode(), |
|
59
|
|
|
"<info>{$address->getName()}</info>", |
|
60
|
|
|
static::$types[$address->getType()], |
|
61
|
|
|
]; |
|
62
|
|
|
}, $regions); |
|
63
|
|
|
$style->table($headers, $rows); |
|
64
|
|
|
} |
|
65
|
|
|
} |