PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpspreadsheet/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php

https://github.com/mackensen/moodle
PHP | 149 lines | 120 code | 23 blank | 6 comment | 18 complexity | 982835ad091f4cef3795d6885b85b5a9 MD5 | raw file
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  3. use PhpOffice\PhpSpreadsheet\Style\Conditional;
  4. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar;
  5. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormattingRuleExtension;
  6. use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormatValueObject;
  7. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  8. use SimpleXMLElement;
  9. class ConditionalStyles
  10. {
  11. private $worksheet;
  12. private $worksheetXml;
  13. private $dxfs;
  14. public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml, array $dxfs = [])
  15. {
  16. $this->worksheet = $workSheet;
  17. $this->worksheetXml = $worksheetXml;
  18. $this->dxfs = $dxfs;
  19. }
  20. public function load(): void
  21. {
  22. $this->setConditionalStyles(
  23. $this->worksheet,
  24. $this->readConditionalStyles($this->worksheetXml),
  25. $this->worksheetXml->extLst
  26. );
  27. }
  28. private function readConditionalStyles($xmlSheet)
  29. {
  30. $conditionals = [];
  31. foreach ($xmlSheet->conditionalFormatting as $conditional) {
  32. foreach ($conditional->cfRule as $cfRule) {
  33. if (Conditional::isValidConditionType((string) $cfRule['type']) && isset($this->dxfs[(int) ($cfRule['dxfId'])])) {
  34. $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
  35. } elseif ((string) $cfRule['type'] == Conditional::CONDITION_DATABAR) {
  36. $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule;
  37. }
  38. }
  39. }
  40. return $conditionals;
  41. }
  42. private function setConditionalStyles(Worksheet $worksheet, array $conditionals, $xmlExtLst): void
  43. {
  44. foreach ($conditionals as $ref => $cfRules) {
  45. ksort($cfRules);
  46. $conditionalStyles = $this->readStyleRules($cfRules, $xmlExtLst);
  47. // Extract all cell references in $ref
  48. $cellBlocks = explode(' ', str_replace('$', '', strtoupper($ref)));
  49. foreach ($cellBlocks as $cellBlock) {
  50. $worksheet->getStyle($cellBlock)->setConditionalStyles($conditionalStyles);
  51. }
  52. }
  53. }
  54. private function readStyleRules($cfRules, $extLst)
  55. {
  56. $conditionalFormattingRuleExtensions = ConditionalFormattingRuleExtension::parseExtLstXml($extLst);
  57. $conditionalStyles = [];
  58. foreach ($cfRules as $cfRule) {
  59. $objConditional = new Conditional();
  60. $objConditional->setConditionType((string) $cfRule['type']);
  61. $objConditional->setOperatorType((string) $cfRule['operator']);
  62. if ((string) $cfRule['text'] != '') {
  63. $objConditional->setText((string) $cfRule['text']);
  64. }
  65. if (isset($cfRule['stopIfTrue']) && (int) $cfRule['stopIfTrue'] === 1) {
  66. $objConditional->setStopIfTrue(true);
  67. }
  68. if (count($cfRule->formula) > 1) {
  69. foreach ($cfRule->formula as $formula) {
  70. $objConditional->addCondition((string) $formula);
  71. }
  72. } else {
  73. $objConditional->addCondition((string) $cfRule->formula);
  74. }
  75. if (isset($cfRule->dataBar)) {
  76. $objConditional->setDataBar(
  77. $this->readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions)
  78. );
  79. } else {
  80. $objConditional->setStyle(clone $this->dxfs[(int) ($cfRule['dxfId'])]);
  81. }
  82. $conditionalStyles[] = $objConditional;
  83. }
  84. return $conditionalStyles;
  85. }
  86. private function readDataBarOfConditionalRule($cfRule, $conditionalFormattingRuleExtensions): ConditionalDataBar
  87. {
  88. $dataBar = new ConditionalDataBar();
  89. //dataBar attribute
  90. if (isset($cfRule->dataBar['showValue'])) {
  91. $dataBar->setShowValue((bool) $cfRule->dataBar['showValue']);
  92. }
  93. //dataBar children
  94. //conditionalFormatValueObjects
  95. $cfvoXml = $cfRule->dataBar->cfvo;
  96. $cfvoIndex = 0;
  97. foreach ((count($cfvoXml) > 1 ? $cfvoXml : [$cfvoXml]) as $cfvo) {
  98. if ($cfvoIndex === 0) {
  99. $dataBar->setMinimumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
  100. }
  101. if ($cfvoIndex === 1) {
  102. $dataBar->setMaximumConditionalFormatValueObject(new ConditionalFormatValueObject((string) $cfvo['type'], (string) $cfvo['val']));
  103. }
  104. ++$cfvoIndex;
  105. }
  106. //color
  107. if (isset($cfRule->dataBar->color)) {
  108. $dataBar->setColor((string) $cfRule->dataBar->color['rgb']);
  109. }
  110. //extLst
  111. $this->readDataBarExtLstOfConditionalRule($dataBar, $cfRule, $conditionalFormattingRuleExtensions);
  112. return $dataBar;
  113. }
  114. private function readDataBarExtLstOfConditionalRule(ConditionalDataBar $dataBar, $cfRule, $conditionalFormattingRuleExtensions): void
  115. {
  116. if (isset($cfRule->extLst)) {
  117. $ns = $cfRule->extLst->getNamespaces(true);
  118. foreach ((count($cfRule->extLst) > 0 ? $cfRule->extLst->ext : [$cfRule->extLst->ext]) as $ext) {
  119. $extId = (string) $ext->children($ns['x14'])->id;
  120. if (isset($conditionalFormattingRuleExtensions[$extId]) && (string) $ext['uri'] === '{B025F937-C7B1-47D3-B67F-A62EFF666E3E}') {
  121. $dataBar->setConditionalFormattingRuleExt($conditionalFormattingRuleExtensions[$extId]);
  122. }
  123. }
  124. }
  125. }
  126. }