ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ConditionalFormattingRuleExtension.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
9 {
10  const CONDITION_EXTENSION_DATABAR = 'dataBar';
11 
13  private $id;
14 
16  private $cfRule;
17 
21  private $dataBar;
22 
24  private $sqref;
25 
29  public function __construct($id = null, string $cfRule = self::CONDITION_EXTENSION_DATABAR)
30  {
31  if (null === $id) {
32  $this->id = '{' . $this->generateUuid() . '}';
33  } else {
34  $this->id = $id;
35  }
36  $this->cfRule = $cfRule;
37  }
38 
39  private function generateUuid()
40  {
41  $chars = str_split('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
42 
43  foreach ($chars as $i => $char) {
44  if ($char === 'x') {
45  $chars[$i] = dechex(random_int(0, 15));
46  } elseif ($char === 'y') {
47  $chars[$i] = dechex(random_int(8, 11));
48  }
49  }
50 
51  return implode('', $chars);
52  }
53 
54  public static function parseExtLstXml($extLstXml)
55  {
56  $conditionalFormattingRuleExtensions = [];
57  $conditionalFormattingRuleExtensionXml = null;
58  if ($extLstXml instanceof SimpleXMLElement) {
59  foreach ((count($extLstXml) > 0 ? $extLstXml : [$extLstXml]) as $extLst) {
60  //this uri is conditionalFormattings
61  //https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/07d607af-5618-4ca2-b683-6a78dc0d9627
62  if (isset($extLst->ext['uri']) && (string) $extLst->ext['uri'] === '{78C0D931-6437-407d-A8EE-F0AAD7539E65}') {
63  $conditionalFormattingRuleExtensionXml = $extLst->ext;
64  }
65  }
66 
67  if ($conditionalFormattingRuleExtensionXml) {
68  $ns = $conditionalFormattingRuleExtensionXml->getNamespaces(true);
69  $extFormattingsXml = $conditionalFormattingRuleExtensionXml->children($ns['x14']);
70 
71  foreach ($extFormattingsXml->children($ns['x14']) as $extFormattingXml) {
72  $extCfRuleXml = $extFormattingXml->cfRule;
73  if (((string) $extCfRuleXml->attributes()->type) !== Conditional::CONDITION_DATABAR) {
74  continue;
75  }
76 
77  $extFormattingRuleObj = new self((string) $extCfRuleXml->attributes()->id);
78  $extFormattingRuleObj->setSqref((string) $extFormattingXml->children($ns['xm'])->sqref);
79  $conditionalFormattingRuleExtensions[$extFormattingRuleObj->getId()] = $extFormattingRuleObj;
80 
81  $extDataBarObj = new ConditionalDataBarExtension();
82  $extFormattingRuleObj->setDataBarExt($extDataBarObj);
83  $dataBarXml = $extCfRuleXml->dataBar;
84  self::parseExtDataBarAttributesFromXml($extDataBarObj, $dataBarXml);
85  self::parseExtDataBarElementChildrenFromXml($extDataBarObj, $dataBarXml, $ns);
86  }
87  }
88  }
89 
90  return $conditionalFormattingRuleExtensions;
91  }
92 
93  private static function parseExtDataBarAttributesFromXml(
94  ConditionalDataBarExtension $extDataBarObj,
95  SimpleXMLElement $dataBarXml
96  ): void {
97  $dataBarAttribute = $dataBarXml->attributes();
98  if ($dataBarAttribute->minLength) {
99  $extDataBarObj->setMinLength((int) $dataBarAttribute->minLength);
100  }
101  if ($dataBarAttribute->maxLength) {
102  $extDataBarObj->setMaxLength((int) $dataBarAttribute->maxLength);
103  }
104  if ($dataBarAttribute->border) {
105  $extDataBarObj->setBorder((bool) (string) $dataBarAttribute->border);
106  }
107  if ($dataBarAttribute->gradient) {
108  $extDataBarObj->setGradient((bool) (string) $dataBarAttribute->gradient);
109  }
110  if ($dataBarAttribute->direction) {
111  $extDataBarObj->setDirection((string) $dataBarAttribute->direction);
112  }
113  if ($dataBarAttribute->negativeBarBorderColorSameAsPositive) {
114  $extDataBarObj->setNegativeBarBorderColorSameAsPositive((bool) (string) $dataBarAttribute->negativeBarBorderColorSameAsPositive);
115  }
116  if ($dataBarAttribute->axisPosition) {
117  $extDataBarObj->setAxisPosition((string) $dataBarAttribute->axisPosition);
118  }
119  }
120 
121  private static function parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, $ns): void
122  {
123  if ($dataBarXml->borderColor) {
124  $extDataBarObj->setBorderColor((string) $dataBarXml->borderColor->attributes()['rgb']);
125  }
126  if ($dataBarXml->negativeFillColor) {
127  $extDataBarObj->setNegativeFillColor((string) $dataBarXml->negativeFillColor->attributes()['rgb']);
128  }
129  if ($dataBarXml->negativeBorderColor) {
130  $extDataBarObj->setNegativeBorderColor((string) $dataBarXml->negativeBorderColor->attributes()['rgb']);
131  }
132  if ($dataBarXml->axisColor) {
133  $axisColorAttr = $dataBarXml->axisColor->attributes();
134  $extDataBarObj->setAxisColor((string) $axisColorAttr['rgb'], (string) $axisColorAttr['theme'], (string) $axisColorAttr['tint']);
135  }
136  $cfvoIndex = 0;
137  foreach ($dataBarXml->cfvo as $cfvo) {
138  $f = (string) $cfvo->children($ns['xm'])->f;
139  if ($cfvoIndex === 0) {
140  $extDataBarObj->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo->attributes()['type'], null, (empty($f) ? null : $f)));
141  }
142  if ($cfvoIndex === 1) {
143  $extDataBarObj->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo->attributes()['type'], null, (empty($f) ? null : $f)));
144  }
145  ++$cfvoIndex;
146  }
147  }
148 
152  public function getId()
153  {
154  return $this->id;
155  }
156 
160  public function setId($id): self
161  {
162  $this->id = $id;
163 
164  return $this;
165  }
166 
167  public function getCfRule(): string
168  {
169  return $this->cfRule;
170  }
171 
172  public function setCfRule(string $cfRule): self
173  {
174  $this->cfRule = $cfRule;
175 
176  return $this;
177  }
178 
180  {
181  return $this->dataBar;
182  }
183 
185  {
186  $this->dataBar = $dataBar;
187 
188  return $this;
189  }
190 
191  public function getSqref(): string
192  {
193  return $this->sqref;
194  }
195 
196  public function setSqref(string $sqref): self
197  {
198  $this->sqref = $sqref;
199 
200  return $this;
201  }
202 }
static parseExtDataBarElementChildrenFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml, $ns)
static parseExtDataBarAttributesFromXml(ConditionalDataBarExtension $extDataBarObj, SimpleXMLElement $dataBarXml)
setMinimumConditionalFormatValueObject(ConditionalFormatValueObject $minimumConditionalFormatValueObject)
$i
Definition: disco.tpl.php:19
__construct($id=null, string $cfRule=self::CONDITION_EXTENSION_DATABAR)
ConditionalFormattingRuleExtension constructor.
setMaximumConditionalFormatValueObject(ConditionalFormatValueObject $maximumConditionalFormatValueObject)