|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | +* CSSRuleSets contains CSSRule objects which always have a key and a value. |
| 5 | +* In CSS, CSSRules are expressed as follows: “key: value[0][0] value[0][1], value[1][0] value[1][1];” |
| 6 | +*/ |
| 7 | +class CSSRule { |
| 8 | + private $sRule; |
| 9 | + private $mValue; |
| 10 | + private $bIsImportant; |
| 11 | + |
| 12 | + public function __construct($sRule) { |
| 13 | + $this->sRule = $sRule; |
| 14 | + $this->mValue = null; |
| 15 | + $this->bIsImportant = false; |
| 16 | + } |
| 17 | + |
| 18 | + public function setRule($sRule) { |
| 19 | + $this->sRule = $sRule; |
| 20 | + } |
| 21 | + |
| 22 | + public function getRule() { |
| 23 | + return $this->sRule; |
| 24 | + } |
| 25 | + |
| 26 | + public function getValue() { |
| 27 | + return $this->mValue; |
| 28 | + } |
| 29 | + |
| 30 | + public function setValue($mValue) { |
| 31 | + $this->mValue = $mValue; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @deprecated Old-Style 2-dimensional array given. Retained for (some) backwards-compatibility. Use setValue() instead and wrapp the value inside a CSSRuleValueList if necessary. |
| 36 | + */ |
| 37 | + public function setValues($aSpaceSeparatedValues) { |
| 38 | + $oSpaceSeparatedList = null; |
| 39 | + if(count($aSpaceSeparatedValues) > 1) { |
| 40 | + $oSpaceSeparatedList = new CSSRuleValueList(' '); |
| 41 | + } |
| 42 | + foreach($aSpaceSeparatedValues as $aCommaSeparatedValues) { |
| 43 | + $oCommaSeparatedList = null; |
| 44 | + if(count($aCommaSeparatedValues) > 1) { |
| 45 | + $oCommaSeparatedList = new CSSRuleValueList(','); |
| 46 | + } |
| 47 | + foreach($aCommaSeparatedValues as $mValue) { |
| 48 | + if(!$oSpaceSeparatedList && !$oCommaSeparatedList) { |
| 49 | + $this->mValue = $mValue; |
| 50 | + return $mValue; |
| 51 | + } |
| 52 | + if($oCommaSeparatedList) { |
| 53 | + $oCommaSeparatedList->addListComponent($mValue); |
| 54 | + } else { |
| 55 | + $oSpaceSeparatedList->addListComponent($mValue); |
| 56 | + } |
| 57 | + } |
| 58 | + if(!$oSpaceSeparatedList) { |
| 59 | + $this->mValue = $oCommaSeparatedList; |
| 60 | + return $oCommaSeparatedList; |
| 61 | + } else { |
| 62 | + $oSpaceSeparatedList->addListComponent($oCommaSeparatedList); |
| 63 | + } |
| 64 | + } |
| 65 | + $this->mValue = $oSpaceSeparatedList; |
| 66 | + return $oSpaceSeparatedList; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @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) CSSValueList object(s). |
| 71 | + */ |
| 72 | + public function getValues() { |
| 73 | + if(!$this->mValue instanceof CSSRuleValueList) { |
| 74 | + return array(array($this->mValue)); |
| 75 | + } |
| 76 | + if($this->mValue->getListSeparator() === ',') { |
| 77 | + return array($this->mValue->getListComponents()); |
| 78 | + } |
| 79 | + $aResult = array(); |
| 80 | + foreach($this->mValue->getListComponents() as $mValue) { |
| 81 | + if(!$mValue instanceof CSSRuleValueList || $mValue->getListSeparator() !== ',') { |
| 82 | + $aResult[] = array($mValue); |
| 83 | + continue; |
| 84 | + } |
| 85 | + if($this->mValue->getListSeparator() === ' ' || count($aResult) === 0) { |
| 86 | + $aResult[] = array(); |
| 87 | + } |
| 88 | + foreach($mValue->getListComponents() as $mValue) { |
| 89 | + $aResult[count($aResult)-1][] = $mValue; |
| 90 | + } |
| 91 | + } |
| 92 | + return $aResult; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Adds a value to the existing value. Value will be appended if a CSSRuleValueList exists of the given type. Otherwise, the existing value will be wrapped by one. |
| 97 | + */ |
| 98 | + public function addValue($mValue, $sType = ' ') { |
| 99 | + if(!is_array($mValue)) { |
| 100 | + $mValue = array($mValue); |
| 101 | + } |
| 102 | + if(!$this->mValue instanceof CSSRuleValueList || $this->mValue->getListSeparator() !== $sType) { |
| 103 | + $mCurrentValue = $this->mValue; |
| 104 | + $this->mValue = new CSSRuleValueList($sType); |
| 105 | + if($mCurrentValue) { |
| 106 | + $this->mValue->addListComponent($mCurrentValue); |
| 107 | + } |
| 108 | + } |
| 109 | + foreach($mValue as $mValueItem) { |
| 110 | + $this->mValue->addListComponent($mValueItem); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public function setIsImportant($bIsImportant) { |
| 115 | + $this->bIsImportant = $bIsImportant; |
| 116 | + } |
| 117 | + |
| 118 | + public function getIsImportant() { |
| 119 | + return $this->bIsImportant; |
| 120 | + } |
| 121 | + |
| 122 | + public function __toString() { |
| 123 | + $sResult = "{$this->sRule}: "; |
| 124 | + if($this->mValue instanceof CSSValue) { //Can also be a CSSValueList |
| 125 | + $sResult .= $this->mValue->__toString(); |
| 126 | + } else { |
| 127 | + $sResult .= $this->mValue; |
| 128 | + } |
| 129 | + if($this->bIsImportant) { |
| 130 | + $sResult .= ' !important'; |
| 131 | + } |
| 132 | + $sResult .= ';'; |
| 133 | + return $sResult; |
| 134 | + } |
| 135 | +} |
0 commit comments