TableCellStyle.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  29. * @var mixed[]
  30. */
  31. private $options = [
  32. 'fg' => 'default',
  33. 'bg' => 'default',
  34. 'options' => null,
  35. 'align' => self::DEFAULT_ALIGN,
  36. 'cellFormat' => null,
  37. ];
  38. /**
  39. * @param mixed[] $options
  40. */
  41. public function __construct($options = [])
  42. {
  43. if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
  44. throw new InvalidArgumentException(sprintf('The TableCellStyle does not support the following options: \'%s\'.', implode('\', \'', $diff)));
  45. }
  46. if (isset($options['align']) && !\array_key_exists($options['align'], self::ALIGN_MAP)) {
  47. throw new InvalidArgumentException(sprintf('Wrong align value. Value must be following: \'%s\'.', implode('\', \'', array_keys(self::ALIGN_MAP))));
  48. }
  49. $this->options = array_merge($this->options, $options);
  50. }
  51. public function getOptions()
  52. {
  53. return $this->options;
  54. }
  55. /**
  56. * Gets options we need for tag for example fg, bg.
  57. *
  58. * @return string[]
  59. */
  60. public function getTagOptions()
  61. {
  62. return array_filter(
  63. $this->getOptions(),
  64. function ($key) {
  65. return \in_array($key, self::TAG_OPTIONS) && isset($this->options[$key]);
  66. },
  67. \ARRAY_FILTER_USE_KEY
  68. );
  69. }
  70. public function getPadByAlign()
  71. {
  72. return self::ALIGN_MAP[$this->getOptions()['align']];
  73. }
  74. public function getCellFormat()
  75. {
  76. return $this->getOptions()['cellFormat'];
  77. }
  78. }