composer package updates
[openemr.git] / vendor / sabberworm / php-css-parser / lib / Sabberworm / CSS / Rule / Rule.php
blob3e4853753b9dde6679b9606828a4454f46db6c51
1 <?php
3 namespace Sabberworm\CSS\Rule;
5 use Sabberworm\CSS\Renderable;
6 use Sabberworm\CSS\Value\RuleValueList;
7 use Sabberworm\CSS\Value\Value;
8 use Sabberworm\CSS\Comment\Commentable;
10 /**
11 * RuleSets contains Rule objects which always have a key and a value.
12 * In CSS, Rules are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];”
14 class Rule implements Renderable, Commentable {
16 private $sRule;
17 private $mValue;
18 private $bIsImportant;
19 private $aIeHack;
20 protected $iLineNo;
21 protected $aComments;
23 public function __construct($sRule, $iLineNo = 0) {
24 $this->sRule = $sRule;
25 $this->mValue = null;
26 $this->bIsImportant = false;
27 $this->aIeHack = array();
28 $this->iLineNo = $iLineNo;
29 $this->aComments = array();
32 /**
33 * @return int
35 public function getLineNo() {
36 return $this->iLineNo;
39 public function setRule($sRule) {
40 $this->sRule = $sRule;
43 public function getRule() {
44 return $this->sRule;
47 public function getValue() {
48 return $this->mValue;
51 public function setValue($mValue) {
52 $this->mValue = $mValue;
55 /**
56 * @deprecated Old-Style 2-dimensional array given. Retained for (some) backwards-compatibility. Use setValue() instead and wrapp the value inside a RuleValueList if necessary.
58 public function setValues($aSpaceSeparatedValues) {
59 $oSpaceSeparatedList = null;
60 if (count($aSpaceSeparatedValues) > 1) {
61 $oSpaceSeparatedList = new RuleValueList(' ', $this->iLineNo);
63 foreach ($aSpaceSeparatedValues as $aCommaSeparatedValues) {
64 $oCommaSeparatedList = null;
65 if (count($aCommaSeparatedValues) > 1) {
66 $oCommaSeparatedList = new RuleValueList(',', $this->iLineNo);
68 foreach ($aCommaSeparatedValues as $mValue) {
69 if (!$oSpaceSeparatedList && !$oCommaSeparatedList) {
70 $this->mValue = $mValue;
71 return $mValue;
73 if ($oCommaSeparatedList) {
74 $oCommaSeparatedList->addListComponent($mValue);
75 } else {
76 $oSpaceSeparatedList->addListComponent($mValue);
79 if (!$oSpaceSeparatedList) {
80 $this->mValue = $oCommaSeparatedList;
81 return $oCommaSeparatedList;
82 } else {
83 $oSpaceSeparatedList->addListComponent($oCommaSeparatedList);
86 $this->mValue = $oSpaceSeparatedList;
87 return $oSpaceSeparatedList;
90 /**
91 * @deprecated Old-Style 2-dimensional array returned. Retained for (some) backwards-compatibility. Use getValue() instead and check for the existance of a (nested set of) ValueList object(s).
93 public function getValues() {
94 if (!$this->mValue instanceof RuleValueList) {
95 return array(array($this->mValue));
97 if ($this->mValue->getListSeparator() === ',') {
98 return array($this->mValue->getListComponents());
100 $aResult = array();
101 foreach ($this->mValue->getListComponents() as $mValue) {
102 if (!$mValue instanceof RuleValueList || $mValue->getListSeparator() !== ',') {
103 $aResult[] = array($mValue);
104 continue;
106 if ($this->mValue->getListSeparator() === ' ' || count($aResult) === 0) {
107 $aResult[] = array();
109 foreach ($mValue->getListComponents() as $mValue) {
110 $aResult[count($aResult) - 1][] = $mValue;
113 return $aResult;
117 * Adds a value to the existing value. Value will be appended if a RuleValueList exists of the given type. Otherwise, the existing value will be wrapped by one.
119 public function addValue($mValue, $sType = ' ') {
120 if (!is_array($mValue)) {
121 $mValue = array($mValue);
123 if (!$this->mValue instanceof RuleValueList || $this->mValue->getListSeparator() !== $sType) {
124 $mCurrentValue = $this->mValue;
125 $this->mValue = new RuleValueList($sType, $this->iLineNo);
126 if ($mCurrentValue) {
127 $this->mValue->addListComponent($mCurrentValue);
130 foreach ($mValue as $mValueItem) {
131 $this->mValue->addListComponent($mValueItem);
135 public function addIeHack($iModifier) {
136 $this->aIeHack[] = $iModifier;
139 public function setIeHack(array $aModifiers) {
140 $this->aIeHack = $aModifiers;
143 public function getIeHack() {
144 return $this->aIeHack;
147 public function setIsImportant($bIsImportant) {
148 $this->bIsImportant = $bIsImportant;
151 public function getIsImportant() {
152 return $this->bIsImportant;
155 public function __toString() {
156 return $this->render(new \Sabberworm\CSS\OutputFormat());
159 public function render(\Sabberworm\CSS\OutputFormat $oOutputFormat) {
160 $sResult = "{$this->sRule}:{$oOutputFormat->spaceAfterRuleName()}";
161 if ($this->mValue instanceof Value) { //Can also be a ValueList
162 $sResult .= $this->mValue->render($oOutputFormat);
163 } else {
164 $sResult .= $this->mValue;
166 if (!empty($this->aIeHack)) {
167 $sResult .= ' \\' . implode('\\', $this->aIeHack);
169 if ($this->bIsImportant) {
170 $sResult .= ' !important';
172 $sResult .= ';';
173 return $sResult;
177 * @param array $aComments Array of comments.
179 public function addComments(array $aComments) {
180 $this->aComments = array_merge($this->aComments, $aComments);
184 * @return array
186 public function getComments() {
187 return $this->aComments;
191 * @param array $aComments Array containing Comment objects.
193 public function setComments(array $aComments) {
194 $this->aComments = $aComments;