Nav apraksta

AbstractStyle.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 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. * @return void
  56. */
  57. public function setParentWriter($writer)
  58. {
  59. $this->parentWriter = $writer;
  60. }
  61. /**
  62. * Get parent writer
  63. *
  64. * @return \PhpOffice\PhpWord\Writer\AbstractWriter
  65. */
  66. public function getParentWriter()
  67. {
  68. return $this->parentWriter;
  69. }
  70. /**
  71. * Get style
  72. *
  73. * @return array|\PhpOffice\PhpWord\Style\AbstractStyle $style
  74. */
  75. public function getStyle()
  76. {
  77. if (!$this->style instanceof Style && !is_array($this->style)) {
  78. return '';
  79. }
  80. return $this->style;
  81. }
  82. /**
  83. * Takes array where of CSS properties / values and converts to CSS string
  84. *
  85. * @param array $css
  86. * @return string
  87. */
  88. protected function assembleCss($css)
  89. {
  90. $pairs = array();
  91. $string = '';
  92. foreach ($css as $key => $value) {
  93. if ($value != '') {
  94. $pairs[] = $key . ': ' . $value;
  95. }
  96. }
  97. if (!empty($pairs)) {
  98. $string = implode('; ', $pairs) . ';';
  99. }
  100. return $string;
  101. }
  102. /**
  103. * Get value if ...
  104. *
  105. * @param bool|null $condition
  106. * @param string $value
  107. * @return string
  108. */
  109. protected function getValueIf($condition, $value)
  110. {
  111. return $condition == true ? $value : '';
  112. }
  113. }