TableCellStyle.php 2.2 KB

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