diff --git a/src/PhpPresentation/Shape/Chart/Type/AbstractType.php b/src/PhpPresentation/Shape/Chart/Type/AbstractType.php index 99b42c430..d888b232c 100644 --- a/src/PhpPresentation/Shape/Chart/Type/AbstractType.php +++ b/src/PhpPresentation/Shape/Chart/Type/AbstractType.php @@ -52,6 +52,13 @@ abstract class AbstractType implements ComparableInterface */ private $series = []; + /** + * Returns xml element name for a chart type implementation + * + * @return string + */ + abstract public function getXmlElementName(): string; + /** * Has Axis X? */ diff --git a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php index 600bf7391..bab4dfa12 100644 --- a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php +++ b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeBar.php @@ -21,7 +21,7 @@ /** * \PhpOffice\PhpPresentation\Shape\Chart\Type\Bar. */ -class AbstractTypeBar extends AbstractType +abstract class AbstractTypeBar extends AbstractType { /** Orientation of bars */ public const DIRECTION_VERTICAL = 'col'; diff --git a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php index 32c5e7f0e..42159077d 100644 --- a/src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php +++ b/src/PhpPresentation/Shape/Chart/Type/AbstractTypeLine.php @@ -18,7 +18,7 @@ namespace PhpOffice\PhpPresentation\Shape\Chart\Type; -class AbstractTypeLine extends AbstractType +abstract class AbstractTypeLine extends AbstractType { /** * Is Line Smooth? diff --git a/src/PhpPresentation/Shape/Chart/Type/AbstractTypePie.php b/src/PhpPresentation/Shape/Chart/Type/AbstractTypePie.php index 08d40b3ce..2c55d8c0b 100644 --- a/src/PhpPresentation/Shape/Chart/Type/AbstractTypePie.php +++ b/src/PhpPresentation/Shape/Chart/Type/AbstractTypePie.php @@ -21,7 +21,7 @@ /** * \PhpOffice\PhpPresentation\Shape\Chart\Type\Bar. */ -class AbstractTypePie extends AbstractType +abstract class AbstractTypePie extends AbstractType { /** * Create a new self instance. diff --git a/src/PhpPresentation/Shape/Chart/Type/Area.php b/src/PhpPresentation/Shape/Chart/Type/Area.php index a8c39ff00..817a49e33 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Area.php +++ b/src/PhpPresentation/Shape/Chart/Type/Area.php @@ -39,4 +39,9 @@ public function getHashCode(): string return md5($hash . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:areaChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Bar.php b/src/PhpPresentation/Shape/Chart/Type/Bar.php index 602b69d53..4b0a8338d 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Bar.php +++ b/src/PhpPresentation/Shape/Chart/Type/Bar.php @@ -34,4 +34,9 @@ public function getHashCode(): string { return md5(parent::getHashCode() . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:barChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Bar3D.php b/src/PhpPresentation/Shape/Chart/Type/Bar3D.php index b2127862d..1ab79e094 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Bar3D.php +++ b/src/PhpPresentation/Shape/Chart/Type/Bar3D.php @@ -34,4 +34,9 @@ public function getHashCode(): string { return md5(parent::getHashCode() . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:bar3DChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Doughnut.php b/src/PhpPresentation/Shape/Chart/Type/Doughnut.php index 33da681fd..d09f0922a 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Doughnut.php +++ b/src/PhpPresentation/Shape/Chart/Type/Doughnut.php @@ -69,4 +69,9 @@ public function getHashCode(): string { return md5(parent::getHashCode() . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:doughnutChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Line.php b/src/PhpPresentation/Shape/Chart/Type/Line.php index 4d1f07cad..20df71555 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Line.php +++ b/src/PhpPresentation/Shape/Chart/Type/Line.php @@ -36,4 +36,9 @@ public function getHashCode(): string return md5(parent::getHashCode() . $hash . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:lineChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Pie.php b/src/PhpPresentation/Shape/Chart/Type/Pie.php index 94c9ff20b..4e5e6a269 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Pie.php +++ b/src/PhpPresentation/Shape/Chart/Type/Pie.php @@ -34,4 +34,9 @@ public function getHashCode(): string { return md5(parent::getHashCode() . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:pieChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Pie3D.php b/src/PhpPresentation/Shape/Chart/Type/Pie3D.php index 1ee43bce9..6821748d1 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Pie3D.php +++ b/src/PhpPresentation/Shape/Chart/Type/Pie3D.php @@ -34,4 +34,9 @@ public function getHashCode(): string { return md5(parent::getHashCode() . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:pie3DChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Radar.php b/src/PhpPresentation/Shape/Chart/Type/Radar.php index 85525f10e..e6e63852c 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Radar.php +++ b/src/PhpPresentation/Shape/Chart/Type/Radar.php @@ -36,4 +36,9 @@ public function getHashCode(): string return md5($hash . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:radarChart'; + } } diff --git a/src/PhpPresentation/Shape/Chart/Type/Scatter.php b/src/PhpPresentation/Shape/Chart/Type/Scatter.php index 3cb2bba43..60ef7eed4 100644 --- a/src/PhpPresentation/Shape/Chart/Type/Scatter.php +++ b/src/PhpPresentation/Shape/Chart/Type/Scatter.php @@ -36,4 +36,9 @@ public function getHashCode(): string return md5(parent::getHashCode() . $hash . __CLASS__); } + + public function getXmlElementName(): string + { + return 'c:scatterChart'; + } } diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index ea6f3a7f7..4176b61ec 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -701,7 +701,7 @@ protected function writeLayout(XMLWriter $objWriter, $subject): void protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $includeSheet = false): void { // c:lineChart - $objWriter->startElement('c:areaChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:grouping $objWriter->startElement('c:grouping'); @@ -804,7 +804,7 @@ protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $incl protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includeSheet = false): void { // c:barChart - $objWriter->startElement('c:barChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:barDir $objWriter->startElement('c:barDir'); @@ -1017,7 +1017,7 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $includeSheet = false): void { // c:bar3DChart - $objWriter->startElement('c:bar3DChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:barDir $objWriter->startElement('c:barDir'); @@ -1210,7 +1210,7 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $in protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bool $includeSheet = false): void { // c:pieChart - $objWriter->startElement('c:doughnutChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:varyColors $objWriter->startElement('c:varyColors'); @@ -1369,7 +1369,7 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includeSheet = false): void { // c:pieChart - $objWriter->startElement('c:pieChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:varyColors $objWriter->startElement('c:varyColors'); @@ -1534,7 +1534,7 @@ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $includeSheet = false): void { // c:pie3DChart - $objWriter->startElement('c:pie3DChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:varyColors $objWriter->startElement('c:varyColors'); @@ -1693,7 +1693,7 @@ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $in protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $includeSheet = false): void { // c:lineChart - $objWriter->startElement('c:lineChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:grouping $objWriter->startElement('c:grouping'); @@ -1864,7 +1864,7 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $includeSheet = false): void { // c:scatterChart - $objWriter->startElement('c:radarChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:radarStyle $objWriter->startElement('c:radarStyle'); @@ -2033,7 +2033,7 @@ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $in protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool $includeSheet = false): void { // c:scatterChart - $objWriter->startElement('c:scatterChart'); + $objWriter->startElement($subject->getXmlElementName()); // c:scatterStyle $objWriter->startElement('c:scatterStyle'); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index 6dad04c85..1e7a9d707 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -82,15 +82,22 @@ public function testChartDisplayBlankAs(): void $this->assertIsSchemaECMA376Valid(); } - public function testChartIncludeSpreadsheet(): void + /** + * @dataProvider dataProviderIncludedSpreadsheet + */ + public function testChartIncludeSpreadsheet(string $chartType): void { $oSlide = $this->oPresentation->getActiveSlide(); $oShape = $oSlide->createChartShape(); $oShape->setIncludeSpreadsheet(true); - $oLine = new Line(); + /** @var AbstractType $oChart */ + $oChart = new $chartType(); $oSeries = new Series('Downloads', $this->seriesData); - $oLine->addSeries($oSeries); - $oShape->getPlotArea()->setType($oLine); + $oChart->addSeries($oSeries); + $oShape->getPlotArea()->setType($oChart); + + $chartElementName = $oChart->getXmlElementName(); + $chartBaseXmlPath = sprintf('/c:chartSpace/c:chart/c:plotArea/%s', $chartElementName); $this->assertTrue($oShape->hasIncludedSpreadsheet()); @@ -103,26 +110,26 @@ public function testChartIncludeSpreadsheet(): void $element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData'; $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart'; + $element = $chartBaseXmlPath; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser'; + $element = $chartBaseXmlPath . '/c:ser'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $this->assertZipXmlElementCount('ppt/charts/' . $oShape->getIndexedFilename(), $element, 1); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:v'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:v'; $this->assertZipXmlElementNotExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:f'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:f'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Sheet1!$B$1'); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache/c:pt'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache/c:pt'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:strRef/c:strCache/c:pt/c:v'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Downloads'); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:val/c:numRef/c:numCache/c:pt/c:v'; + $element = $chartBaseXmlPath . '/c:ser/c:val/c:numRef/c:numCache/c:pt/c:v'; $this->assertZipXmlElementCount('ppt/charts/' . $oShape->getIndexedFilename(), $element, count($this->seriesData)); foreach (array_values($this->seriesData) as $index => $value) { $this->assertZipXmlElementAtIndexEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, $index, $value); @@ -149,11 +156,11 @@ public function testChartIncludeSpreadsheet(): void $element = '/p:sld/p:cSld/p:spTree/p:graphicFrame/a:graphic/a:graphicData'; $this->assertZipXmlElementExists('ppt/slides/slide1.xml', $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart'; + $element = $chartBaseXmlPath; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser'; + $element = $chartBaseXmlPath . '/c:ser'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); - $element = '/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser/c:tx/c:v'; + $element = $chartBaseXmlPath . '/c:ser/c:tx/c:v'; $this->assertZipXmlElementExists('ppt/charts/' . $oShape->getIndexedFilename(), $element); $this->assertZipXmlElementEquals('ppt/charts/' . $oShape->getIndexedFilename(), $element, 'Downloads'); $element = '/c:chartSpace/c:externalData'; @@ -1483,4 +1490,25 @@ public function dataProviderMarkerSymbol(): iterable yield [$symbol]; } } + + /** + * @return array> + */ + public function dataProviderIncludedSpreadsheet(): iterable + { + $chartTypes = [ + Area::class, + Bar::class, + Bar3D::class, + Doughnut::class, + Pie::class, + Pie3D::class, + Line::class, + Radar::class, + Scatter::class, + ]; + foreach ($chartTypes as $chartType) { + yield [$chartType]; + } + } } diff --git a/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php b/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php index 6f75fbd42..02b408ec9 100644 --- a/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php +++ b/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php @@ -252,7 +252,7 @@ public function assertZipXmlElementExists($filePath, $xPath): void * @param string $xPath * @param int $index */ - public function assertZipXmlElementAtIndexExists($filePath, $xPath, $index): void + public function assertZipXmlElementAtIndexExists(string $filePath, string $xPath, int $index): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -306,7 +306,7 @@ public function assertZipXmlElementEquals($filePath, $xPath, $value): void * @param int $index * @param mixed $value */ - public function assertZipXmlElementAtIndexEquals($filePath, $xPath, $index, $value): void + public function assertZipXmlElementAtIndexEquals(string $filePath, string $xPath, int $index, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath);