Open Journal Systems  3.3.0
FormattingTrait.php
1 <?php
2 /*
3  * citeproc-php
4  *
5  * @link http://github.com/seboettg/citeproc-php for the source repository
6  * @copyright Copyright (c) 2016 Sebastian Böttger.
7  * @license https://opensource.org/licenses/MIT
8  */
9 
10 namespace Seboettg\CiteProc\Styles;
11 
13 use SimpleXMLElement;
14 
20 trait FormattingTrait
21 {
22 
26  private static $formattingAttributes = [
27  'font-style',
28  'font-family',
29  'font-weight',
30  'font-variant',
31  'text-decoration',
32  'vertical-align'
33  ];
34 
38  private $formattingOptions;
39 
43  private $stripPeriods = false;
44 
48  private $format;
49 
53  protected function initFormattingAttributes(SimpleXMLElement $node)
54  {
55  $this->formattingOptions = new ArrayList();
56 
58  foreach ($node->attributes() as $attribute) {
59 
61  $name = (string) $attribute->getName();
62  $value = (string) $attribute;
63 
64  if (in_array($name, self::$formattingAttributes)) {
65  $this->formattingOptions->add($name, $value);
66  continue;
67  }
68  }
69  }
70 
71 
72  protected function format($text)
73  {
74  if (empty($text)) {
75  return $text;
76  }
77 
78  if (!empty($this->formattingOptions)) {
79  $format = "";
80  foreach ($this->formattingOptions as $option => $optionValue) {
81  if ($optionValue === "italic") {
82  $text = "<i>$text</i>";
83  } elseif ($optionValue === "bold") {
84  $text = "<b>$text</b>";
85  } elseif ($optionValue === "normal") {
86  $text = "$text";
87  } elseif ($option === "vertical-align") {
88  if ($optionValue === "sub") {
89  $text = "<sub>$text</sub>";
90  } elseif ($optionValue === "sup") {
91  $text = "<sup>$text</sup>";
92  }
93  } elseif ($option === "text-decoration" && $optionValue === "none") {
94  $format .= "";
95  } else {
96  $format .= "$option: $optionValue;";
97  }
98  }
99  if (!empty($format)) {
100  $text = '<span style="' . $format . '">' . $text . '</span>';
101  }
102  }
103  return $text;
104  }
105 }
Seboettg\CiteProc\Styles\FormattingTrait
trait FormattingTrait
Definition: FormattingTrait.php:21
Seboettg\CiteProc\Styles
Definition: AffixesTrait.php:10
Seboettg\CiteProc\Styles\initFormattingAttributes
initFormattingAttributes(SimpleXMLElement $node)
Definition: FormattingTrait.php:62
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20
Seboettg\CiteProc\Styles\format
format($text)
Definition: FormattingTrait.php:81