TableCellStyle.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 const TAG_OPTIONS = [
  19. 'fg',
  20. 'bg',
  21. 'options',
  22. ];
  23. private const ALIGN_MAP = [
  24. 'left' => \STR_PAD_RIGHT,
  25. 'center' => \STR_PAD_BOTH,
  26. 'right' => \STR_PAD_LEFT,
  27. ];
  28. private $options = [
  29. 'fg' => 'default',
  30. 'bg' => 'default',
  31. 'options' => null,
  32. 'align' => self::DEFAULT_ALIGN,
  33. 'cellFormat' => null,
  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'], self::ALIGN_MAP)) {
  41. throw new InvalidArgumentException(sprintf('Wrong align value. Value must be following: \'%s\'.', implode('\', \'', array_keys(self::ALIGN_MAP))));
  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, self::TAG_OPTIONS) && isset($this->options[$key]);
  60. },
  61. \ARRAY_FILTER_USE_KEY
  62. );
  63. }
  64. /**
  65. * @return int
  66. */
  67. public function getPadByAlign()
  68. {
  69. return self::ALIGN_MAP[$this->getOptions()['align']];
  70. }
  71. public function getCellFormat(): ?string
  72. {
  73. return $this->getOptions()['cellFormat'];
  74. }
  75. }