AbstractStyle.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer\HTML\Style;
  18. use PhpOffice\PhpWord\Style\AbstractStyle as Style;
  19. /**
  20. * Style writer
  21. *
  22. * @since 0.10.0
  23. */
  24. abstract class AbstractStyle
  25. {
  26. /**
  27. * Parent writer
  28. *
  29. * @var \PhpOffice\PhpWord\Writer\AbstractWriter
  30. */
  31. private $parentWriter;
  32. /**
  33. * Style
  34. *
  35. * @var array|\PhpOffice\PhpWord\Style\AbstractStyle
  36. */
  37. private $style;
  38. /**
  39. * Write style
  40. */
  41. abstract public function write();
  42. /**
  43. * Create new instance
  44. *
  45. * @param array|\PhpOffice\PhpWord\Style\AbstractStyle $style
  46. */
  47. public function __construct($style = null)
  48. {
  49. $this->style = $style;
  50. }
  51. /**
  52. * Set parent writer.
  53. *
  54. * @param \PhpOffice\PhpWord\Writer\AbstractWriter $writer
  55. */
  56. public function setParentWriter($writer)
  57. {
  58. $this->parentWriter = $writer;
  59. }
  60. /**
  61. * Get parent writer
  62. *
  63. * @return \PhpOffice\PhpWord\Writer\AbstractWriter
  64. */
  65. public function getParentWriter()
  66. {
  67. return $this->parentWriter;
  68. }
  69. /**
  70. * Get style
  71. *
  72. * @return array|\PhpOffice\PhpWord\Style\AbstractStyle $style
  73. */
  74. public function getStyle()
  75. {
  76. if (!$this->style instanceof Style && !is_array($this->style)) {
  77. return '';
  78. }
  79. return $this->style;
  80. }
  81. /**
  82. * Takes array where of CSS properties / values and converts to CSS string
  83. *
  84. * @param array $css
  85. * @return string
  86. */
  87. protected function assembleCss($css)
  88. {
  89. $pairs = array();
  90. $string = '';
  91. foreach ($css as $key => $value) {
  92. if ($value != '') {
  93. $pairs[] = $key . ': ' . $value;
  94. }
  95. }
  96. if (!empty($pairs)) {
  97. $string = implode('; ', $pairs) . ';';
  98. }
  99. return $string;
  100. }
  101. /**
  102. * Get value if ...
  103. *
  104. * @param bool|null $condition
  105. * @param string $value
  106. * @return string
  107. */
  108. protected function getValueIf($condition, $value)
  109. {
  110. return $condition == true ? $value : '';
  111. }
  112. }