| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xlsx; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\Sparkline; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\SparklineGroup; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Sparkline\SparklineType; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
| 9 |
use SimpleXMLElement; |
| 10 |
|
| 11 |
/** |
| 12 |
* Reads the `x14:sparklineGroups` extension from a worksheet's `extLst` and |
| 13 |
* populates the worksheet's sparkline group collection. |
| 14 |
*/ |
| 15 |
class Sparklines extends BaseParserClass |
| 16 |
{ |
| 17 |
private Worksheet $worksheet; |
| 18 |
|
| 19 |
private SimpleXMLElement $worksheetXml; |
| 20 |
|
| 21 |
public function __construct(Worksheet $workSheet, SimpleXMLElement $worksheetXml) |
| 22 |
{ |
| 23 |
$this->worksheet = $workSheet; |
| 24 |
$this->worksheetXml = $worksheetXml; |
| 25 |
} |
| 26 |
|
| 27 |
public function load(): void |
| 28 |
{ |
| 29 |
if (!isset($this->worksheetXml->extLst->ext)) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
foreach ($this->worksheetXml->extLst->ext as $ext) { |
| 34 |
$extAttrs = $ext->attributes() ?? []; |
| 35 |
if ((string) ($extAttrs['uri'] ?? '') !== Namespaces::SPARKLINE_URI) { |
| 36 |
continue; |
| 37 |
} |
| 38 |
|
| 39 |
$groups = $ext->children(Namespaces::DATA_VALIDATIONS1)->sparklineGroups; |
| 40 |
if (!isset($groups)) { |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
$groupList = $groups->children(Namespaces::DATA_VALIDATIONS1)->sparklineGroup ?? null; |
| 45 |
if ($groupList === null) { |
| 46 |
continue; |
| 47 |
} |
| 48 |
|
| 49 |
foreach ($groupList as $groupXml) { |
| 50 |
$group = $this->readGroup($groupXml); |
| 51 |
if ($group !== null) { |
| 52 |
$this->worksheet->addSparklineGroup($group); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
private function readGroup(SimpleXMLElement $groupXml): ?SparklineGroup |
| 59 |
{ |
| 60 |
$group = new SparklineGroup(); |
| 61 |
$attributes = $groupXml->attributes() ?? []; |
| 62 |
|
| 63 |
$typeValue = isset($attributes['type']) ? (string) $attributes['type'] : SparklineType::Line->value; |
| 64 |
$group->setType(SparklineType::tryFrom($typeValue) ?? SparklineType::Line); |
| 65 |
|
| 66 |
if (isset($attributes['lineWeight'])) { |
| 67 |
$group->setLineWeight((float) $attributes['lineWeight']); |
| 68 |
} |
| 69 |
if (isset($attributes['displayEmptyCellsAs'])) { |
| 70 |
$group->setDisplayEmptyCellsAs((string) $attributes['displayEmptyCellsAs']); |
| 71 |
} |
| 72 |
if (isset($attributes['minAxisType'])) { |
| 73 |
$group->setMinAxisType((string) $attributes['minAxisType']); |
| 74 |
} |
| 75 |
if (isset($attributes['maxAxisType'])) { |
| 76 |
$group->setMaxAxisType((string) $attributes['maxAxisType']); |
| 77 |
} |
| 78 |
if (isset($attributes['manualMin'])) { |
| 79 |
$group->setManualMin((float) $attributes['manualMin']); |
| 80 |
} |
| 81 |
if (isset($attributes['manualMax'])) { |
| 82 |
$group->setManualMax((float) $attributes['manualMax']); |
| 83 |
} |
| 84 |
|
| 85 |
$group->setDisplayMarkers(self::boolAttr($attributes, 'markers')); |
| 86 |
$group->setDisplayHigh(self::boolAttr($attributes, 'high')); |
| 87 |
$group->setDisplayLow(self::boolAttr($attributes, 'low')); |
| 88 |
$group->setDisplayFirst(self::boolAttr($attributes, 'first')); |
| 89 |
$group->setDisplayLast(self::boolAttr($attributes, 'last')); |
| 90 |
$group->setDisplayNegative(self::boolAttr($attributes, 'negative')); |
| 91 |
$group->setDisplayXAxis(self::boolAttr($attributes, 'displayXAxis')); |
| 92 |
$group->setDisplayHidden(self::boolAttr($attributes, 'displayHidden')); |
| 93 |
$group->setRightToLeft(self::boolAttr($attributes, 'rightToLeft')); |
| 94 |
|
| 95 |
$this->readColors($group, $groupXml); |
| 96 |
$this->readSparklines($group, $groupXml); |
| 97 |
|
| 98 |
// A group with no sparklines carries no useful information. |
| 99 |
return $group->getSparklines() === [] ? null : $group; |
| 100 |
} |
| 101 |
|
| 102 |
private function readColors(SparklineGroup $group, SimpleXMLElement $groupXml): void |
| 103 |
{ |
| 104 |
$children = $groupXml->children(Namespaces::DATA_VALIDATIONS1); |
| 105 |
$group->setColorSeries(self::color($children->colorSeries)); |
| 106 |
$group->setColorNegative(self::color($children->colorNegative)); |
| 107 |
$group->setColorAxis(self::color($children->colorAxis)); |
| 108 |
$group->setColorMarkers(self::color($children->colorMarkers)); |
| 109 |
$group->setColorFirst(self::color($children->colorFirst)); |
| 110 |
$group->setColorLast(self::color($children->colorLast)); |
| 111 |
$group->setColorHigh(self::color($children->colorHigh)); |
| 112 |
$group->setColorLow(self::color($children->colorLow)); |
| 113 |
} |
| 114 |
|
| 115 |
private function readSparklines(SparklineGroup $group, SimpleXMLElement $groupXml): void |
| 116 |
{ |
| 117 |
$sparklinesXml = $groupXml->children(Namespaces::DATA_VALIDATIONS1)->sparklines; |
| 118 |
if (!isset($sparklinesXml)) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
$sparklineList = $sparklinesXml->children(Namespaces::DATA_VALIDATIONS1)->sparkline ?? null; |
| 123 |
if ($sparklineList === null) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
foreach ($sparklineList as $sparklineXml) { |
| 128 |
$xm = $sparklineXml->children(Namespaces::DATA_VALIDATIONS2); |
| 129 |
$dataRange = isset($xm->f) ? (string) $xm->f : ''; |
| 130 |
$location = isset($xm->sqref) ? (string) $xm->sqref : ''; |
| 131 |
if ($location === '') { |
| 132 |
continue; |
| 133 |
} |
| 134 |
$group->addSparkline(new Sparkline($location, $dataRange)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param mixed $attributes |
| 140 |
*/ |
| 141 |
private static function boolAttr($attributes, string $name): bool |
| 142 |
{ |
| 143 |
if (!($attributes instanceof SimpleXMLElement)) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
$attrs = $attributes; |
| 147 |
|
| 148 |
return isset($attrs[$name]) && self::boolean((string) $attrs[$name]); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Extract an ARGB colour from an `x14:color*` element's `rgb` attribute. |
| 153 |
*/ |
| 154 |
private static function color(?SimpleXMLElement $colorXml): ?string |
| 155 |
{ |
| 156 |
if ($colorXml === null || !isset($colorXml[0])) { |
| 157 |
return null; |
| 158 |
} |
| 159 |
$attrs = $colorXml->attributes() ?? []; |
| 160 |
|
| 161 |
return isset($attrs['rgb']) ? (string) $attrs['rgb'] : null; |
| 162 |
} |
| 163 |
} |
| 164 |
|