| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Console\Helper;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* @author Yewhen Khoptynskyi <khoptynskyi@gmail.com>
|
|
|
18 |
*/
|
|
|
19 |
class TableCellStyle
|
|
|
20 |
{
|
|
|
21 |
public const DEFAULT_ALIGN = 'left';
|
|
|
22 |
|
|
|
23 |
private const TAG_OPTIONS = [
|
|
|
24 |
'fg',
|
|
|
25 |
'bg',
|
|
|
26 |
'options',
|
|
|
27 |
];
|
|
|
28 |
|
|
|
29 |
private const ALIGN_MAP = [
|
|
|
30 |
'left' => \STR_PAD_RIGHT,
|
|
|
31 |
'center' => \STR_PAD_BOTH,
|
|
|
32 |
'right' => \STR_PAD_LEFT,
|
|
|
33 |
];
|
|
|
34 |
|
|
|
35 |
private array $options = [
|
|
|
36 |
'fg' => 'default',
|
|
|
37 |
'bg' => 'default',
|
|
|
38 |
'options' => null,
|
|
|
39 |
'align' => self::DEFAULT_ALIGN,
|
|
|
40 |
'cellFormat' => null,
|
|
|
41 |
];
|
|
|
42 |
|
|
|
43 |
public function __construct(array $options = [])
|
|
|
44 |
{
|
|
|
45 |
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
|
|
|
46 |
throw new InvalidArgumentException(sprintf('The TableCellStyle does not support the following options: \'%s\'.', implode('\', \'', $diff)));
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
if (isset($options['align']) && !\array_key_exists($options['align'], self::ALIGN_MAP)) {
|
|
|
50 |
throw new InvalidArgumentException(sprintf('Wrong align value. Value must be following: \'%s\'.', implode('\', \'', array_keys(self::ALIGN_MAP))));
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
$this->options = array_merge($this->options, $options);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
public function getOptions(): array
|
|
|
57 |
{
|
|
|
58 |
return $this->options;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Gets options we need for tag for example fg, bg.
|
|
|
63 |
*
|
|
|
64 |
* @return string[]
|
|
|
65 |
*/
|
|
|
66 |
public function getTagOptions(): array
|
|
|
67 |
{
|
|
|
68 |
return array_filter(
|
|
|
69 |
$this->getOptions(),
|
|
|
70 |
function ($key) {
|
|
|
71 |
return \in_array($key, self::TAG_OPTIONS) && isset($this->options[$key]);
|
|
|
72 |
},
|
|
|
73 |
\ARRAY_FILTER_USE_KEY
|
|
|
74 |
);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function getPadByAlign(): int
|
|
|
78 |
{
|
|
|
79 |
return self::ALIGN_MAP[$this->getOptions()['align']];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function getCellFormat(): ?string
|
|
|
83 |
{
|
|
|
84 |
return $this->getOptions()['cellFormat'];
|
|
|
85 |
}
|
|
|
86 |
}
|