From cea5f34260472aac57aa2fcb5b8baf70843639c3 Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Tue, 7 Sep 2021 12:24:37 +0200 Subject: [PATCH] #634 : Set specific exceptions --- src/PhpPresentation/AbstractShape.php | 15 +- .../Exception/DirectoryNotFoundException.php | 32 +++ .../FeatureNotImplementedException.php | 29 +++ .../Exception/FileCopyException.php | 33 ++++ .../Exception/FileNotFoundException.php | 32 +++ .../Exception/FileRemoveException.php | 32 +++ .../Exception/InvalidClassException.php | 33 ++++ .../Exception/InvalidFileFormatException.php | 42 ++++ .../Exception/InvalidParameterException.php | 33 ++++ .../Exception/OutOfBoundsException.php | 34 ++++ .../Exception/PhpPresentationException.php | 27 +++ ...ShapeContainerAlreadyAssignedException.php | 32 +++ .../UnauthorizedMimetypeException.php | 37 ++++ .../Exception/UndefinedChartTypeException.php | 29 +++ src/PhpPresentation/HashTable.php | 4 - src/PhpPresentation/IOFactory.php | 57 +++--- src/PhpPresentation/PhpPresentation.php | 27 +-- src/PhpPresentation/Reader/ODPresentation.php | 56 ++---- src/PhpPresentation/Reader/PowerPoint2007.php | 65 ++----- src/PhpPresentation/Reader/PowerPoint97.php | 183 +++++++++--------- .../Reader/ReaderInterface.php | 2 - src/PhpPresentation/Reader/Serialized.php | 24 +-- src/PhpPresentation/Shape/Chart/Legend.php | 2 - src/PhpPresentation/Shape/Chart/PlotArea.php | 5 +- src/PhpPresentation/Shape/Chart/Title.php | 2 - src/PhpPresentation/Shape/Drawing/Base64.php | 9 +- src/PhpPresentation/Shape/Drawing/File.php | 13 +- src/PhpPresentation/Shape/Drawing/ZipFile.php | 9 +- src/PhpPresentation/Shape/Group.php | 34 ++-- src/PhpPresentation/Shape/Hyperlink.php | 24 ++- src/PhpPresentation/Shape/RichText.php | 28 +-- .../Shape/RichText/Paragraph.php | 14 +- src/PhpPresentation/Shape/RichText/Run.php | 4 +- .../Shape/RichText/TextElement.php | 5 - src/PhpPresentation/Shape/Table.php | 43 ++-- src/PhpPresentation/Shape/Table/Cell.php | 25 +-- src/PhpPresentation/Shape/Table/Row.php | 74 ++++--- src/PhpPresentation/Slide/AbstractSlide.php | 21 +- .../Slide/Background/Image.php | 9 +- src/PhpPresentation/Slide/Iterator.php | 5 - src/PhpPresentation/Slide/Note.php | 9 +- src/PhpPresentation/Slide/SlideMaster.php | 45 ++--- src/PhpPresentation/Style/Alignment.php | 22 +-- src/PhpPresentation/Style/Border.php | 46 ++--- src/PhpPresentation/Style/Fill.php | 5 - src/PhpPresentation/Style/TextStyle.php | 7 +- .../Writer/AbstractDecoratorWriter.php | 2 +- src/PhpPresentation/Writer/AbstractWriter.php | 14 +- src/PhpPresentation/Writer/ODPresentation.php | 32 +-- .../Writer/ODPresentation/Content.php | 49 ++--- .../Writer/ODPresentation/Meta.php | 7 +- .../Writer/ODPresentation/MetaInfManifest.php | 4 +- .../Writer/ODPresentation/Mimetype.php | 8 +- .../Writer/ODPresentation/ObjectsChart.php | 59 ++---- .../Writer/ODPresentation/Pictures.php | 4 +- .../Writer/ODPresentation/Styles.php | 5 - .../ODPresentation/ThumbnailsThumbnail.php | 9 +- src/PhpPresentation/Writer/PowerPoint2007.php | 42 ++-- .../AbstractDecoratorWriter.php | 22 +-- .../Writer/PowerPoint2007/AbstractSlide.php | 54 ++---- .../Writer/PowerPoint2007/CommentAuthors.php | 7 +- .../Writer/PowerPoint2007/ContentTypes.php | 28 +-- .../Writer/PowerPoint2007/DocPropsApp.php | 8 +- .../Writer/PowerPoint2007/DocPropsCore.php | 8 +- .../Writer/PowerPoint2007/DocPropsCustom.php | 8 +- .../PowerPoint2007/DocPropsThumbnail.php | 9 +- .../Writer/PowerPoint2007/PptCharts.php | 84 ++++---- .../Writer/PowerPoint2007/PptComments.php | 6 +- .../Writer/PowerPoint2007/PptMedia.php | 7 +- .../Writer/PowerPoint2007/PptPresProps.php | 7 +- .../Writer/PowerPoint2007/PptPresentation.php | 7 +- .../Writer/PowerPoint2007/PptSlideLayouts.php | 15 +- .../Writer/PowerPoint2007/PptSlideMasters.php | 19 +- .../Writer/PowerPoint2007/PptSlides.php | 15 +- .../Writer/PowerPoint2007/PptTableProps.php | 7 +- .../Writer/PowerPoint2007/PptTheme.php | 9 +- .../Writer/PowerPoint2007/PptViewProps.php | 7 +- .../Writer/PowerPoint2007/Relationships.php | 15 +- src/PhpPresentation/Writer/Serialized.php | 19 +- .../Writer/WriterInterface.php | 4 +- .../Tests/AbstractShapeTest.php | 5 +- tests/PhpPresentation/Tests/IOFactoryTest.php | 16 +- .../Tests/PhpPresentationTest.php | 13 +- .../Tests/Reader/ODPresentationTest.php | 19 +- .../Tests/Reader/PowerPoint2007Test.php | 19 +- .../Tests/Reader/PowerPoint97Test.php | 19 +- .../Tests/Reader/SerializedTest.php | 31 +-- .../Tests/Shape/Chart/PlotAreaTest.php | 33 ++-- .../Tests/Shape/Drawing/Base64Test.php | 5 +- .../Tests/Shape/Drawing/FileTest.php | 5 +- .../Tests/Shape/Drawing/ZipFileTest.php | 8 +- .../Tests/Shape/RichTextTest.php | 25 +-- .../Tests/Shape/Table/CellTest.php | 9 +- .../Tests/Shape/Table/RowTest.php | 21 +- .../PhpPresentation/Tests/Shape/TableTest.php | 15 +- .../Tests/Slide/Background/ImageTest.php | 5 +- .../Tests/Style/AlignmentTest.php | 5 +- .../ODPresentation/MetaInfManifestTest.php | 21 +- .../Tests/Writer/ODPresentationTest.php | 20 +- .../Writer/PowerPoint2007/PptChartsTest.php | 6 +- .../Writer/PowerPoint2007/PptMediaTest.php | 23 ++- .../Tests/Writer/PowerPoint2007Test.php | 34 +--- .../Tests/Writer/SerializedTest.php | 39 ++-- 103 files changed, 1220 insertions(+), 1094 deletions(-) create mode 100644 src/PhpPresentation/Exception/DirectoryNotFoundException.php create mode 100644 src/PhpPresentation/Exception/FeatureNotImplementedException.php create mode 100644 src/PhpPresentation/Exception/FileCopyException.php create mode 100644 src/PhpPresentation/Exception/FileNotFoundException.php create mode 100644 src/PhpPresentation/Exception/FileRemoveException.php create mode 100644 src/PhpPresentation/Exception/InvalidClassException.php create mode 100644 src/PhpPresentation/Exception/InvalidFileFormatException.php create mode 100644 src/PhpPresentation/Exception/InvalidParameterException.php create mode 100644 src/PhpPresentation/Exception/OutOfBoundsException.php create mode 100644 src/PhpPresentation/Exception/PhpPresentationException.php create mode 100644 src/PhpPresentation/Exception/ShapeContainerAlreadyAssignedException.php create mode 100644 src/PhpPresentation/Exception/UnauthorizedMimetypeException.php create mode 100644 src/PhpPresentation/Exception/UndefinedChartTypeException.php diff --git a/src/PhpPresentation/AbstractShape.php b/src/PhpPresentation/AbstractShape.php index 2015d0451..c8d3df00d 100644 --- a/src/PhpPresentation/AbstractShape.php +++ b/src/PhpPresentation/AbstractShape.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation; +use PhpOffice\PhpPresentation\Exception\ShapeContainerAlreadyAssignedException; use PhpOffice\PhpPresentation\Shape\Hyperlink; use PhpOffice\PhpPresentation\Shape\Placeholder; use PhpOffice\PhpPresentation\Style\Border; @@ -146,7 +147,7 @@ public function getContainer(): ?ShapeContainerInterface * @param ShapeContainerInterface $pValue * @param bool $pOverrideOld If a Slide has already been assigned, overwrite it and remove image from old Slide? * - * @throws \Exception + * @throws ShapeContainerAlreadyAssignedException * * @return $this */ @@ -175,7 +176,7 @@ public function setContainer(ShapeContainerInterface $pValue = null, $pOverrideO // Set new \PhpOffice\PhpPresentation\Slide $this->setContainer($pValue); } else { - throw new \Exception(sprintf('A %s has already been assigned. Shapes can only exist on one %s.', ShapeContainerInterface::class, ShapeContainerInterface::class)); + throw new ShapeContainerAlreadyAssignedException(self::class); } } @@ -328,8 +329,6 @@ public function getShadow(): ?Shadow } /** - * @throws \Exception - * * @return $this */ public function setShadow(Shadow $pValue = null) @@ -350,9 +349,7 @@ public function hasHyperlink() } /** - * Get Hyperlink. - * - * @throws \Exception + * Get Hyperlink */ public function getHyperlink(): Hyperlink { @@ -364,9 +361,7 @@ public function getHyperlink(): Hyperlink } /** - * Set Hyperlink. - * - * @throws \Exception + * Set Hyperlink */ public function setHyperlink(Hyperlink $pHyperlink = null): self { diff --git a/src/PhpPresentation/Exception/DirectoryNotFoundException.php b/src/PhpPresentation/Exception/DirectoryNotFoundException.php new file mode 100644 index 000000000..bfd444c44 --- /dev/null +++ b/src/PhpPresentation/Exception/DirectoryNotFoundException.php @@ -0,0 +1,32 @@ + $authorizedMimetypes + */ + public function __construct(string $expectedMimetype, array $authorizedMimetypes) + { + parent::__construct(sprintf( + 'The mime type %s is not found in autorized values (%s)', + $expectedMimetype, + implode(', ', $authorizedMimetypes) + )); + } +} diff --git a/src/PhpPresentation/Exception/UndefinedChartTypeException.php b/src/PhpPresentation/Exception/UndefinedChartTypeException.php new file mode 100644 index 000000000..5e77181a0 --- /dev/null +++ b/src/PhpPresentation/Exception/UndefinedChartTypeException.php @@ -0,0 +1,29 @@ + $pSource Optional source array to create HashTable from - * - * @throws \Exception */ public function __construct(array $pSource = []) { @@ -92,8 +90,6 @@ public function add(ComparableInterface $pSource): void * Remove HashTable item. * * @param ComparableInterface $pSource Item to remove - * - * @throws \Exception */ public function remove(ComparableInterface $pSource): void { diff --git a/src/PhpPresentation/IOFactory.php b/src/PhpPresentation/IOFactory.php index 5a34cd171..0d7f90b29 100644 --- a/src/PhpPresentation/IOFactory.php +++ b/src/PhpPresentation/IOFactory.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation; +use PhpOffice\PhpPresentation\Exception\InvalidClassException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\Reader\ReaderInterface; use PhpOffice\PhpPresentation\Writer\WriterInterface; use ReflectionClass; @@ -39,31 +41,28 @@ class IOFactory /** * Create writer. * - * @throws \Exception + * @param PhpPresentation $phpPresentation + * @param string $name */ public static function createWriter(PhpPresentation $phpPresentation, string $name = 'PowerPoint2007'): WriterInterface { - $class = 'PhpOffice\\PhpPresentation\\Writer\\' . $name; - - return self::loadClass($class, $name, 'writer', $phpPresentation); + return self::loadClass('PhpOffice\\PhpPresentation\\Writer\\' . $name, 'Writer', $phpPresentation); } /** * Create reader. * - * @throws \Exception + * @param string $name */ public static function createReader(string $name): ReaderInterface { - $class = 'PhpOffice\\PhpPresentation\\Reader\\' . $name; - - return self::loadClass($class, $name, 'reader'); + return self::loadClass('PhpOffice\\PhpPresentation\\Reader\\' . $name, 'Reader'); } /** - * Loads PhpPresentation from file using automatic \PhpOffice\PhpPresentation\Reader\ReaderInterface resolution. + * Loads PhpPresentation from file using automatic ReaderInterface resolution. * - * @throws \Exception + * @throws InvalidFileFormatException */ public static function load(string $pFilename): PhpPresentation { @@ -75,33 +74,41 @@ public static function load(string $pFilename): PhpPresentation } } - throw new \Exception("Could not automatically determine \PhpOffice\PhpPresentation\Reader\ReaderInterface for file."); + throw new InvalidFileFormatException( + $pFilename, + IOFactory::class, + 'Could not automatically determine the good ' . ReaderInterface::class + ); } /** - * Load class. + * Load class * - * @return mixed + * @param string $class + * @param string $type + * @param PhpPresentation|null $phpPresentation * - * @throws \ReflectionException + * @return object + * + * @throws InvalidClassException */ - private static function loadClass(string $class, string $name, string $type, PhpPresentation $phpPresentation = null) + private static function loadClass(string $class, string $type, PhpPresentation $phpPresentation = null) { - if (class_exists($class) && self::isConcreteClass($class)) { - if (is_null($phpPresentation)) { - return new $class(); - } else { - return new $class($phpPresentation); - } - } else { - throw new \Exception('"' . $name . '" is not a valid ' . $type . '.'); + if (!class_exists($class)) { + throw new InvalidClassException($class, $type . ': The class doesn\'t exist'); } + if (!self::isConcreteClass($class)) { + throw new InvalidClassException($class, $type . ': The class is an abstract class or an interface'); + } + if (is_null($phpPresentation)) { + return new $class(); + } + + return new $class($phpPresentation); } /** * Is it a concrete class? - * - * @throws \ReflectionException */ private static function isConcreteClass(string $class): bool { diff --git a/src/PhpPresentation/PhpPresentation.php b/src/PhpPresentation/PhpPresentation.php index 4e3c4665d..e2db811f5 100644 --- a/src/PhpPresentation/PhpPresentation.php +++ b/src/PhpPresentation/PhpPresentation.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation; use ArrayObject; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Slide\Iterator; use PhpOffice\PhpPresentation\Slide\SlideMaster; @@ -155,8 +156,6 @@ public function getActiveSlide(): Slide /** * Create slide and add it to this presentation. - * - * @throws \Exception */ public function createSlide(): Slide { @@ -168,8 +167,6 @@ public function createSlide(): Slide /** * Add slide. - * - * @throws \Exception */ public function addSlide(Slide $slide): Slide { @@ -183,12 +180,12 @@ public function addSlide(Slide $slide): Slide * * @param int $index Slide index * - * @throws \Exception + * @throws OutOfBoundsException */ public function removeSlideByIndex(int $index = 0): self { if ($index > count($this->slideCollection) - 1) { - throw new \Exception('Slide index is out of bounds.'); + throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index); } array_splice($this->slideCollection, $index, 1); @@ -200,12 +197,12 @@ public function removeSlideByIndex(int $index = 0): self * * @param int $index Slide index * - * @throws \Exception + * @throws OutOfBoundsException */ public function getSlide(int $index = 0): Slide { if ($index > count($this->slideCollection) - 1) { - throw new \Exception('Slide index is out of bounds.'); + throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index); } return $this->slideCollection[$index]; @@ -223,8 +220,6 @@ public function getAllSlides(): array /** * Get index for slide. - * - * @throws \Exception */ public function getIndex(Slide\AbstractSlide $slide): ?int { @@ -263,12 +258,12 @@ public function getActiveSlideIndex(): int * * @param int $index Active slide index * - * @throws \Exception + * @throws OutOfBoundsException */ public function setActiveSlideIndex(int $index = 0): Slide { if ($index > count($this->slideCollection) - 1) { - throw new \Exception('Active slide index is out of bounds.'); + throw new OutOfBoundsException(0, count($this->slideCollection) - 1, $index); } $this->activeSlideIndex = $index; @@ -279,8 +274,6 @@ public function setActiveSlideIndex(int $index = 0): Slide * Add external slide. * * @param Slide $slide External slide to add - * - * @throws \Exception */ public function addExternalSlide(Slide $slide): Slide { @@ -301,8 +294,6 @@ public function getSlideIterator(): Iterator /** * Create a masterslide and add it to this presentation. - * - * @throws \Exception */ public function createMasterSlide(): SlideMaster { @@ -314,8 +305,6 @@ public function createMasterSlide(): SlideMaster /** * Add masterslide. - * - * @throws \Exception */ public function addMasterSlide(SlideMaster $slide): SlideMaster { @@ -326,8 +315,6 @@ public function addMasterSlide(SlideMaster $slide): SlideMaster /** * Copy presentation (!= clone!). - * - * @throws \Exception */ public function copy(): PhpPresentation { diff --git a/src/PhpPresentation/Reader/ODPresentation.php b/src/PhpPresentation/Reader/ODPresentation.php index 0ba6288c4..25a6d1cbb 100644 --- a/src/PhpPresentation/Reader/ODPresentation.php +++ b/src/PhpPresentation/Reader/ODPresentation.php @@ -25,6 +25,8 @@ use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\Common\XMLReader; use PhpOffice\PhpPresentation\DocumentProperties; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\PresentationProperties; use PhpOffice\PhpPresentation\Shape\Drawing\Base64; @@ -76,8 +78,6 @@ class ODPresentation implements ReaderInterface /** * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? - * - * @throws \Exception */ public function canRead(string $pFilename): bool { @@ -87,13 +87,13 @@ public function canRead(string $pFilename): bool /** * Does a file support UnserializePhpPresentation ? * - * @throws \Exception + * @throws FileNotFoundException */ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): bool { // Check if file exists if (!file_exists($pFilename)) { - throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.'); + throw new FileNotFoundException($pFilename); } $oZip = new ZipArchive(); @@ -112,13 +112,13 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): /** * Loads PhpPresentation Serialized file. * - * @throws \Exception + * @throws InvalidFileFormatException */ public function load(string $pFilename): PhpPresentation { // Unserialize... First make sure the file supports it! if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { - throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\ODPresentation: " . $pFilename . '.'); + throw new InvalidFileFormatException($pFilename, ODPresentation::class); } return $this->loadFile($pFilename); @@ -129,9 +129,7 @@ public function load(string $pFilename): PhpPresentation * * @param string $pFilename * - * @return \PhpOffice\PhpPresentation\PhpPresentation - * - * @throws \Exception + * @return PhpPresentation */ protected function loadFile($pFilename) { @@ -159,7 +157,7 @@ protected function loadFile($pFilename) } /** - * Read Document Properties. + * Read Document Properties */ protected function loadDocumentProperties(): void { @@ -219,7 +217,7 @@ protected function loadDocumentProperties(): void } /** - * Extract all slides. + * Extract all slides */ protected function loadSlides(): void { @@ -246,13 +244,9 @@ protected function loadPresentationProperties(): void } /** - * Extract style. - * - * @return bool - * - * @throws \Exception + * Extract style */ - protected function loadStyle(DOMElement $nodeStyle) + protected function loadStyle(DOMElement $nodeStyle): bool { $keyStyle = $nodeStyle->getAttribute('style:name'); @@ -493,9 +487,7 @@ protected function loadStyle(DOMElement $nodeStyle) } /** - * Read Slide. - * - * @throws \Exception + * Read Slide */ protected function loadSlide(DOMElement $nodeSlide): bool { @@ -528,9 +520,7 @@ protected function loadSlide(DOMElement $nodeSlide): bool } /** - * Read Shape Drawing. - * - * @throws \Exception + * Read Shape Drawing */ protected function loadShapeDrawing(DOMElement $oNodeFrame): void { @@ -587,9 +577,7 @@ protected function loadShapeDrawing(DOMElement $oNodeFrame): void } /** - * Read Shape RichText. - * - * @throws \Exception + * Read Shape RichText */ protected function loadShapeRichText(DOMElement $oNodeFrame): void { @@ -620,9 +608,7 @@ protected function loadShapeRichText(DOMElement $oNodeFrame): void } /** - * Read Paragraph. - * - * @throws \Exception + * Read Paragraph */ protected function readParagraph(RichText $oShape, DOMElement $oNodeParent): void { @@ -660,9 +646,7 @@ protected function readParagraph(RichText $oShape, DOMElement $oNodeParent): voi } /** - * Read Paragraph Item. - * - * @throws \Exception + * Read Paragraph Item */ protected function readParagraphItem(Paragraph $oParagraph, DOMElement $oNodeParent): void { @@ -689,9 +673,7 @@ protected function readParagraphItem(Paragraph $oParagraph, DOMElement $oNodePar } /** - * Read List. - * - * @throws \Exception + * Read List */ protected function readList(RichText $oShape, DOMElement $oNodeParent): void { @@ -710,9 +692,7 @@ protected function readList(RichText $oShape, DOMElement $oNodeParent): void } /** - * Read List Item. - * - * @throws \Exception + * Read List Item */ protected function readListItem(RichText $oShape, DOMElement $oNodeParent, DOMElement $oNodeParagraph): void { diff --git a/src/PhpPresentation/Reader/PowerPoint2007.php b/src/PhpPresentation/Reader/PowerPoint2007.php index 3b19e7cb6..ad0deb976 100644 --- a/src/PhpPresentation/Reader/PowerPoint2007.php +++ b/src/PhpPresentation/Reader/PowerPoint2007.php @@ -28,6 +28,9 @@ use PhpOffice\Common\XMLReader; use PhpOffice\PhpPresentation\DocumentLayout; use PhpOffice\PhpPresentation\DocumentProperties; +use PhpOffice\PhpPresentation\Exception\FeatureNotImplementedException; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\PresentationProperties; use PhpOffice\PhpPresentation\Shape\Drawing\Base64; @@ -87,8 +90,6 @@ class PowerPoint2007 implements ReaderInterface /** * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? - * - * @throws \Exception */ public function canRead(string $pFilename): bool { @@ -98,13 +99,13 @@ public function canRead(string $pFilename): bool /** * Does a file support UnserializePhpPresentation ? * - * @throws \Exception + * @throws FileNotFoundException */ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): bool { // Check if file exists if (!file_exists($pFilename)) { - throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.'); + throw new FileNotFoundException($pFilename); } $oZip = new ZipArchive(); @@ -123,13 +124,13 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): /** * Loads PhpPresentation Serialized file. * - * @throws \Exception + * @throws InvalidFileFormatException */ public function load(string $pFilename): PhpPresentation { // Unserialize... First make sure the file supports it! if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { - throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\PowerPoint2007: " . $pFilename . '.'); + throw new InvalidFileFormatException($pFilename, PowerPoint2007::class); } return $this->loadFile($pFilename); @@ -137,8 +138,6 @@ public function load(string $pFilename): PhpPresentation /** * Load PhpPresentation Serialized file. - * - * @throws \Exception */ protected function loadFile(string $pFilename): PhpPresentation { @@ -341,9 +340,7 @@ protected function loadViewProperties(string $sPart): void } /** - * Extract all slides. - * - * @throws \Exception + * Extract all slides */ protected function loadSlides(string $sPart): void { @@ -379,9 +376,7 @@ protected function loadSlides(string $sPart): void } /** - * Extract all MasterSlides. - * - * @throws \Exception + * Extract all MasterSlides */ protected function loadMasterSlides(XMLReader $xmlReader, string $fileRels): void { @@ -405,9 +400,7 @@ protected function loadMasterSlides(XMLReader $xmlReader, string $fileRels): voi } /** - * Extract data from slide. - * - * @throws \Exception + * Extract data from slide */ protected function loadSlide(string $sPart, string $baseFile): void { @@ -492,9 +485,6 @@ protected function loadSlide(string $sPart, string $baseFile): void } } - /** - * @throws \Exception - */ protected function loadMasterSlide(string $sPart, string $baseFile): void { $xmlReader = new XMLReader(); @@ -631,9 +621,6 @@ protected function loadMasterSlide(string $sPart, string $baseFile): void } } - /** - * @throws \Exception - */ protected function loadLayoutSlide(string $sPart, string $baseFile, SlideMaster $oSlideMaster): ?SlideLayout { $xmlReader = new XMLReader(); @@ -700,9 +687,6 @@ protected function loadTheme(string $sPart, SlideMaster $oSlideMaster): void } } - /** - * @throws \Exception - */ protected function loadSlideBackground(XMLReader $xmlReader, DOMElement $oElement, AbstractSlide $oSlide): void { // Background color @@ -759,9 +743,6 @@ protected function loadSlideBackground(XMLReader $xmlReader, DOMElement $oElemen } } - /** - * @throws \Exception - */ protected function loadSlideNote(string $baseFile, Slide $oSlide): void { $sPart = $this->oZip->getFromName('ppt/notesSlides/' . $baseFile); @@ -775,9 +756,6 @@ protected function loadSlideNote(string $baseFile, Slide $oSlide): void } } - /** - * @throws \Exception - */ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void { // Core @@ -907,9 +885,6 @@ protected function loadShapeDrawing(XMLReader $document, DOMElement $node, Abstr $oSlide->addShape($oShape); } - /** - * @throws \Exception - */ protected function loadShapeRichText(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void { if (!$document->elementExists('p:txBody/a:p/a:r', $node)) { @@ -968,9 +943,6 @@ protected function loadShapeRichText(XMLReader $document, DOMElement $node, Abst } } - /** - * @throws \Exception - */ protected function loadShapeTable(XMLReader $document, DOMElement $node, AbstractSlide $oSlide): void { $this->fileRels = $oSlide->getRelsIndex(); @@ -1021,8 +993,9 @@ protected function loadShapeTable(XMLReader $document, DOMElement $node, Abstrac if (!($oElementRow instanceof DOMElement)) { continue; } - $oRow = $oShape->getRow($keyRow, true); - if (is_null($oRow)) { + if ($oShape->hasRow($keyRow)) { + $oRow = $oShape->getRow($keyRow); + } else { $oRow = $oShape->createRow(); } if ($oElementRow->hasAttribute('h')) { @@ -1110,8 +1083,6 @@ protected function loadShapeTable(XMLReader $document, DOMElement $node, Abstrac /** * @param Cell|RichText $oShape - * - * @throws \Exception */ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oShape): void { @@ -1278,9 +1249,6 @@ protected function loadParagraph(XMLReader $document, DOMElement $oElement, $oSh } } - /** - * @throws \Exception - */ protected function loadStyleBorder(XMLReader $xmlReader, DOMElement $oElement, Border $oBorder): void { if ($oElement->hasAttribute('w')) { @@ -1319,9 +1287,6 @@ protected function loadStyleColor(XMLReader $xmlReader, DOMElement $oElement): C return $oColor; } - /** - * @throws \Exception - */ protected function loadStyleFill(XMLReader $xmlReader, DOMElement $oElement): ?Fill { // Gradient fill @@ -1389,7 +1354,7 @@ protected function loadRels(string $fileRels): void * @param AbstractSlide|Note $oSlide * @param DOMNodeList $oElements * - * @throws \Exception + * @throws FeatureNotImplementedException * * @internal param $baseFile */ @@ -1410,7 +1375,7 @@ protected function loadSlideShapes($oSlide, DOMNodeList $oElements, XMLReader $x $this->loadShapeRichText($xmlReader, $oNode, $oSlide); break; default: - //var_export($oNode->tagName); + //throw new FeatureNotImplementedException(); } } } diff --git a/src/PhpPresentation/Reader/PowerPoint97.php b/src/PhpPresentation/Reader/PowerPoint97.php index 9c086f69f..acd074ca2 100644 --- a/src/PhpPresentation/Reader/PowerPoint97.php +++ b/src/PhpPresentation/Reader/PowerPoint97.php @@ -20,9 +20,13 @@ namespace PhpOffice\PhpPresentation\Reader; +use Exception; use PhpOffice\Common\Microsoft\OLERead; use PhpOffice\Common\Text; use PhpOffice\PhpPresentation\AbstractShape; +use PhpOffice\PhpPresentation\Exception\FeatureNotImplementedException; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape; use PhpOffice\PhpPresentation\Shape\Drawing; @@ -384,10 +388,13 @@ class PowerPoint97 implements ReaderInterface */ private $currentNote; + /** + * @var string|null + */ + private $filename; + /** * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? - * - * @throws \Exception */ public function canRead(string $pFilename): bool { @@ -397,13 +404,13 @@ public function canRead(string $pFilename): bool /** * Does a file support UnserializePhpPresentation ? * - * @throws \Exception + * @throws FileNotFoundException */ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): bool { // Check if file exists if (!file_exists($pFilename)) { - throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.'); + throw new FileNotFoundException($pFilename); } try { @@ -413,7 +420,7 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): $ole->read($pFilename); return true; - } catch (\Exception $e) { + } catch (Exception $e) { return false; } } @@ -421,30 +428,30 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): /** * Loads PhpPresentation Serialized file. * - * @throws \Exception + * @throws InvalidFileFormatException */ public function load(string $pFilename): PhpPresentation { // Unserialize... First make sure the file supports it! if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { - throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\PowerPoint97: " . $pFilename . '.'); + throw new InvalidFileFormatException($pFilename, PowerPoint97::class); } - return $this->loadFile($pFilename); + $this->filename = $pFilename; + + return $this->loadFile(); } /** - * Load PhpPresentation Serialized file. - * - * @throws \Exception + * Load PhpPresentation Serialized file */ - private function loadFile(string $pFilename): PhpPresentation + private function loadFile(): PhpPresentation { $this->oPhpPresentation = new PhpPresentation(); $this->oPhpPresentation->removeSlideByIndex(); // Read OLE Blocks - $this->loadOLE($pFilename); + $this->loadOLE(); // Read pictures in the Pictures Stream $this->loadPicturesStream(); // Read information in the Current User Stream @@ -456,15 +463,13 @@ private function loadFile(string $pFilename): PhpPresentation } /** - * Read OLE Part. - * - * @throws \Exception + * Read OLE Part */ - private function loadOLE(string $pFilename): void + private function loadOLE(): void { // OLE reader $oOLE = new OLERead(); - $oOLE->read($pFilename); + $oOLE->read($this->filename); // PowerPoint Document Stream $this->streamPowerpointDocument = $oOLE->getStream($oOLE->powerpointDocument); @@ -485,6 +490,8 @@ private function loadOLE(string $pFilename): void /** * Stream Pictures. * + * @throws FeatureNotImplementedException + * * @see http://msdn.microsoft.com/en-us/library/dd920746(v=office.12).aspx */ private function loadPicturesStream(): void @@ -500,7 +507,7 @@ private function loadPicturesStream(): void //@link : http://msdn.microsoft.com/en-us/library/dd950560(v=office.12).aspx if (0xF007 == $arrayRH['recType']) { // OfficeArtFBSE - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if ($arrayRH['recType'] >= 0xF018 && $arrayRH['recType'] <= 0xF117) { $arrayRecord = $this->readRecordOfficeArtBlip($stream, $pos - 8); @@ -517,6 +524,9 @@ private function loadPicturesStream(): void /** * Stream Current User. * + * @throws FeatureNotImplementedException + * @throws InvalidFileFormatException + * * @see http://msdn.microsoft.com/en-us/library/dd908567(v=office.12).aspx */ private function loadCurrentUserStream(): void @@ -530,21 +540,22 @@ private function loadCurrentUserStream(): void $rHeader = $this->loadRecordHeader($this->streamCurrentUser, $pos); $pos += 8; if (0x0 != $rHeader['recVer'] || 0x000 != $rHeader['recInstance'] || self::RT_CURRENTUSERATOM != $rHeader['recType']) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > RecordHeader).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > RecordHeader'); } // Size $size = self::getInt4d($this->streamCurrentUser, $pos); $pos += 4; if (0x00000014 != $size) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > Size).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > Size'); } // headerToken $headerToken = self::getInt4d($this->streamCurrentUser, $pos); $pos += 4; if (0xF3D1C4DF == $headerToken && 0xE391C05F != $headerToken) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ') : Encrypted file'); + // Encrypted file + throw new FeatureNotImplementedException(); } // offsetToCurrentEdit @@ -555,28 +566,28 @@ private function loadCurrentUserStream(): void $lenUserName = self::getInt2d($this->streamCurrentUser, $pos); $pos += 2; if ($lenUserName > 255) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > lenUserName).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > lenUserName'); } // docFileVersion $docFileVersion = self::getInt2d($this->streamCurrentUser, $pos); $pos += 2; if (0x03F4 != $docFileVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > docFileVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > docFileVersion'); } // majorVersion $majorVersion = self::getInt1d($this->streamCurrentUser, $pos); ++$pos; if (0x03 != $majorVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > majorVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > majorVersion'); } // minorVersion $minorVersion = self::getInt1d($this->streamCurrentUser, $pos); ++$pos; if (0x00 != $minorVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > minorVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > minorVersion'); } // unused @@ -598,7 +609,7 @@ private function loadCurrentUserStream(): void $relVersion = self::getInt4d($this->streamCurrentUser, $pos); $pos += 4; if (0x00000008 != $relVersion && 0x00000009 != $relVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : CurrentUserAtom > relVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : CurrentUserAtom > relVersion'); } // unicodeUserName @@ -642,7 +653,6 @@ private function loadPowerpointDocumentStream(): void $this->readRecordSlideContainer($this->streamPowerpointDocument, $pos); break; default: - // throw new \Exception('Feature not implemented : l.'.__LINE__.'('.dechex($rh['recType']).')'); break; } } @@ -707,7 +717,7 @@ public static function getInt4d(string $data, int $pos): int * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd772900(v=office.12).aspx */ @@ -723,7 +733,7 @@ private function readRecordAnimationInfoContainer(string $stream, int $pos): arr $arrayReturn['length'] += 8; // animationAtom // animationSound - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } return $arrayReturn; @@ -732,7 +742,8 @@ private function readRecordAnimationInfoContainer(string $stream, int $pos): arr /** * A container record that specifies information about the document. * - * @throws \Exception + * @throws FeatureNotImplementedException + * @throws InvalidFileFormatException * * @see http://msdn.microsoft.com/en-us/library/dd947357(v=office.12).aspx */ @@ -741,7 +752,7 @@ private function readRecordDocumentContainer(string $stream, int $pos): void $documentAtom = $this->loadRecordHeader($stream, $pos); $pos += 8; if (0x1 != $documentAtom['recVer'] || 0x000 != $documentAtom['recInstance'] || self::RT_DOCUMENTATOM != $documentAtom['recType']) { - throw new \Exception('File PowerPoint 97 in error (Location : RTDocument > DocumentAtom).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : RTDocument > DocumentAtom'); } $pos += $documentAtom['recLen']; @@ -751,7 +762,7 @@ private function readRecordDocumentContainer(string $stream, int $pos): void // exObjListAtom > rh $exObjListAtom = $this->loadRecordHeader($stream, $pos); if (0x0 != $exObjListAtom['recVer'] || 0x000 != $exObjListAtom['recInstance'] || self::RT_EXTERNALOBJECTLISTATOM != $exObjListAtom['recType'] || 0x00000004 != $exObjListAtom['recLen']) { - throw new \Exception('File PowerPoint 97 in error (Location : RTDocument > DocumentAtom > exObjList > exObjListAtom).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : RTDocument > DocumentAtom > exObjList > exObjListAtom'); } $pos += 8; // exObjListAtom > exObjIdSeed @@ -768,7 +779,7 @@ private function readRecordDocumentContainer(string $stream, int $pos): void // exHyperlinkAtom > rh $exHyperlinkAtom = $this->loadRecordHeader($stream, $pos); if (0x0 != $exHyperlinkAtom['recVer'] || 0x000 != $exHyperlinkAtom['recInstance'] || self::RT_EXTERNALHYPERLINKATOM != $exHyperlinkAtom['recType'] || 0x00000004 != $exObjListAtom['recLen']) { - throw new \Exception('File PowerPoint 97 in error (Location : RTDocument > DocumentAtom > exObjList > rgChildRec > RT_ExternalHyperlink).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : RTDocument > DocumentAtom > exObjList > rgChildRec > RT_ExternalHyperlink'); } $pos += 8; $exObjList['recLen'] -= 8; @@ -819,7 +830,8 @@ private function readRecordDocumentContainer(string $stream, int $pos): void } break; default: - throw new \Exception('Feature not implemented (l.' . __LINE__ . ' : ' . dechex((int) $childRec['recType']) . ')'); + // var_dump(dechex((int) $childRec['recType'])); + throw new FeatureNotImplementedException(); } } while ($exObjList['recLen'] > 0); } @@ -844,7 +856,7 @@ private function readRecordDocumentContainer(string $stream, int $pos): void $pos += 8; $fontCollection['recLen'] -= 8; if (0x0 != $fontEntityAtom['recVer'] || $fontEntityAtom['recInstance'] > 128 || self::RT_FONTENTITYATOM != $fontEntityAtom['recType']) { - throw new \Exception('File PowerPoint 97 in error (Location : RTDocument > RT_Environment > RT_FontCollection > RT_FontEntityAtom).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : RTDocument > RT_Environment > RT_FontCollection > RT_FontEntityAtom'); } $string = ''; for ($inc = 0; $inc < 32; ++$inc) { @@ -1020,8 +1032,6 @@ private function readRecordDocumentContainer(string $stream, int $pos): void * * @return array * - * @throws \Exception - * * @see https://msdn.microsoft.com/en-us/library/dd923801(v=office.12).aspx */ private function readRecordDrawingContainer(string $stream, int $pos): array @@ -1188,7 +1198,7 @@ private function readRecordMouseClickInteractiveInfoContainer(string $stream, in * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd925811(v=office.12).aspx */ @@ -1204,7 +1214,7 @@ private function readRecordMouseOverInteractiveInfoContainer(string $stream, int $arrayReturn['length'] += 8; // interactiveInfoAtom // macroNameAtom - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } return $arrayReturn; @@ -1215,7 +1225,7 @@ private function readRecordMouseOverInteractiveInfoContainer(string $stream, int * * @return array{'length': int, 'picture': null|string} * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd910081(v=office.12).aspx */ @@ -1250,7 +1260,8 @@ private function readRecordOfficeArtBlip(string $stream, int $pos): array $arrayReturn['length'] += $data['recLen']; break; default: - throw new \Exception('Feature not implemented (l.' . __LINE__ . ' : ' . dechex((int) $data['recType']) . ')'); + // var_dump(dechex((int) $data['recType'])) + throw new FeatureNotImplementedException(); } } @@ -1293,7 +1304,7 @@ private function readRecordOfficeArtChildAnchor(string $stream, int $pos) * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd922797(v=office.12).aspx */ @@ -1321,7 +1332,8 @@ private function readRecordOfficeArtClientAnchor(string $stream, int $pos) $pos += 8; break; case 0x00000010: - throw new \Exception('PowerPoint97 Reader : record OfficeArtClientAnchor (0x00000010)'); + // record OfficeArtClientAnchor (0x00000010) + throw new FeatureNotImplementedException(); } } @@ -1333,7 +1345,7 @@ private function readRecordOfficeArtClientAnchor(string $stream, int $pos) * * @return array{'length': int, 'text': string, 'numParts': int, 'numTexts': int, 'hyperlink': array>, 'part': array} * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd910958(v=office.12).aspx */ @@ -1444,7 +1456,7 @@ private function readRecordOfficeArtClientTextbox(string $stream, int $pos) $arrayReturn['length'] += 4; } if (0x0001 == $rhChild['recInstance']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } break; case self::RT_TEXTSPECIALINFOATOM: @@ -1470,7 +1482,6 @@ private function readRecordOfficeArtClientTextbox(string $stream, int $pos) default: $arrayReturn['length'] += 8; $arrayReturn['length'] += $rhChild['recLen']; - // throw new \Exception('Feature not implemented (l.'.__LINE__.' : 0x'.dechex($rhChild['recType']).')'); } } while (($data['recLen'] - $arrayReturn['length']) > 0); } @@ -1483,7 +1494,7 @@ private function readRecordOfficeArtClientTextbox(string $stream, int $pos) * * @return array{'length': int, 'shape': null|AbstractShape} * - * @throws \Exception + * @throws InvalidFileFormatException * * @see https://msdn.microsoft.com/en-us/library/dd943794(v=office.12).aspx */ @@ -1505,7 +1516,7 @@ private function readRecordOfficeArtSpContainer(string $stream, int $pos) // shapeProp $shapeProp = $this->readRecordOfficeArtFSP($stream, $pos + $arrayReturn['length']); if (0 == $shapeProp['length']) { - throw new \Exception('PowerPoint97 Reader : record OfficeArtFSP'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class); } $arrayReturn['length'] += $shapeProp['length']; @@ -1753,7 +1764,7 @@ private function readRecordOfficeArtSpContainer(string $stream, int $pos) * * @return array * - * @throws \Exception + * @throws InvalidFileFormatException * * @see : https://msdn.microsoft.com/en-us/library/dd910416(v=office.12).aspx */ @@ -1770,7 +1781,7 @@ private function readRecordOfficeArtSpgrContainer($stream, $pos, $bInGroup = fal do { $rhFileBlock = $this->loadRecordHeader($stream, $pos + $arrayReturn['length']); if (!(0xF == $rhFileBlock['recVer'] && 0x0000 == $rhFileBlock['recInstance'] && (0xF003 == $rhFileBlock['recType'] || 0xF004 == $rhFileBlock['recType']))) { - throw new \Exception('PowerPoint97 Reader : readRecordOfficeArtSpgrContainer.'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class); } switch ($rhFileBlock['recType']) { @@ -1828,7 +1839,7 @@ private function readRecordOfficeArtSpgrContainer($stream, $pos, $bInGroup = fal * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd950206(v=office.12).aspx */ @@ -1882,7 +1893,8 @@ private function readRecordOfficeArtTertiaryFOPT(string $stream, int $pos) //@link : https://msdn.microsoft.com/en-us/library/dd951605(v=office.12).aspx break; default: - throw new \Exception('Feature not implemented (l.' . __LINE__ . ' : 0x' . dechex($opt['opid']) . ')'); + // var_dump('0x' . dechex($opt['opid'])); + throw new FeatureNotImplementedException(); } } } @@ -1895,8 +1907,6 @@ private function readRecordOfficeArtTertiaryFOPT(string $stream, int $pos) * * @return array * - * @throws \Exception - * * @see : https://msdn.microsoft.com/en-us/library/dd924455(v=office.12).aspx */ private function readRecordOfficeArtDgContainer(string $stream, int $pos): array @@ -2232,7 +2242,6 @@ private function readRecordOfficeArtFOPT(string $stream, int $pos): array //@link : http://msdn.microsoft.com/en-us/library/dd949807(v=office.12).aspx break; default: - // throw new \Exception('Feature not implemented (l.'.__LINE__.' : 0x'.dechex($opt['opid']).')'); } } if ($data['recLen'] > 0) { @@ -2353,7 +2362,7 @@ private function readRecordOfficeArtSecondaryFOPT(string $stream, int $pos): arr * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see : https://msdn.microsoft.com/en-us/library/dd950927(v=office.12).aspx */ @@ -2423,7 +2432,8 @@ private function readRecordOfficeArtClientData(string $stream, int $pos): array $arrayReturn['length'] += $dataRG['length']; break; default: - throw new \Exception('Feature not implemented (l.' . __LINE__ . ' : 0x' . dechex($dataHeaderRG['recType']) . ')'); + // var_dump('0x' . dechex($dataHeaderRG['recType'])); + throw new FeatureNotImplementedException(); } } } while (in_array($dataHeaderRG['recType'], $array)); @@ -2437,14 +2447,14 @@ private function readRecordOfficeArtClientData(string $stream, int $pos): array * * @see http://msdn.microsoft.com/en-us/library/dd952680(v=office.12).aspx * - * @throws \Exception + * @throws InvalidFileFormatException */ private function readRecordPersistDirectoryAtom(string $stream, int $pos): void { $rHeader = $this->loadRecordHeader($stream, $pos); $pos += 8; if (0x0 != $rHeader['recVer'] || 0x000 != $rHeader['recInstance'] || self::RT_PERSISTDIRECTORYATOM != $rHeader['recType']) { - throw new \Exception('File PowerPoint 97 in error (Location : PersistDirectoryAtom > RecordHeader).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : PersistDirectoryAtom > RecordHeader'); } // rgPersistDirEntry // @link : http://msdn.microsoft.com/en-us/library/dd947347(v=office.12).aspx @@ -2686,7 +2696,7 @@ private function readRecordShapeProgBinaryTagContainer(string $stream, int $pos) * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd911266(v=office.12).aspx */ @@ -2711,7 +2721,8 @@ private function readRecordShapeProgTagsContainer(string $stream, int $pos): arr break; //case self::RT_PROGSTRINGTAG: default: - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + // var_dump('0x' . dechex($dataHeaderRG['recType'])); + throw new FeatureNotImplementedException(); } } while ($length < $data['recLen']); // Datas @@ -2763,7 +2774,7 @@ private function readRecordSlideAtom(string $stream, int $pos): array /** * A container record that specifies a presentation slide or title master slide. * - * @throws \Exception + * @throws InvalidFileFormatException * * @see http://msdn.microsoft.com/en-us/library/dd946323(v=office.12).aspx */ @@ -2776,7 +2787,7 @@ private function readRecordSlideContainer(string $stream, int $pos): void // *** slideAtom (32 bytes) $slideAtom = $this->readRecordSlideAtom($stream, $pos); if (0 == $slideAtom['length']) { - throw new \Exception('PowerPoint97 Reader : record SlideAtom'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class); } $pos += $slideAtom['length']; @@ -2799,7 +2810,8 @@ private function readRecordSlideContainer(string $stream, int $pos): void // *** slideSchemeColorSchemeAtom (40 bytes) $slideSchemeColorAtom = $this->readRecordSlideSchemeColorSchemeAtom($stream, $pos); if (0 == $slideSchemeColorAtom['length']) { - throw new \Exception('PowerPoint97 Reader : record SlideSchemeColorSchemeAtom'); + // Record SlideSchemeColorSchemeAtom + throw new InvalidFileFormatException($this->filename, PowerPoint97::class); } $pos += $slideSchemeColorAtom['length']; @@ -2953,14 +2965,14 @@ private function readRecordSlideShowSlideInfoAtom(string $stream, int $pos): arr * * @see http://msdn.microsoft.com/en-us/library/dd945746(v=office.12).aspx * - * @throws \Exception + * @throws InvalidFileFormatException */ private function readRecordUserEditAtom(string $stream, int $pos): void { $rHeader = $this->loadRecordHeader($stream, $pos); $pos += 8; if (0x0 != $rHeader['recVer'] || 0x000 != $rHeader['recInstance'] || self::RT_USEREDITATOM != $rHeader['recType'] || (0x0000001C != $rHeader['recLen'] && 0x00000020 != $rHeader['recLen'])) { - throw new \Exception('File PowerPoint 97 in error (Location : UserEditAtom > RecordHeader).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : UserEditAtom > RecordHeader'); } // lastSlideIdRef @@ -2972,14 +2984,14 @@ private function readRecordUserEditAtom(string $stream, int $pos): void $minorVersion = self::getInt1d($stream, $pos); ++$pos; if (0x00 != $minorVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : UserEditAtom > minorVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : UserEditAtom > minorVersion'); } // majorVersion $majorVersion = self::getInt1d($stream, $pos); ++$pos; if (0x03 != $majorVersion) { - throw new \Exception('File PowerPoint 97 in error (Location : UserEditAtom > majorVersion).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : UserEditAtom > majorVersion'); } // offsetLastEdit @@ -2992,7 +3004,7 @@ private function readRecordUserEditAtom(string $stream, int $pos): void $docPersistIdRef = self::getInt4d($stream, $pos); $pos += 4; if (0x00000001 != $docPersistIdRef) { - throw new \Exception('File PowerPoint 97 in error (Location : UserEditAtom > docPersistIdRef).'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : UserEditAtom > docPersistIdRef'); } // persistIdSeed @@ -3008,7 +3020,7 @@ private function readRecordUserEditAtom(string $stream, int $pos): void * * @return array{'length': int, 'strLenRT': int, 'partLength': int, 'bold': bool, 'italic': bool, 'underline': bool, 'fontName': string, 'fontSize': int, 'color': Color} * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd945870(v=office.12).aspx */ @@ -3114,7 +3126,7 @@ private function readStructureTextCFRun(string $stream, int $pos, int $strLenRT) } } if (1 == $masksData['position']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } return $arrayReturn; @@ -3125,7 +3137,7 @@ private function readStructureTextCFRun(string $stream, int $pos, int $strLenRT) * * @return array{'length': int, 'strLenRT': int, 'alignH': string, 'bulletChar': string, 'leftMargin': int, 'indent': int} * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd923535(v=office.12).aspx */ @@ -3270,7 +3282,7 @@ private function readStructureTextPFRun(string $stream, int $pos, int $strLenRT) $arrayReturn['length'] += 2; } if (1 == $masksData['tabStops']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if (1 == $masksData['fontAlign']) { // $data = self::getInt2d($stream, $pos + $arrayReturn['length']); @@ -3281,7 +3293,7 @@ private function readStructureTextPFRun(string $stream, int $pos, int $strLenRT) $arrayReturn['length'] += 2; } if (1 == $masksData['textDirection']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } return $arrayReturn; @@ -3292,7 +3304,7 @@ private function readStructureTextPFRun(string $stream, int $pos, int $strLenRT) * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd909603(v=office.12).aspx */ @@ -3337,13 +3349,13 @@ private function readStructureTextSIRun(string $stream, int $pos, int $strLenRT) $arrayReturn['length'] += 2; } if (1 == $masksData['fBidi']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if (1 == $masksData['fPp10ext']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if (1 == $masksData['smartTag']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } return $arrayReturn; @@ -3354,7 +3366,7 @@ private function readStructureTextSIRun(string $stream, int $pos, int $strLenRT) * * @return array * - * @throws \Exception + * @throws FeatureNotImplementedException * * @see https://msdn.microsoft.com/en-us/library/dd922749(v=office.12).aspx */ @@ -3383,10 +3395,10 @@ private function readStructureTextRuler(string $stream, int $pos): array $masksData['fIndent5'] = ($data >> 12) & bindec('1'); if (1 == $masksData['fCLevels']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if (1 == $masksData['fDefaultTabSize']) { - throw new \Exception('Feature not implemented (l.' . __LINE__ . ')'); + throw new FeatureNotImplementedException(); } if (1 == $masksData['fTabStops']) { $count = self::getInt2d($stream, $pos + $arrayReturn['length']); @@ -3447,9 +3459,6 @@ private function readStructureTextRuler(string $stream, int $pos): array return $arrayReturn; } - /** - * @throws \Exception - */ private function readRecordNotesContainer(string $stream, int $pos): void { // notesAtom @@ -3469,7 +3478,7 @@ private function readRecordNotesContainer(string $stream, int $pos): void /** * @return array * - * @throws \Exception + * @throws InvalidFileFormatException */ private function readRecordNotesAtom(string $stream, int $pos): array { @@ -3479,7 +3488,7 @@ private function readRecordNotesAtom(string $stream, int $pos): array $data = $this->loadRecordHeader($stream, $pos); if (0x1 != $data['recVer'] || 0x000 != $data['recInstance'] || self::RT_NOTESATOM != $data['recType'] || 0x00000008 != $data['recLen']) { - throw new \Exception('File PowerPoint 97 in error (Location : NotesAtom > RecordHeader)'); + throw new InvalidFileFormatException($this->filename, PowerPoint97::class, 'Location : NotesAtom > RecordHeader)'); } // Record Header $arrayReturn['length'] += 8; diff --git a/src/PhpPresentation/Reader/ReaderInterface.php b/src/PhpPresentation/Reader/ReaderInterface.php index 41bc7c34f..d3e06d393 100644 --- a/src/PhpPresentation/Reader/ReaderInterface.php +++ b/src/PhpPresentation/Reader/ReaderInterface.php @@ -34,8 +34,6 @@ public function canRead(string $pFilename): bool; /** * Loads PhpPresentation from file. - * - * @throws \Exception */ public function load(string $pFilename): PhpPresentation; } diff --git a/src/PhpPresentation/Reader/Serialized.php b/src/PhpPresentation/Reader/Serialized.php index f0cb9fe6b..33de31b41 100644 --- a/src/PhpPresentation/Reader/Serialized.php +++ b/src/PhpPresentation/Reader/Serialized.php @@ -20,8 +20,9 @@ namespace PhpOffice\PhpPresentation\Reader; -use Exception; use PhpOffice\Common\File; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter; use PhpOffice\PhpPresentation\Shape\Drawing\File as DrawingFile; @@ -34,8 +35,6 @@ class Serialized implements ReaderInterface { /** * Can the current \PhpOffice\PhpPresentation\Reader\ReaderInterface read the file? - * - * @throws \Exception */ public function canRead(string $pFilename): bool { @@ -45,13 +44,13 @@ public function canRead(string $pFilename): bool /** * Does a file support UnserializePhpPresentation ? * - * @throws \Exception + * @throws FileNotFoundException */ - public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): bool + public function fileSupportsUnserializePhpPresentation(string $pFilename): bool { // Check if file exists if (!file_exists($pFilename)) { - throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.'); + throw new FileNotFoundException($pFilename); } // File exists, does it contain PhpPresentation.xml? @@ -61,18 +60,19 @@ public function fileSupportsUnserializePhpPresentation(string $pFilename = ''): /** * Loads PhpPresentation Serialized file. * - * @throws \Exception + * @throws FileNotFoundException + * @throws InvalidFileFormatException */ public function load(string $pFilename): PhpPresentation { // Check if file exists if (!file_exists($pFilename)) { - throw new \Exception('Could not open ' . $pFilename . ' for reading! File does not exist.'); + throw new FileNotFoundException($pFilename); } // Unserialize... First make sure the file supports it! if (!$this->fileSupportsUnserializePhpPresentation($pFilename)) { - throw new \Exception("Invalid file format for PhpOffice\PhpPresentation\Reader\Serialized: " . $pFilename . '.'); + throw new InvalidFileFormatException($pFilename, Serialized::class); } return $this->loadSerialized($pFilename); @@ -80,17 +80,19 @@ public function load(string $pFilename): PhpPresentation /** * Load PhpPresentation Serialized file. + * + * @throws InvalidFileFormatException */ private function loadSerialized(string $pFilename): PhpPresentation { $oArchive = new ZipArchive(); if (true !== $oArchive->open($pFilename)) { - throw new Exception(''); + throw new InvalidFileFormatException($pFilename, Serialized::class); } $xmlContent = $oArchive->getFromName('PhpPresentation.xml'); if (empty($xmlContent)) { - throw new Exception(sprintf('The file %s in the serialized file %s is malformed', 'PhpPresentation.xml', $pFilename)); + throw new InvalidFileFormatException($pFilename, Serialized::class, 'The file PhpPresentation.xml is malformed'); } $xmlData = simplexml_load_string($xmlContent); diff --git a/src/PhpPresentation/Shape/Chart/Legend.php b/src/PhpPresentation/Shape/Chart/Legend.php index 0a7a51131..6878f62a8 100644 --- a/src/PhpPresentation/Shape/Chart/Legend.php +++ b/src/PhpPresentation/Shape/Chart/Legend.php @@ -251,8 +251,6 @@ public function getFont(): ?Font * Set font. * * @param Font|null $pFont Font - * - * @throws \Exception */ public function setFont(Font $pFont = null): self { diff --git a/src/PhpPresentation/Shape/Chart/PlotArea.php b/src/PhpPresentation/Shape/Chart/PlotArea.php index f20a290c2..2eb97174f 100644 --- a/src/PhpPresentation/Shape/Chart/PlotArea.php +++ b/src/PhpPresentation/Shape/Chart/PlotArea.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException; use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractType; /** @@ -90,12 +91,12 @@ public function __clone() } /** - * @throws \Exception + * @throws UndefinedChartTypeException */ public function getType(): AbstractType { if (is_null($this->type)) { - throw new \Exception('Chart type has not been set.'); + throw new UndefinedChartTypeException(); } return $this->type; diff --git a/src/PhpPresentation/Shape/Chart/Title.php b/src/PhpPresentation/Shape/Chart/Title.php index ceef81b00..a03165724 100644 --- a/src/PhpPresentation/Shape/Chart/Title.php +++ b/src/PhpPresentation/Shape/Chart/Title.php @@ -235,8 +235,6 @@ public function getFont(): ?Font * Set font. * * @param Font|null $pFont Font - * - * @throws \Exception */ public function setFont(Font $pFont = null): self { diff --git a/src/PhpPresentation/Shape/Drawing/Base64.php b/src/PhpPresentation/Shape/Drawing/Base64.php index 889bc73e3..9d4dfbc90 100644 --- a/src/PhpPresentation/Shape/Drawing/Base64.php +++ b/src/PhpPresentation/Shape/Drawing/Base64.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Shape\Drawing; +use PhpOffice\PhpPresentation\Exception\UnauthorizedMimetypeException; + class Base64 extends AbstractDrawingAdapter { /** @@ -80,7 +82,7 @@ public function getContents(): string } /** - * @throws \Exception + * @throws UnauthorizedMimetypeException */ public function getExtension(): string { @@ -88,15 +90,12 @@ public function getExtension(): string list(, $mime) = explode(':', $data); if (!array_key_exists($mime, $this->arrayMimeExtension)) { - throw new \Exception('Type Mime not found : "' . $mime . '"'); + throw new UnauthorizedMimetypeException($mime, $this->arrayMimeExtension); } return $this->arrayMimeExtension[$mime]; } - /** - * @throws \Exception - */ public function getIndexedFilename(): string { return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension(); diff --git a/src/PhpPresentation/Shape/Drawing/File.php b/src/PhpPresentation/Shape/Drawing/File.php index 3f8eceefc..754985a44 100644 --- a/src/PhpPresentation/Shape/Drawing/File.php +++ b/src/PhpPresentation/Shape/Drawing/File.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape\Drawing; use PhpOffice\Common\File as CommonFile; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; class File extends AbstractDrawingAdapter { @@ -43,15 +44,15 @@ public function getPath(): string * @param string $pValue File path * @param bool $pVerifyFile Verify file * - * @throws \Exception + * @throws FileNotFoundException * - * @return \PhpOffice\PhpPresentation\Shape\Drawing\File + * @return self */ - public function setPath(string $pValue = '', $pVerifyFile = true): self + public function setPath(string $pValue = '', bool $pVerifyFile = true): self { if ($pVerifyFile) { if (!file_exists($pValue)) { - throw new \Exception("File $pValue not found!"); + throw new FileNotFoundException($pValue); } } $this->path = $pValue; @@ -76,12 +77,12 @@ public function getExtension(): string } /** - * @throws \Exception + * @throws FileNotFoundException */ public function getMimeType(): string { if (!CommonFile::fileExists($this->getPath())) { - throw new \Exception('File ' . $this->getPath() . ' does not exist'); + throw new FileNotFoundException($this->getPath()); } $image = getimagesizefromstring(CommonFile::fileGetContents($this->getPath())); diff --git a/src/PhpPresentation/Shape/Drawing/ZipFile.php b/src/PhpPresentation/Shape/Drawing/ZipFile.php index e14573011..2a921e9d2 100644 --- a/src/PhpPresentation/Shape/Drawing/ZipFile.php +++ b/src/PhpPresentation/Shape/Drawing/ZipFile.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape\Drawing; use PhpOffice\Common\File as CommonFile; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; class ZipFile extends AbstractDrawingAdapter { @@ -52,12 +53,12 @@ public function setPath(string $pValue = ''): self } /** - * @throws \Exception + * @throws FileNotFoundException */ public function getContents(): string { if (!CommonFile::fileExists($this->getZipFileOut())) { - throw new \Exception('File ' . $this->getZipFileOut() . ' does not exist'); + throw new FileNotFoundException($this->getZipFileOut()); } $imageZip = new \ZipArchive(); @@ -75,12 +76,12 @@ public function getExtension(): string } /** - * @throws \Exception + * @throws FileNotFoundException */ public function getMimeType(): string { if (!CommonFile::fileExists($this->getZipFileOut())) { - throw new \Exception('File ' . $this->getZipFileOut() . ' does not exist'); + throw new FileNotFoundException($this->getZipFileOut()); } $oArchive = new \ZipArchive(); $oArchive->open($this->getZipFileOut()); diff --git a/src/PhpPresentation/Shape/Group.php b/src/PhpPresentation/Shape/Group.php index 537e33763..dcb29ac4e 100644 --- a/src/PhpPresentation/Shape/Group.php +++ b/src/PhpPresentation/Shape/Group.php @@ -70,10 +70,8 @@ public function getShapeCollection() * Add shape to slide. * * @return AbstractShape - * - * @throws \Exception */ - public function addShape(AbstractShape $shape) + public function addShape(AbstractShape $shape): AbstractShape { $shape->setContainer($this); @@ -179,11 +177,9 @@ public function setHeight(int $pValue = 0) /** * Create rich text shape. * - * @return \PhpOffice\PhpPresentation\Shape\RichText - * - * @throws \Exception + * @return RichText */ - public function createRichTextShape() + public function createRichTextShape(): RichText { $shape = new RichText(); $this->addShape($shape); @@ -199,11 +195,9 @@ public function createRichTextShape() * @param int $toX Ending point x offset * @param int $toY Ending point y offset * - * @return \PhpOffice\PhpPresentation\Shape\Line - * - * @throws \Exception + * @return Line */ - public function createLineShape($fromX, $fromY, $toX, $toY) + public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Line { $shape = new Line($fromX, $fromY, $toX, $toY); $this->addShape($shape); @@ -214,11 +208,9 @@ public function createLineShape($fromX, $fromY, $toX, $toY) /** * Create chart shape. * - * @return \PhpOffice\PhpPresentation\Shape\Chart - * - * @throws \Exception + * @return Chart */ - public function createChartShape() + public function createChartShape(): Chart { $shape = new Chart(); $this->addShape($shape); @@ -229,11 +221,9 @@ public function createChartShape() /** * Create drawing shape. * - * @return \PhpOffice\PhpPresentation\Shape\Drawing\File - * - * @throws \Exception + * @return Drawing\File */ - public function createDrawingShape() + public function createDrawingShape(): Drawing\File { $shape = new Drawing\File(); $this->addShape($shape); @@ -246,11 +236,9 @@ public function createDrawingShape() * * @param int $columns Number of columns * - * @return \PhpOffice\PhpPresentation\Shape\Table - * - * @throws \Exception + * @return Table */ - public function createTableShape($columns = 1) + public function createTableShape(int $columns = 1): Table { $shape = new Table($columns); $this->addShape($shape); diff --git a/src/PhpPresentation/Shape/Hyperlink.php b/src/PhpPresentation/Shape/Hyperlink.php index 7063c726c..694fe2acf 100644 --- a/src/PhpPresentation/Shape/Hyperlink.php +++ b/src/PhpPresentation/Shape/Hyperlink.php @@ -65,10 +65,8 @@ class Hyperlink * * @param string $pUrl Url to link the shape to * @param string $pTooltip Tooltip to display on the hyperlink - * - * @throws \Exception */ - public function __construct($pUrl = '', $pTooltip = '') + public function __construct(string $pUrl = '', string $pTooltip = '') { // Initialise member variables $this->setUrl($pUrl); @@ -80,7 +78,7 @@ public function __construct($pUrl = '', $pTooltip = '') * * @return string */ - public function getUrl() + public function getUrl(): string { return $this->url; } @@ -90,9 +88,9 @@ public function getUrl() * * @param string $value * - * @return \PhpOffice\PhpPresentation\Shape\Hyperlink + * @return self */ - public function setUrl($value = '') + public function setUrl(string $value = ''): self { $this->url = $value; @@ -104,7 +102,7 @@ public function setUrl($value = '') * * @return string */ - public function getTooltip() + public function getTooltip(): string { return $this->tooltip; } @@ -114,9 +112,9 @@ public function getTooltip() * * @param string $value * - * @return \PhpOffice\PhpPresentation\Shape\Hyperlink + * @return self */ - public function setTooltip($value = '') + public function setTooltip(string $value = ''): self { $this->tooltip = $value; @@ -128,7 +126,7 @@ public function setTooltip($value = '') * * @return int */ - public function getSlideNumber() + public function getSlideNumber(): int { return $this->slideNumber; } @@ -138,9 +136,9 @@ public function getSlideNumber() * * @param int $value * - * @return \PhpOffice\PhpPresentation\Shape\Hyperlink + * @return self */ - public function setSlideNumber($value = 1) + public function setSlideNumber(int $value = 1): self { $this->url = 'ppaction://hlinksldjump'; $this->slideNumber = $value; @@ -153,7 +151,7 @@ public function setSlideNumber($value = 1) * * @return bool */ - public function isInternal() + public function isInternal(): bool { return false !== strpos($this->url, 'ppaction://'); } diff --git a/src/PhpPresentation/Shape/RichText.php b/src/PhpPresentation/Shape/RichText.php index 89b62acc2..b9082773a 100644 --- a/src/PhpPresentation/Shape/RichText.php +++ b/src/PhpPresentation/Shape/RichText.php @@ -22,6 +22,7 @@ use PhpOffice\PhpPresentation\AbstractShape; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\RichText\Paragraph; use PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface; @@ -179,7 +180,6 @@ public function __construct() $this->richTextParagraphs = [ new Paragraph(), ]; - $this->activeParagraph = 0; // Initialize parent parent::__construct(); @@ -203,12 +203,12 @@ public function getActiveParagraph(): Paragraph /** * Set active paragraph. * - * @throws \Exception + * @throws OutOfBoundsException */ public function setActiveParagraph(int $index = 0): Paragraph { if ($index >= count($this->richTextParagraphs)) { - throw new \Exception('Invalid paragraph count.'); + throw new OutOfBoundsException(0, count($this->richTextParagraphs), $index); } $this->activeParagraph = $index; @@ -219,12 +219,12 @@ public function setActiveParagraph(int $index = 0): Paragraph /** * Get paragraph. * - * @throws \Exception + * @throws OutOfBoundsException */ public function getParagraph(int $index = 0): Paragraph { if ($index >= count($this->richTextParagraphs)) { - throw new \Exception('Invalid paragraph count.'); + throw new OutOfBoundsException(0, count($this->richTextParagraphs), $index); } return $this->richTextParagraphs[$index]; @@ -232,8 +232,6 @@ public function getParagraph(int $index = 0): Paragraph /** * Create paragraph. - * - * @throws \Exception */ public function createParagraph(): Paragraph { @@ -265,8 +263,6 @@ public function createParagraph(): Paragraph * * @param TextElementInterface|null $pText Rich text element * - * @throws \Exception - * * @return self */ public function addText(TextElementInterface $pText = null): self @@ -282,8 +278,6 @@ public function addText(TextElementInterface $pText = null): self * @param string $pText Text * * @return RichText\TextElement - * - * @throws \Exception */ public function createText(string $pText = ''): RichText\TextElement { @@ -294,8 +288,6 @@ public function createText(string $pText = ''): RichText\TextElement * Create break. * * @return RichText\BreakElement - * - * @throws \Exception */ public function createBreak(): RichText\BreakElement { @@ -308,8 +300,6 @@ public function createBreak(): RichText\BreakElement * @param string $pText Text * * @return RichText\Run - * - * @throws \Exception */ public function createTextRun(string $pText = ''): RichText\Run { @@ -357,8 +347,6 @@ public function getParagraphs(): array * Set paragraphs. * * @param array $paragraphs Array of paragraphs - * - * @throws \Exception */ public function setParagraphs(array $paragraphs = []): self { @@ -553,14 +541,14 @@ public function getColumns(): int * * @param int $value * - * @throws \Exception - * * @return self + * + * @throws OutOfBoundsException */ public function setColumns(int $value = 1): self { if ($value > 16 || $value < 1) { - throw new \Exception('Number of columns should be 1-16'); + throw new OutOfBoundsException(1, 16, $value); } $this->columns = $value; diff --git a/src/PhpPresentation/Shape/RichText/Paragraph.php b/src/PhpPresentation/Shape/RichText/Paragraph.php index cadeaa17c..5fa26925c 100644 --- a/src/PhpPresentation/Shape/RichText/Paragraph.php +++ b/src/PhpPresentation/Shape/RichText/Paragraph.php @@ -128,8 +128,6 @@ public function getFont(): ?Font * Set font. * * @param Font|null $pFont Font - * - * @throws \Exception */ public function setFont(Font $pFont = null): self { @@ -147,9 +145,7 @@ public function getBulletStyle(): ?Bullet } /** - * Set bullet style. - * - * @throws \Exception + * Set bullet style */ public function setBulletStyle(Bullet $style = null): self { @@ -162,8 +158,6 @@ public function setBulletStyle(Bullet $style = null): self * Create text (can not be formatted !). * * @param string $pText Text - * - * @throws \Exception */ public function createText(string $pText = ''): TextElement { @@ -177,8 +171,6 @@ public function createText(string $pText = ''): TextElement * Add text. * * @param TextElementInterface|null $pText Rich text element - * - * @throws \Exception */ public function addText(TextElementInterface $pText = null): self { @@ -189,8 +181,6 @@ public function addText(TextElementInterface $pText = null): self /** * Create break. - * - * @throws \Exception */ public function createBreak(): BreakElement { @@ -204,8 +194,6 @@ public function createBreak(): BreakElement * Create text run (can be formatted). * * @param string $pText Text - * - * @throws \Exception */ public function createTextRun(string $pText = ''): Run { diff --git a/src/PhpPresentation/Shape/RichText/Run.php b/src/PhpPresentation/Shape/RichText/Run.php index c4fabf545..ba4f53250 100644 --- a/src/PhpPresentation/Shape/RichText/Run.php +++ b/src/PhpPresentation/Shape/RichText/Run.php @@ -57,9 +57,7 @@ public function getFont(): Font /** * Set font. * - * @param \PhpOffice\PhpPresentation\Style\Font $pFont Font - * - * @throws \Exception + * @param Font|null $pFont Font * * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface */ diff --git a/src/PhpPresentation/Shape/RichText/TextElement.php b/src/PhpPresentation/Shape/RichText/TextElement.php index 5efee1157..3c74d02fd 100644 --- a/src/PhpPresentation/Shape/RichText/TextElement.php +++ b/src/PhpPresentation/Shape/RichText/TextElement.php @@ -95,9 +95,6 @@ public function hasHyperlink(): bool return !is_null($this->hyperlink); } - /** - * @throws \Exception - */ public function getHyperlink(): Hyperlink { if (is_null($this->hyperlink)) { @@ -110,8 +107,6 @@ public function getHyperlink(): Hyperlink /** * Set Hyperlink. * - * @throws \Exception - * * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement */ public function setHyperlink(Hyperlink $pHyperlink = null) diff --git a/src/PhpPresentation/Shape/Table.php b/src/PhpPresentation/Shape/Table.php index 6df8edff0..d6313be11 100644 --- a/src/PhpPresentation/Shape/Table.php +++ b/src/PhpPresentation/Shape/Table.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\Table\Row; /** @@ -31,9 +32,9 @@ class Table extends AbstractGraphic implements ComparableInterface /** * Rows. * - * @var \PhpOffice\PhpPresentation\Shape\Table\Row[] + * @var array */ - private $rows; + private $rows = []; /** * Number of columns. @@ -49,8 +50,6 @@ class Table extends AbstractGraphic implements ComparableInterface */ public function __construct($columns = 1) { - // Initialise variables - $this->rows = []; $this->columnCount = $columns; // Initialize parent @@ -64,28 +63,38 @@ public function __construct($columns = 1) * Get row. * * @param int $row Row number - * @param bool $exceptionAsNull Return a null value instead of an exception? * - * @throws \Exception + * @throws OutOfBoundsException */ - public function getRow($row = 0, $exceptionAsNull = false): ?Row + public function getRow(int $row = 0): Row { if (!isset($this->rows[$row])) { - if ($exceptionAsNull) { - return null; - } - throw new \Exception('Row number out of bounds.'); + throw new OutOfBoundsException( + 0, + (count($this->rows) - 1) < 0 ? 0 : count($this->rows) - 1, + $row + ); } return $this->rows[$row]; } + /** + * @param int $row + * + * @return bool + */ + public function hasRow(int $row): bool + { + return isset($this->rows[$row]); + } + /** * Get rows. * * @return Row[] */ - public function getRows() + public function getRows(): array { return $this->rows; } @@ -93,9 +102,9 @@ public function getRows() /** * Create row. * - * @return \PhpOffice\PhpPresentation\Shape\Table\Row + * @return Row */ - public function createRow() + public function createRow(): Row { $row = new Row($this->columnCount); $this->rows[] = $row; @@ -106,7 +115,7 @@ public function createRow() /** * @return int */ - public function getNumColumns() + public function getNumColumns(): int { return $this->columnCount; } @@ -114,9 +123,9 @@ public function getNumColumns() /** * @param int $numColumn * - * @return Table + * @return self */ - public function setNumColumns($numColumn) + public function setNumColumns(int $numColumn): self { $this->columnCount = $numColumn; diff --git a/src/PhpPresentation/Shape/Table/Cell.php b/src/PhpPresentation/Shape/Table/Cell.php index 4231c5aa1..c8e8a942c 100644 --- a/src/PhpPresentation/Shape/Table/Cell.php +++ b/src/PhpPresentation/Shape/Table/Cell.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape\Table; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\RichText\Paragraph; use PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface; use PhpOffice\PhpPresentation\Style\Borders; @@ -128,12 +129,12 @@ public function getActiveParagraph(): Paragraph * * @param int $index * - * @throws \Exception + * @throws OutOfBoundsException */ public function setActiveParagraph($index = 0): Paragraph { if ($index >= count($this->richTextParagraphs)) { - throw new \Exception('Invalid paragraph count.'); + throw new OutOfBoundsException(0, count($this->richTextParagraphs), $index); } $this->activeParagraph = $index; @@ -146,12 +147,12 @@ public function setActiveParagraph($index = 0): Paragraph * * @param int $index * - * @throws \Exception + * @throws OutOfBoundsException */ - public function getParagraph($index = 0): Paragraph + public function getParagraph(int $index = 0): Paragraph { if ($index >= count($this->richTextParagraphs)) { - throw new \Exception('Invalid paragraph count.'); + throw new OutOfBoundsException(0, count($this->richTextParagraphs), $index); } return $this->richTextParagraphs[$index]; @@ -159,8 +160,6 @@ public function getParagraph($index = 0): Paragraph /** * Create paragraph. - * - * @throws \Exception */ public function createParagraph(): Paragraph { @@ -184,9 +183,7 @@ public function createParagraph(): Paragraph /** * Add text. * - * @param \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element - * - * @throws \Exception + * @param TextElementInterface $pText Rich text element * * @return \PhpOffice\PhpPresentation\Shape\Table\Cell */ @@ -203,8 +200,6 @@ public function addText(TextElementInterface $pText = null) * @param string $pText Text * * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement - * - * @throws \Exception */ public function createText($pText = '') { @@ -215,8 +210,6 @@ public function createText($pText = '') * Create break. * * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement - * - * @throws \Exception */ public function createBreak() { @@ -229,10 +222,8 @@ public function createBreak() * @param string $pText Text * * @return \PhpOffice\PhpPresentation\Shape\RichText\Run - * - * @throws \Exception */ - public function createTextRun($pText = '') + public function createTextRun(string $pText = '') { return $this->richTextParagraphs[$this->activeParagraph]->createTextRun($pText); } diff --git a/src/PhpPresentation/Shape/Table/Row.php b/src/PhpPresentation/Shape/Table/Row.php index 3e8c9cb51..039bbf16d 100644 --- a/src/PhpPresentation/Shape/Table/Row.php +++ b/src/PhpPresentation/Shape/Table/Row.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Shape\Table; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Style\Fill; /** @@ -31,14 +32,14 @@ class Row implements ComparableInterface /** * Cells. * - * @var \PhpOffice\PhpPresentation\Shape\Table\Cell[] + * @var Cell[] */ - private $cells; + private $cells = []; /** * Fill. * - * @var \PhpOffice\PhpPresentation\Style\Fill + * @var Fill */ private $fill; @@ -64,42 +65,50 @@ class Row implements ComparableInterface private $hashIndex; /** - * Create a new \PhpOffice\PhpPresentation\Shape\Table\Row instance. - * * @param int $columns Number of columns */ - public function __construct($columns = 1) + public function __construct(int $columns = 1) { - // Initialise variables - $this->cells = []; - for ($i = 0; $i < $columns; ++$i) { + // Fill + $this->fill = new Fill(); + // Cells + for ($inc = 0; $inc < $columns; ++$inc) { $this->cells[] = new Cell(); } - - // Set fill - $this->fill = new Fill(); } /** * Get cell. * * @param int $cell Cell number - * @param bool $exceptionAsNull Return a null value instead of an exception? * - * @throws \Exception + * @throws OutOfBoundsException */ - public function getCell($cell = 0, $exceptionAsNull = false): ?Cell + public function getCell(int $cell = 0): Cell { if (!isset($this->cells[$cell])) { - if ($exceptionAsNull) { - return null; - } - throw new \Exception('Cell number out of bounds.'); + throw new OutOfBoundsException( + 0, + (count($this->cells) - 1) < 0 ? count($this->cells) - 1 : 0, + $cell + ); } return $this->cells[$cell]; } + /** + * Get cell. + * + * @param int $cell Cell number + * + * @return bool + */ + public function hasCell(int $cell): bool + { + return isset($this->cells[$cell]); + } + /** * Get cells. * @@ -113,11 +122,11 @@ public function getCells(): array /** * Next cell (moves one cell to the right). * - * @return \PhpOffice\PhpPresentation\Shape\Table\Cell + * @return Cell * - * @throws \Exception + * @throws OutOfBoundsException */ - public function nextCell() + public function nextCell(): Cell { ++$this->activeCellIndex; if (isset($this->cells[$this->activeCellIndex])) { @@ -125,15 +134,20 @@ public function nextCell() return $this->cells[$this->activeCellIndex]; } - throw new \Exception('Cell count out of bounds.'); + + throw new OutOfBoundsException( + 0, + (count($this->cells) - 1) < 0 ? count($this->cells) - 1 : 0, + $this->activeCellIndex + ); } /** * Get fill. * - * @return \PhpOffice\PhpPresentation\Style\Fill + * @return Fill */ - public function getFill() + public function getFill(): Fill { return $this->fill; } @@ -141,9 +155,9 @@ public function getFill() /** * Set fill. * - * @return \PhpOffice\PhpPresentation\Shape\Table\Row + * @return self */ - public function setFill(Fill $fill) + public function setFill(Fill $fill): self { $this->fill = $fill; @@ -155,7 +169,7 @@ public function setFill(Fill $fill) * * @return int */ - public function getHeight() + public function getHeight(): int { return $this->height; } @@ -165,9 +179,9 @@ public function getHeight() * * @param int $value * - * @return \PhpOffice\PhpPresentation\Shape\Table\Row + * @return self */ - public function setHeight($value = 0) + public function setHeight(int $value = 0): self { $this->height = $value; diff --git a/src/PhpPresentation/Slide/AbstractSlide.php b/src/PhpPresentation/Slide/AbstractSlide.php index d633ea99d..ad5acd19c 100644 --- a/src/PhpPresentation/Slide/AbstractSlide.php +++ b/src/PhpPresentation/Slide/AbstractSlide.php @@ -127,10 +127,8 @@ public function setShapeCollection($shapeCollection = []) * Add shape to slide. * * @return AbstractShape - * - * @throws \Exception */ - public function addShape(AbstractShape $shape) + public function addShape(AbstractShape $shape): AbstractShape { $shape->setContainer($this); @@ -235,8 +233,6 @@ public function setHashIndex(int $value) /** * Create rich text shape. - * - * @throws \Exception */ public function createRichTextShape(): RichText { @@ -253,8 +249,6 @@ public function createRichTextShape(): RichText * @param int $fromY Starting point y offset * @param int $toX Ending point x offset * @param int $toY Ending point y offset - * - * @throws \Exception */ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Line { @@ -266,8 +260,6 @@ public function createLineShape(int $fromX, int $fromY, int $toX, int $toY): Lin /** * Create chart shape. - * - * @throws \Exception */ public function createChartShape(): Chart { @@ -279,8 +271,6 @@ public function createChartShape(): Chart /** * Create drawing shape. - * - * @throws \Exception */ public function createDrawingShape(): File { @@ -294,8 +284,6 @@ public function createDrawingShape(): File * Create table shape. * * @param int $columns Number of columns - * - * @throws \Exception */ public function createTableShape(int $columns = 1): Table { @@ -307,8 +295,6 @@ public function createTableShape(int $columns = 1): Table /** * Creates a group within this slide. - * - * @throws \Exception */ public function createGroup(): Group { @@ -328,8 +314,6 @@ public function getParent(): ?PhpPresentation /** * Re-bind parent. - * - * @throws \Exception */ public function rebindParent(PhpPresentation $parent): AbstractSlide { @@ -344,9 +328,6 @@ public function getBackground(): ?AbstractBackground return $this->background; } - /** - * @param AbstractBackground $background - */ public function setBackground(AbstractBackground $background = null): AbstractSlide { $this->background = $background; diff --git a/src/PhpPresentation/Slide/Background/Image.php b/src/PhpPresentation/Slide/Background/Image.php index 2a10d6565..39c998e2b 100644 --- a/src/PhpPresentation/Slide/Background/Image.php +++ b/src/PhpPresentation/Slide/Background/Image.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Slide\Background; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Slide\AbstractBackground; class Image extends AbstractBackground @@ -51,7 +52,7 @@ class Image extends AbstractBackground * * @return string */ - public function getPath() + public function getPath(): ?string { return $this->path; } @@ -62,15 +63,15 @@ public function getPath() * @param string $pValue File path * @param bool $pVerifyFile Verify file * - * @throws \Exception + * @throws FileNotFoundException * - * @return \PhpOffice\PhpPresentation\Slide\Background\Image + * @return self */ public function setPath(string $pValue = '', bool $pVerifyFile = true) { if ($pVerifyFile) { if (!file_exists($pValue)) { - throw new \Exception("File not found : $pValue"); + throw new FileNotFoundException($pValue); } if (0 == $this->width && 0 == $this->height) { diff --git a/src/PhpPresentation/Slide/Iterator.php b/src/PhpPresentation/Slide/Iterator.php index 0164d655e..787d06153 100644 --- a/src/PhpPresentation/Slide/Iterator.php +++ b/src/PhpPresentation/Slide/Iterator.php @@ -23,9 +23,6 @@ use IteratorIterator; use PhpOffice\PhpPresentation\PhpPresentation; -/** - * Used to iterate slides in PhpPresentation. - */ // @phpstan-ignore-next-line class Iterator extends IteratorIterator { @@ -71,8 +68,6 @@ public function rewind() * Current \PhpOffice\PhpPresentation\Slide. * * @return \PhpOffice\PhpPresentation\Slide - * - * @throws \Exception */ public function current() { diff --git a/src/PhpPresentation/Slide/Note.php b/src/PhpPresentation/Slide/Note.php index f8bc8b831..d1423a026 100644 --- a/src/PhpPresentation/Slide/Note.php +++ b/src/PhpPresentation/Slide/Note.php @@ -28,9 +28,6 @@ use PhpOffice\PhpPresentation\ShapeContainerInterface; use PhpOffice\PhpPresentation\Slide; -/** - * Note class. - */ class Note implements ComparableInterface, ShapeContainerInterface { /** @@ -120,10 +117,8 @@ public function getShapeCollection() * Add shape to slide. * * @return AbstractShape - * - * @throws \Exception */ - public function addShape(AbstractShape $shape) + public function addShape(AbstractShape $shape): AbstractShape { $shape->setContainer($this); @@ -132,8 +127,6 @@ public function addShape(AbstractShape $shape) /** * Create rich text shape. - * - * @throws \Exception */ public function createRichTextShape(): RichText { diff --git a/src/PhpPresentation/Slide/SlideMaster.php b/src/PhpPresentation/Slide/SlideMaster.php index 13e42aed9..3966f008c 100644 --- a/src/PhpPresentation/Slide/SlideMaster.php +++ b/src/PhpPresentation/Slide/SlideMaster.php @@ -29,29 +29,26 @@ use PhpOffice\PhpPresentation\Style\SchemeColor; use PhpOffice\PhpPresentation\Style\TextStyle; -/** - * Class SlideMaster. - */ class SlideMaster extends AbstractSlide implements ComparableInterface, ShapeContainerInterface { /** * Collection of Slide objects. * - * @var \PhpOffice\PhpPresentation\Slide\SlideLayout[] + * @var array */ protected $slideLayouts = []; /** * Mapping of colors to the theme. * - * @var \PhpOffice\PhpPresentation\Style\ColorMap + * @var ColorMap */ public $colorMap; /** - * @var \PhpOffice\PhpPresentation\Style\TextStyle + * @var TextStyle */ protected $textStyles; /** - * @var \PhpOffice\PhpPresentation\Style\SchemeColor[] + * @var array */ protected $arraySchemeColor = []; /** @@ -74,8 +71,6 @@ class SlideMaster extends AbstractSlide implements ComparableInterface, ShapeCon /** * Create a new slideMaster. - * - * @throws \Exception */ public function __construct(PhpPresentation $pParent = null) { @@ -104,11 +99,9 @@ public function __construct(PhpPresentation $pParent = null) /** * Create a slideLayout and add it to this presentation. * - * @return \PhpOffice\PhpPresentation\Slide\SlideLayout - * - * @throws \Exception + * @return SlideLayout */ - public function createSlideLayout() + public function createSlideLayout(): SlideLayout { $newSlideLayout = new SlideLayout($this); $this->addSlideLayout($newSlideLayout); @@ -119,13 +112,11 @@ public function createSlideLayout() /** * Add slideLayout. * - * @param \PhpOffice\PhpPresentation\Slide\SlideLayout $slideLayout - * - * @throws \Exception + * @param SlideLayout|null $slideLayout * - * @return \PhpOffice\PhpPresentation\Slide\SlideLayout + * @return SlideLayout */ - public function addSlideLayout(SlideLayout $slideLayout = null) + public function addSlideLayout(SlideLayout $slideLayout = null): SlideLayout { $this->slideLayouts[] = $slideLayout; @@ -133,9 +124,9 @@ public function addSlideLayout(SlideLayout $slideLayout = null) } /** - * @return SlideLayout[] + * @return array */ - public function getAllSlideLayouts() + public function getAllSlideLayouts(): array { return $this->slideLayouts; } @@ -143,15 +134,15 @@ public function getAllSlideLayouts() /** * @return TextStyle */ - public function getTextStyles() + public function getTextStyles(): TextStyle { return $this->textStyles; } /** - * @return $this + * @return self */ - public function setTextStyles(TextStyle $textStyle) + public function setTextStyles(TextStyle $textStyle): self { $this->textStyles = $textStyle; @@ -159,9 +150,9 @@ public function setTextStyles(TextStyle $textStyle) } /** - * @return $this + * @return self */ - public function addSchemeColor(SchemeColor $schemeColor) + public function addSchemeColor(SchemeColor $schemeColor): self { $this->arraySchemeColor[$schemeColor->getValue()] = $schemeColor; @@ -169,9 +160,9 @@ public function addSchemeColor(SchemeColor $schemeColor) } /** - * @return \PhpOffice\PhpPresentation\Style\SchemeColor[] + * @return array */ - public function getAllSchemeColors() + public function getAllSchemeColors(): array { return $this->arraySchemeColor; } diff --git a/src/PhpPresentation/Style/Alignment.php b/src/PhpPresentation/Style/Alignment.php index e620626bd..fae963ebe 100644 --- a/src/PhpPresentation/Style/Alignment.php +++ b/src/PhpPresentation/Style/Alignment.php @@ -21,10 +21,8 @@ namespace PhpOffice\PhpPresentation\Style; use PhpOffice\PhpPresentation\ComparableInterface; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; -/** - * \PhpOffice\PhpPresentation\Style\Alignment. - */ class Alignment implements ComparableInterface { /* Horizontal alignment */ @@ -62,14 +60,14 @@ class Alignment implements ComparableInterface * * @var string */ - private $horizontal; + private $horizontal = self::HORIZONTAL_LEFT; /** * Vertical. * * @var string */ - private $vertical; + private $vertical = self::VERTICAL_BASE; /** * Text Direction. @@ -134,16 +132,6 @@ class Alignment implements ComparableInterface */ private $hashIndex; - /** - * Create a new \PhpOffice\PhpPresentation\Style\Alignment. - */ - public function __construct() - { - // Initialise values - $this->horizontal = self::HORIZONTAL_LEFT; - $this->vertical = self::VERTICAL_BASE; - } - /** * Get Horizontal. */ @@ -199,12 +187,12 @@ public function getLevel(): int * * @param int $pValue Ranging 0 - 8 * - * @throws \Exception + * @throws OutOfBoundsException */ public function setLevel(int $pValue = 0): self { if ($pValue < 0) { - throw new \Exception('Invalid value should be more than 0.'); + throw new OutOfBoundsException(0, null, $pValue); } $this->level = $pValue; diff --git a/src/PhpPresentation/Style/Border.php b/src/PhpPresentation/Style/Border.php index da8c6b60a..59b55ba3a 100644 --- a/src/PhpPresentation/Style/Border.php +++ b/src/PhpPresentation/Style/Border.php @@ -22,9 +22,6 @@ use PhpOffice\PhpPresentation\ComparableInterface; -/** - * \PhpOffice\PhpPresentation\Style\Border. - */ class Border implements ComparableInterface { /* Line style */ @@ -60,19 +57,19 @@ class Border implements ComparableInterface * * @var string */ - private $lineStyle; + private $lineStyle = self::LINE_SINGLE; /** * Dash style. * * @var string */ - private $dashStyle; + private $dashStyle = self::DASH_SOLID; /** * Border color. * - * @var \PhpOffice\PhpPresentation\Style\Color + * @var Color */ private $color; @@ -83,15 +80,8 @@ class Border implements ComparableInterface */ private $hashIndex; - /** - * Create a new \PhpOffice\PhpPresentation\Style\Border. - */ public function __construct() { - // Initialise values - $this->lineWidth = 1; - $this->lineStyle = self::LINE_SINGLE; - $this->dashStyle = self::DASH_SOLID; $this->color = new Color(Color::COLOR_BLACK); } @@ -100,7 +90,7 @@ public function __construct() * * @return int */ - public function getLineWidth() + public function getLineWidth(): int { return $this->lineWidth; } @@ -110,9 +100,9 @@ public function getLineWidth() * * @param int $pValue * - * @return \PhpOffice\PhpPresentation\Style\Border + * @return self */ - public function setLineWidth($pValue = 1) + public function setLineWidth(int $pValue = 1): self { $this->lineWidth = $pValue; @@ -124,7 +114,7 @@ public function setLineWidth($pValue = 1) * * @return string */ - public function getLineStyle() + public function getLineStyle(): string { return $this->lineStyle; } @@ -134,9 +124,9 @@ public function getLineStyle() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\Style\Border + * @return self */ - public function setLineStyle($pValue = self::LINE_SINGLE) + public function setLineStyle(string $pValue = self::LINE_SINGLE): self { if ('' == $pValue) { $pValue = self::LINE_SINGLE; @@ -151,7 +141,7 @@ public function setLineStyle($pValue = self::LINE_SINGLE) * * @return string */ - public function getDashStyle() + public function getDashStyle(): string { return $this->dashStyle; } @@ -161,9 +151,9 @@ public function getDashStyle() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\Style\Border + * @return self */ - public function setDashStyle($pValue = self::DASH_SOLID) + public function setDashStyle(string $pValue = self::DASH_SOLID): self { if ('' == $pValue) { $pValue = self::DASH_SOLID; @@ -176,9 +166,9 @@ public function setDashStyle($pValue = self::DASH_SOLID) /** * Get Border Color. * - * @return \PhpOffice\PhpPresentation\Style\Color + * @return Color */ - public function getColor() + public function getColor(): ?Color { return $this->color; } @@ -186,13 +176,11 @@ public function getColor() /** * Set Border Color. * - * @param \PhpOffice\PhpPresentation\Style\Color $color - * - * @throws \Exception + * @param Color|null $color * - * @return \PhpOffice\PhpPresentation\Style\Border + * @return self */ - public function setColor(Color $color = null) + public function setColor(Color $color = null): self { $this->color = $color; diff --git a/src/PhpPresentation/Style/Fill.php b/src/PhpPresentation/Style/Fill.php index cfb13a2f6..19b92dbc2 100644 --- a/src/PhpPresentation/Style/Fill.php +++ b/src/PhpPresentation/Style/Fill.php @@ -22,9 +22,6 @@ use PhpOffice\PhpPresentation\ComparableInterface; -/** - * \PhpOffice\PhpPresentation\Style\Fill. - */ class Fill implements ComparableInterface { /* Fill types */ @@ -159,8 +156,6 @@ public function getStartColor(): Color * * @param Color $pValue * - * @throws \Exception - * * @return self */ public function setStartColor(Color $pValue): self diff --git a/src/PhpPresentation/Style/TextStyle.php b/src/PhpPresentation/Style/TextStyle.php index 0670f7645..64d7c5abf 100644 --- a/src/PhpPresentation/Style/TextStyle.php +++ b/src/PhpPresentation/Style/TextStyle.php @@ -22,9 +22,6 @@ use PhpOffice\PhpPresentation\Shape\RichText\Paragraph as RichTextParagraph; -/** - * Class TextStyle. - */ class TextStyle { /** @@ -44,10 +41,8 @@ class TextStyle * TextStyle constructor. * * @param bool $default - * - * @throws \Exception */ - public function __construct($default = true) + public function __construct(bool $default = true) { if ($default) { $oColorLT1 = new SchemeColor(); diff --git a/src/PhpPresentation/Writer/AbstractDecoratorWriter.php b/src/PhpPresentation/Writer/AbstractDecoratorWriter.php index e77b00e28..9b12bf936 100644 --- a/src/PhpPresentation/Writer/AbstractDecoratorWriter.php +++ b/src/PhpPresentation/Writer/AbstractDecoratorWriter.php @@ -29,7 +29,7 @@ abstract class AbstractDecoratorWriter /** * @return ZipInterface */ - abstract public function render(); + abstract public function render(): ZipInterface; /** * @var \PhpOffice\PhpPresentation\HashTable diff --git a/src/PhpPresentation/Writer/AbstractWriter.php b/src/PhpPresentation/Writer/AbstractWriter.php index 0646a3a23..14179efc3 100644 --- a/src/PhpPresentation/Writer/AbstractWriter.php +++ b/src/PhpPresentation/Writer/AbstractWriter.php @@ -41,7 +41,7 @@ abstract class AbstractWriter /** * Private PhpPresentation. * - * @var PhpPresentation + * @var PhpPresentation|null */ protected $oPresentation; @@ -60,15 +60,9 @@ public function getDrawingHashTable(): HashTable /** * Get PhpPresentation object. - * - * @throws \Exception */ - public function getPhpPresentation(): PhpPresentation + public function getPhpPresentation(): ?PhpPresentation { - if (empty($this->oPresentation)) { - throw new \Exception('No PhpPresentation assigned.'); - } - return $this->oPresentation; } @@ -77,8 +71,6 @@ public function getPhpPresentation(): PhpPresentation * * @param PhpPresentation|null $pPhpPresentation PhpPresentation object * - * @throws \Exception - * * @return self */ public function setPhpPresentation(PhpPresentation $pPhpPresentation = null) @@ -104,8 +96,6 @@ public function getZipAdapter(): ?ZipInterface * Get an array of all drawings. * * @return array - * - * @throws \Exception */ protected function allDrawings(): array { diff --git a/src/PhpPresentation/Writer/ODPresentation.php b/src/PhpPresentation/Writer/ODPresentation.php index bf23d02c8..a963259db 100644 --- a/src/PhpPresentation/Writer/ODPresentation.php +++ b/src/PhpPresentation/Writer/ODPresentation.php @@ -22,6 +22,10 @@ use DirectoryIterator; use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\FileCopyException; +use PhpOffice\PhpPresentation\Exception\FileRemoveException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\HashTable; use PhpOffice\PhpPresentation\PhpPresentation; @@ -53,13 +57,11 @@ class ODPresentation extends AbstractWriter implements WriterInterface * Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation. * * @param PhpPresentation $pPhpPresentation - * - * @throws \Exception */ public function __construct(PhpPresentation $pPhpPresentation = null) { // Assign PhpPresentation - $this->setPhpPresentation($pPhpPresentation); + $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation()); // Set up disk caching location $this->diskCachingDirectory = './'; @@ -73,12 +75,14 @@ public function __construct(PhpPresentation $pPhpPresentation = null) /** * Save PhpPresentation to file. * - * @throws \Exception + * @throws FileCopyException + * @throws FileRemoveException + * @throws InvalidParameterException */ public function save(string $pFilename): void { if (empty($pFilename)) { - throw new \Exception('Filename is empty'); + throw new InvalidParameterException('pFilename', ''); } // If $pFilename is php://output or php://stdout, make it a temporary file... $originalFilename = $pFilename; @@ -135,10 +139,10 @@ public function save(string $pFilename): void // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { if (false === copy($pFilename, $originalFilename)) { - throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename."); + throw new FileCopyException($pFilename, $originalFilename); } if (false === @unlink($pFilename)) { - throw new \Exception('The file ' . $pFilename . ' could not be removed.'); + throw new FileRemoveException($pFilename); } } } @@ -157,21 +161,21 @@ public function hasDiskCaching() * Set use disk caching where possible? * * @param bool $pValue - * @param string $pDirectory Disk caching directory + * @param string $directory Disk caching directory * - * @throws \Exception + * @throws DirectoryNotFoundException * * @return \PhpOffice\PhpPresentation\Writer\ODPresentation */ - public function setUseDiskCaching($pValue = false, $pDirectory = null) + public function setUseDiskCaching(bool $pValue = false, string $directory = null) { $this->useDiskCaching = $pValue; - if (!is_null($pDirectory)) { - if (!is_dir($pDirectory)) { - throw new \Exception("Directory does not exist: $pDirectory"); + if (!is_null($directory)) { + if (!is_dir($directory)) { + throw new DirectoryNotFoundException($directory); } - $this->diskCachingDirectory = $pDirectory; + $this->diskCachingDirectory = $directory; } return $this; diff --git a/src/PhpPresentation/Writer/ODPresentation/Content.php b/src/PhpPresentation/Writer/ODPresentation/Content.php index 7a29fce45..bf9fc5b65 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Content.php +++ b/src/PhpPresentation/Writer/ODPresentation/Content.php @@ -77,9 +77,6 @@ class Content extends AbstractDecoratorWriter */ protected $shapeId; - /** - * @throws \Exception - */ public function render(): ZipInterface { $this->getZip()->addFromString('content.xml', $this->writeContent()); @@ -91,10 +88,8 @@ public function render(): ZipInterface * Write content file to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeContent(): string + protected function writeContent(): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -387,7 +382,7 @@ public function writeContent(): string /** * Write picture. */ - public function writeShapeMedia(XMLWriter $objWriter, Media $shape): void + protected function writeShapeMedia(XMLWriter $objWriter, Media $shape): void { // draw:frame $objWriter->startElement('draw:frame'); @@ -433,10 +428,8 @@ public function writeShapeMedia(XMLWriter $objWriter, Media $shape): void * Write picture. * * @param AbstractDrawingAdapter $shape - * - * @throws \Exception */ - public function writeShapeDrawing(XMLWriter $objWriter, ShapeDrawing\AbstractDrawingAdapter $shape): void + protected function writeShapeDrawing(XMLWriter $objWriter, ShapeDrawing\AbstractDrawingAdapter $shape): void { // draw:frame $objWriter->startElement('draw:frame'); @@ -480,10 +473,8 @@ public function writeShapeDrawing(XMLWriter $objWriter, ShapeDrawing\AbstractDra /** * Write text. - * - * @throws \Exception */ - public function writeShapeTxt(XMLWriter $objWriter, RichText $shape): void + protected function writeShapeTxt(XMLWriter $objWriter, RichText $shape): void { // draw:frame $objWriter->startElement('draw:frame'); @@ -640,7 +631,7 @@ public function writeShapeTxt(XMLWriter $objWriter, RichText $shape): void /** * Write Comment. */ - public function writeShapeComment(XMLWriter $objWriter, Comment $oShape): void + protected function writeShapeComment(XMLWriter $objWriter, Comment $oShape): void { /* * Note : This element is not valid in the Schema 1.2 @@ -660,7 +651,7 @@ public function writeShapeComment(XMLWriter $objWriter, Comment $oShape): void $objWriter->endElement(); } - public function writeShapeLine(XMLWriter $objWriter, Line $shape): void + protected function writeShapeLine(XMLWriter $objWriter, Line $shape): void { // draw:line $objWriter->startElement('draw:line'); @@ -678,10 +669,8 @@ public function writeShapeLine(XMLWriter $objWriter, Line $shape): void /** * Write table Shape. - * - * @throws \Exception */ - public function writeShapeTable(XMLWriter $objWriter, Table $shape): void + protected function writeShapeTable(XMLWriter $objWriter, Table $shape): void { // draw:frame $objWriter->startElement('draw:frame'); @@ -774,10 +763,8 @@ public function writeShapeTable(XMLWriter $objWriter, Table $shape): void /** * Write table Chart. - * - * @throws \Exception */ - public function writeShapeChart(XMLWriter $objWriter, Chart $shape): void + protected function writeShapeChart(XMLWriter $objWriter, Chart $shape): void { $arrayChart = $this->getArrayChart(); $arrayChart[$this->shapeId] = $shape; @@ -805,10 +792,8 @@ public function writeShapeChart(XMLWriter $objWriter, Chart $shape): void /** * Writes a group of shapes. - * - * @throws \Exception */ - public function writeShapeGroup(XMLWriter $objWriter, Group $group): void + protected function writeShapeGroup(XMLWriter $objWriter, Group $group): void { // draw:g $objWriter->startElement('draw:g'); @@ -840,7 +825,7 @@ public function writeShapeGroup(XMLWriter $objWriter, Group $group): void /** * Writes the style information for a group of shapes. */ - public function writeGroupStyle(XMLWriter $objWriter, Group $group): void + protected function writeGroupStyle(XMLWriter $objWriter, Group $group): void { $shapes = $group->getShapeCollection(); foreach ($shapes as $shape) { @@ -866,7 +851,7 @@ public function writeGroupStyle(XMLWriter $objWriter, Group $group): void /** * Write the default style information for a RichText shape. */ - public function writeTxtStyle(XMLWriter $objWriter, RichText $shape): void + protected function writeTxtStyle(XMLWriter $objWriter, RichText $shape): void { // style:style $objWriter->startElement('style:style'); @@ -974,7 +959,7 @@ public function writeTxtStyle(XMLWriter $objWriter, RichText $shape): void /** * Write the default style information for an AbstractDrawingAdapter. */ - public function writeDrawingStyle(XMLWriter $objWriter, AbstractDrawingAdapter $shape): void + protected function writeDrawingStyle(XMLWriter $objWriter, AbstractDrawingAdapter $shape): void { // style:style $objWriter->startElement('style:style'); @@ -996,7 +981,7 @@ public function writeDrawingStyle(XMLWriter $objWriter, AbstractDrawingAdapter $ /** * Write the default style information for a Line shape. */ - public function writeLineStyle(XMLWriter $objWriter, Line $shape): void + protected function writeLineStyle(XMLWriter $objWriter, Line $shape): void { // style:style $objWriter->startElement('style:style'); @@ -1028,7 +1013,7 @@ public function writeLineStyle(XMLWriter $objWriter, Line $shape): void /** * Write the default style information for a Table shape. */ - public function writeTableStyle(XMLWriter $objWriter, Table $shape): void + protected function writeTableStyle(XMLWriter $objWriter, Table $shape): void { foreach ($shape->getRows() as $keyRow => $shapeRow) { // style:style @@ -1140,10 +1125,8 @@ public function writeTableStyle(XMLWriter $objWriter, Table $shape): void /** * Write the slide note. - * - * @throws \Exception */ - public function writeSlideNote(XMLWriter $objWriter, Note $note): void + protected function writeSlideNote(XMLWriter $objWriter, Note $note): void { $shapesNote = $note->getShapeCollection(); if (count($shapesNote) > 0) { @@ -1165,7 +1148,7 @@ public function writeSlideNote(XMLWriter $objWriter, Note $note): void /** * Write style of a slide. */ - public function writeStyleSlide(XMLWriter $objWriter, Slide $slide, int $incPage): void + protected function writeStyleSlide(XMLWriter $objWriter, Slide $slide, int $incPage): void { // style:style $objWriter->startElement('style:style'); diff --git a/src/PhpPresentation/Writer/ODPresentation/Meta.php b/src/PhpPresentation/Writer/ODPresentation/Meta.php index b91d7d5d8..d976503e8 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Meta.php +++ b/src/PhpPresentation/Writer/ODPresentation/Meta.php @@ -20,17 +20,16 @@ namespace PhpOffice\PhpPresentation\Writer\ODPresentation; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\DocumentProperties; class Meta extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/ODPresentation/MetaInfManifest.php b/src/PhpPresentation/Writer/ODPresentation/MetaInfManifest.php index 0265d47bf..49f023e76 100644 --- a/src/PhpPresentation/Writer/ODPresentation/MetaInfManifest.php +++ b/src/PhpPresentation/Writer/ODPresentation/MetaInfManifest.php @@ -29,10 +29,8 @@ class MetaInfManifest extends AbstractDecoratorWriter { /** * @return ZipInterface - * - * @throws \Exception */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/ODPresentation/Mimetype.php b/src/PhpPresentation/Writer/ODPresentation/Mimetype.php index f364860be..80101241d 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Mimetype.php +++ b/src/PhpPresentation/Writer/ODPresentation/Mimetype.php @@ -20,14 +20,14 @@ namespace PhpOffice\PhpPresentation\Writer\ODPresentation; +use PhpOffice\Common\Adapter\Zip\ZipInterface; + class Mimetype extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { $this->getZip()->addFromString('mimetype', 'application/vnd.oasis.opendocument.presentation'); diff --git a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php index 4addc132a..38c8057ae 100644 --- a/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php +++ b/src/PhpPresentation/Writer/ODPresentation/ObjectsChart.php @@ -69,9 +69,6 @@ class ObjectsChart extends AbstractDecoratorWriter */ protected $rangeCol; - /** - * @throws \Exception - */ public function render(): ZipInterface { foreach ($this->getArrayChart() as $keyChart => $shapeChart) { @@ -85,9 +82,6 @@ public function render(): ZipInterface return $this->getZip(); } - /** - * @throws \Exception - */ protected function writeContentPart(Chart $chart): string { $this->xmlContent = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -213,10 +207,7 @@ protected function writeContentPart(Chart $chart): string return $this->xmlContent->getData(); } - /** - * @throws \Exception - */ - private function writeAxis(Chart $chart): void + protected function writeAxis(Chart $chart): void { $chartType = $chart->getPlotArea()->getType(); @@ -285,8 +276,6 @@ protected function writeGridline(?Chart\Gridlines $oGridlines, string $styleName } /** - * @throws \Exception - * * @todo Set function in \PhpPresentation\Shape\Chart\Axis for defining width and color of the axis */ protected function writeAxisStyle(Chart $chart): void @@ -318,7 +307,7 @@ protected function writeAxisStyle(Chart $chart): void $this->writeGridlineStyle($chart->getPlotArea()->getAxisY()->getMinorGridlines(), 'styleAxisYGridlinesMinor'); } - private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, AbstractType $chartType): void + protected function writeAxisMainStyle(Chart\Axis $axis, string $styleName, AbstractType $chartType): void { // style:style $this->xmlContent->startElement('style:style'); @@ -367,7 +356,7 @@ private function writeAxisMainStyle(Chart\Axis $axis, string $styleName, Abstrac $this->xmlContent->endElement(); } - private function writeAxisTitleStyle(Chart\Axis $axis, string $styleName): void + protected function writeAxisTitleStyle(Chart\Axis $axis, string $styleName): void { // style:style $this->xmlContent->startElement('style:style'); @@ -409,7 +398,7 @@ protected function writeGridlineStyle(?Chart\Gridlines $oGridlines, string $styl $this->xmlContent->endElement(); } - private function writeChartStyle(Chart $chart): void + protected function writeChartStyle(Chart $chart): void { // style:style $this->xmlContent->startElement('style:style'); @@ -425,7 +414,7 @@ private function writeChartStyle(Chart $chart): void $this->xmlContent->endElement(); } - private function writeFloor(): void + protected function writeFloor(): void { // chart:floor $this->xmlContent->startElement('chart:floor'); @@ -434,7 +423,7 @@ private function writeFloor(): void $this->xmlContent->endElement(); } - private function writeFloorStyle(): void + protected function writeFloorStyle(): void { // style:style $this->xmlContent->startElement('style:style'); @@ -453,7 +442,7 @@ private function writeFloorStyle(): void $this->xmlContent->endElement(); } - private function writeLegend(Chart $chart): void + protected function writeLegend(Chart $chart): void { // chart:legend $this->xmlContent->startElement('chart:legend'); @@ -484,7 +473,7 @@ private function writeLegend(Chart $chart): void $this->xmlContent->endElement(); } - private function writeLegendStyle(Chart $chart): void + protected function writeLegendStyle(Chart $chart): void { // style:style $this->xmlContent->startElement('style:style'); @@ -507,10 +496,7 @@ private function writeLegendStyle(Chart $chart): void $this->xmlContent->endElement(); } - /** - * @throws \Exception - */ - private function writePlotArea(Chart $chart): void + protected function writePlotArea(Chart $chart): void { $chartType = $chart->getPlotArea()->getType(); @@ -563,11 +549,9 @@ private function writePlotArea(Chart $chart): void } /** - * @throws \Exception - * * @see : http://books.evc-cit.info/odbook/ch08.html#chart-plot-area-section */ - private function writePlotAreaStyle(Chart $chart): void + protected function writePlotAreaStyle(Chart $chart): void { $chartType = $chart->getPlotArea()->getType(); @@ -629,10 +613,7 @@ private function writePlotAreaStyle(Chart $chart): void $this->xmlContent->endElement(); } - /** - * @throws \Exception - */ - private function writeSeries(Chart $chart, Chart\Series $series): void + protected function writeSeries(Chart $chart, Chart\Series $series): void { $chartType = $chart->getPlotArea()->getType(); @@ -695,10 +676,7 @@ private function writeSeries(Chart $chart, Chart\Series $series): void $this->xmlContent->endElement(); } - /** - * @throws \Exception - */ - private function writeSeriesStyle(Chart $chart, Chart\Series $series): void + protected function writeSeriesStyle(Chart $chart, Chart\Series $series): void { $chartType = $chart->getPlotArea()->getType(); @@ -830,7 +808,7 @@ private function writeSeriesStyle(Chart $chart, Chart\Series $series): void } } - private function writeTable(): void + protected function writeTable(): void { // table:table $this->xmlContent->startElement('table:table'); @@ -924,7 +902,7 @@ private function writeTable(): void $this->xmlContent->endElement(); } - private function writeTitle(Title $oTitle): void + protected function writeTitle(Title $oTitle): void { if (!$oTitle->isVisible()) { return; @@ -942,7 +920,7 @@ private function writeTitle(Title $oTitle): void $this->xmlContent->endElement(); } - private function writeTitleStyle(Title $oTitle): void + protected function writeTitleStyle(Title $oTitle): void { if (!$oTitle->isVisible()) { return; @@ -963,7 +941,7 @@ private function writeTitleStyle(Title $oTitle): void $this->xmlContent->endElement(); } - private function writeWall(): void + protected function writeWall(): void { // chart:wall $this->xmlContent->startElement('chart:wall'); @@ -971,10 +949,7 @@ private function writeWall(): void $this->xmlContent->endElement(); } - /** - * @throws \Exception - */ - private function writeWallStyle(Chart $chart): void + protected function writeWallStyle(Chart $chart): void { $chartType = $chart->getPlotArea()->getType(); diff --git a/src/PhpPresentation/Writer/ODPresentation/Pictures.php b/src/PhpPresentation/Writer/ODPresentation/Pictures.php index 405c6b184..ac8cfa933 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Pictures.php +++ b/src/PhpPresentation/Writer/ODPresentation/Pictures.php @@ -28,10 +28,8 @@ class Pictures extends AbstractDecoratorWriter { /** * @return ZipInterface - * - * @throws \Exception */ - public function render() + public function render(): ZipInterface { $arrMedia = []; for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { diff --git a/src/PhpPresentation/Writer/ODPresentation/Styles.php b/src/PhpPresentation/Writer/ODPresentation/Styles.php index 351e8a205..6bc8cbfb2 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Styles.php +++ b/src/PhpPresentation/Writer/ODPresentation/Styles.php @@ -46,9 +46,6 @@ class Styles extends AbstractDecoratorWriter */ protected $arrayStrokeDash = []; - /** - * @throws \Exception - */ public function render(): ZipInterface { $this->getZip()->addFromString('styles.xml', $this->writePart()); @@ -60,8 +57,6 @@ public function render(): ZipInterface * Write Meta file to XML format. * * @return string XML Output - * - * @throws \Exception */ protected function writePart(): string { diff --git a/src/PhpPresentation/Writer/ODPresentation/ThumbnailsThumbnail.php b/src/PhpPresentation/Writer/ODPresentation/ThumbnailsThumbnail.php index 7e9988983..1d754335f 100644 --- a/src/PhpPresentation/Writer/ODPresentation/ThumbnailsThumbnail.php +++ b/src/PhpPresentation/Writer/ODPresentation/ThumbnailsThumbnail.php @@ -20,14 +20,11 @@ namespace PhpOffice\PhpPresentation\Writer\ODPresentation; +use PhpOffice\Common\Adapter\Zip\ZipInterface; + class ThumbnailsThumbnail extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath(); if ($pathThumbnail) { diff --git a/src/PhpPresentation/Writer/PowerPoint2007.php b/src/PhpPresentation/Writer/PowerPoint2007.php index db43657ff..e98a61837 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007.php +++ b/src/PhpPresentation/Writer/PowerPoint2007.php @@ -22,8 +22,14 @@ use DirectoryIterator; use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\FileCopyException; +use PhpOffice\PhpPresentation\Exception\FileRemoveException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\HashTable; use PhpOffice\PhpPresentation\PhpPresentation; +use PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter; +use ReflectionClass; /** * \PhpOffice\PhpPresentation\Writer\PowerPoint2007. @@ -48,13 +54,11 @@ class PowerPoint2007 extends AbstractWriter implements WriterInterface * Create a new PowerPoint2007 file. * * @param PhpPresentation $pPhpPresentation - * - * @throws \Exception */ public function __construct(PhpPresentation $pPhpPresentation = null) { // Assign PhpPresentation - $this->setPhpPresentation($pPhpPresentation); + $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation()); // Set up disk caching location $this->diskCachingDir = './'; @@ -68,12 +72,14 @@ public function __construct(PhpPresentation $pPhpPresentation = null) /** * Save PhpPresentation to file. * - * @throws \Exception + * @throws FileCopyException + * @throws FileRemoveException + * @throws InvalidParameterException */ public function save(string $pFilename): void { if (empty($pFilename)) { - throw new \Exception('Filename is empty'); + throw new InvalidParameterException('pFilename', ''); } $oPresentation = $this->getPhpPresentation(); @@ -100,9 +106,9 @@ public function save(string $pFilename): void } $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php'); - $class = new \ReflectionClass($class); + $class = new ReflectionClass($class); - if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) { + if ($class->isAbstract() || !$class->isSubclassOf(AbstractDecoratorWriter::class)) { continue; } $arrayFiles[$oFile->getBasename('.php')] = $class; @@ -125,10 +131,10 @@ public function save(string $pFilename): void // If a temporary file was used, copy it to the correct file stream if ($originalFilename != $pFilename) { if (false === copy($pFilename, $originalFilename)) { - throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename."); + throw new FileCopyException($pFilename, $originalFilename); } if (false === @unlink($pFilename)) { - throw new \Exception('The file ' . $pFilename . ' could not be removed.'); + throw new FileRemoveException($pFilename); } } } @@ -146,22 +152,22 @@ public function hasDiskCaching() /** * Set use disk caching where possible? * - * @param bool $pValue - * @param string $pDirectory Disk caching directory + * @param bool $useDiskCaching + * @param string $directory Disk caching directory * - * @throws \Exception + * @throws DirectoryNotFoundException * * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007 */ - public function setUseDiskCaching($pValue = false, $pDirectory = null) + public function setUseDiskCaching(bool $useDiskCaching = false, string $directory = null) { - $this->useDiskCaching = $pValue; + $this->useDiskCaching = $useDiskCaching; - if (!is_null($pDirectory)) { - if (!is_dir($pDirectory)) { - throw new \Exception("Directory does not exist: $pDirectory"); + if (!is_null($directory)) { + if (!is_dir($directory)) { + throw new DirectoryNotFoundException($directory); } - $this->diskCachingDir = $pDirectory; + $this->diskCachingDir = $directory; } return $this; diff --git a/src/PhpPresentation/Writer/PowerPoint2007/AbstractDecoratorWriter.php b/src/PhpPresentation/Writer/PowerPoint2007/AbstractDecoratorWriter.php index 7e9eff342..672d828a2 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/AbstractDecoratorWriter.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/AbstractDecoratorWriter.php @@ -37,15 +37,9 @@ abstract class AbstractDecoratorWriter extends \PhpOffice\PhpPresentation\Writer * @param string $pType Relationship type * @param string $pTarget Relationship target * @param string $pTargetMode Relationship target mode - * - * @throws \Exception */ - protected function writeRelationship(XMLWriter $objWriter, int $pId = 1, string $pType = '', string $pTarget = '', string $pTargetMode = ''): void + protected function writeRelationship(XMLWriter $objWriter, int $pId, string $pType, string $pTarget, string $pTargetMode = ''): void { - if ('' == $pType || '' == $pTarget) { - throw new \Exception('Invalid parameters passed.'); - } - // Write relationship $objWriter->startElement('Relationship'); $objWriter->writeAttribute('Id', 'rId' . (string) $pId); @@ -66,8 +60,6 @@ protected function writeRelationship(XMLWriter $objWriter, int $pId = 1, string * @param Border $pBorder Border * @param string $pElementName Element name * @param bool $isMarker - * - * @throws \Exception */ protected function writeBorder(XMLWriter $objWriter, Border $pBorder, string $pElementName = 'L', bool $isMarker = false): void { @@ -154,8 +146,6 @@ protected function writeColor(XMLWriter $objWriter, Color $color, ?int $alpha = * * @param XMLWriter $objWriter XML Writer * @param Fill|null $pFill Fill style - * - * @throws \Exception */ protected function writeFill(XMLWriter $objWriter, ?Fill $pFill): void { @@ -193,8 +183,6 @@ protected function writeFill(XMLWriter $objWriter, ?Fill $pFill): void * * @param XMLWriter $objWriter XML Writer * @param Fill $pFill Fill style - * - * @throws \Exception */ protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill): void { @@ -209,8 +197,6 @@ protected function writeSolidFill(XMLWriter $objWriter, Fill $pFill): void * * @param XMLWriter $objWriter XML Writer * @param Fill $pFill Fill style - * - * @throws \Exception */ protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill): void { @@ -247,8 +233,6 @@ protected function writeGradientFill(XMLWriter $objWriter, Fill $pFill): void * * @param XMLWriter $objWriter XML Writer * @param Fill $pFill Fill style - * - * @throws \Exception */ protected function writePatternFill(XMLWriter $objWriter, Fill $pFill): void { @@ -273,9 +257,7 @@ protected function writePatternFill(XMLWriter $objWriter, Fill $pFill): void } /** - * Write Outline. - * - * @throws \Exception + * Write Outline */ protected function writeOutline(XMLWriter $objWriter, ?Outline $oOutline): void { diff --git a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php index dbb7c53fd..d7f533108 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php @@ -25,6 +25,7 @@ use PhpOffice\Common\Text; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\AbstractShape; +use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException; use PhpOffice\PhpPresentation\Shape\AbstractGraphic; use PhpOffice\PhpPresentation\Shape\AutoShape; use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart; @@ -55,8 +56,6 @@ abstract class AbstractSlide extends AbstractDecoratorWriter { /** * @return mixed - * - * @throws \Exception */ protected function writeDrawingRelations(AbstractSlideAlias $pSlideMaster, XMLWriter $objWriter, int $relId) { @@ -124,7 +123,7 @@ protected function writeDrawingRelations(AbstractSlideAlias $pSlideMaster, XMLWr * @param array|ArrayObject $shapes * @param int $shapeId * - * @throws \Exception + * @throws UndefinedChartTypeException */ protected function writeShapeCollection(XMLWriter $objWriter, $shapes = [], &$shapeId = 0): void { @@ -151,7 +150,7 @@ protected function writeShapeCollection(XMLWriter $objWriter, $shapes = [], &$sh $this->writeShapeGroup($objWriter, $shape, $shapeId); } elseif ($shape instanceof Comment) { } else { - throw new \Exception(sprintf('Unknown Shape type: %s', get_class($shape))); + throw new UndefinedChartTypeException(); } } } @@ -160,8 +159,8 @@ protected function writeShapeCollection(XMLWriter $objWriter, $shapes = [], &$sh * Write txt. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param RichText $shape + * @param int $shapeId */ protected function writeShapeText(XMLWriter $objWriter, RichText $shape, int $shapeId): void { @@ -312,8 +311,8 @@ protected function writeShapeText(XMLWriter $objWriter, RichText $shape, int $sh * Write table. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param ShapeTable $shape + * @param int $shapeId */ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, int $shapeId): void { @@ -406,13 +405,9 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, int // Current cell $currentCell = $shape->getRow($row)->getCell($cell); // Next cell right - $nextCellRight = $shape->getRow($row)->getCell($cell + 1, true); + $hasNextCellRight = $shape->getRow($row)->hasCell($cell + 1); // Next cell below - $nextRowBelow = $shape->getRow($row + 1, true); - $nextCellBelow = null; - if (null != $nextRowBelow) { - $nextCellBelow = $nextRowBelow->getCell($cell, true); - } + $hasNextRowBelow = $shape->hasRow($row + 1); // a:tc $objWriter->startElement('a:tc'); // Colspan @@ -476,15 +471,18 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, int $borderDiagonalDown = $currentCell->getBorders()->getDiagonalDown(); $borderDiagonalUp = $currentCell->getBorders()->getDiagonalUp(); // Fix PowerPoint implementation - if (!is_null($nextCellRight) - && $nextCellRight->getBorders()->getRight()->getHashCode() != $defaultBorder->getHashCode() - ) { - $borderRight = $nextCellRight->getBorders()->getLeft(); + if ($hasNextCellRight) { + $nextCellRight = $shape->getRow($row)->getCell($cell + 1); + if ($nextCellRight->getBorders()->getRight()->getHashCode() != $defaultBorder->getHashCode()) { + $borderRight = $nextCellRight->getBorders()->getLeft(); + } } - if (!is_null($nextCellBelow) - && $nextCellBelow->getBorders()->getBottom()->getHashCode() != $defaultBorder->getHashCode() - ) { - $borderBottom = $nextCellBelow->getBorders()->getTop(); + if ($hasNextRowBelow) { + $nextRowBelow = $shape->getRow($row + 1); + $nextCellBelow = $nextRowBelow->getCell($cell); + if ($nextCellBelow->getBorders()->getBottom()->getHashCode() != $defaultBorder->getHashCode()) { + $borderBottom = $nextCellBelow->getBorders()->getTop(); + } } // Write borders $this->writeBorder($objWriter, $borderLeft, 'L'); @@ -511,8 +509,6 @@ protected function writeShapeTable(XMLWriter $objWriter, ShapeTable $shape, int * * @param XMLWriter $objWriter XML Writer * @param array $paragraphs - * - * @throws \Exception */ protected function writeParagraphs(XMLWriter $objWriter, array $paragraphs, bool $bIsPlaceholder = false): void { @@ -655,8 +651,6 @@ protected function writeParagraphs(XMLWriter $objWriter, array $paragraphs, bool * Write Line Shape. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception */ protected function writeShapeLine(XMLWriter $objWriter, Line $shape, int $shapeId): void { @@ -781,8 +775,6 @@ protected function writeShadow(XMLWriter $objWriter, Shadow $oShadow): void * * @param XMLWriter $objWriter XML Writer * @param AbstractShape|TextElement $shape - * - * @throws \Exception */ protected function writeHyperlink(XMLWriter $objWriter, $shape): void { @@ -801,8 +793,6 @@ protected function writeHyperlink(XMLWriter $objWriter, $shape): void /** * Write Note Slide. - * - * @throws \Exception */ protected function writeNote(Note $pNote): string { @@ -1244,8 +1234,6 @@ protected function writeShapeChart(XMLWriter $objWriter, ShapeChart $shape, int * Write pic. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception */ protected function writeShapePic(XMLWriter $objWriter, AbstractGraphic $shape, int $shapeId): void { @@ -1416,8 +1404,6 @@ protected function writeShapePic(XMLWriter $objWriter, AbstractGraphic $shape, i * Write group. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception */ protected function writeShapeGroup(XMLWriter $objWriter, Group $group, int &$shapeId): void { diff --git a/src/PhpPresentation/Writer/PowerPoint2007/CommentAuthors.php b/src/PhpPresentation/Writer/PowerPoint2007/CommentAuthors.php index 4471b612f..35b90e177 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/CommentAuthors.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/CommentAuthors.php @@ -27,12 +27,7 @@ class CommentAuthors extends AbstractDecoratorWriter { - /** - * @return ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { /** * @var Author[] diff --git a/src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php b/src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php index e0293817e..6d0a6d7e3 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/ContentTypes.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart; use PhpOffice\PhpPresentation\Shape\Comment; @@ -30,12 +31,7 @@ */ class ContentTypes extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -127,7 +123,11 @@ public function render() $shapeIndex = $this->getDrawingHashTable()->getByIndex($i); if ($shapeIndex instanceof ShapeChart) { // Chart content type - $this->writeOverrideContentType($objWriter, '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml'); + $this->writeOverrideContentType( + $objWriter, + '/ppt/charts/chart' . $shapeIndex->getImageIndex() . '.xml', + 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml' + ); } elseif ($shapeIndex instanceof AbstractDrawingAdapter) { $extension = strtolower($shapeIndex->getExtension()); $mimeType = $shapeIndex->getMimeType(); @@ -153,14 +153,9 @@ public function render() * @param XMLWriter $objWriter XML Writer * @param string $pPartname Part name * @param string $pContentType Content type - * - * @throws \Exception */ - private function writeDefaultContentType(XMLWriter $objWriter, string $pPartname = '', string $pContentType = ''): void + protected function writeDefaultContentType(XMLWriter $objWriter, string $pPartname, string $pContentType): void { - if ('' == $pPartname || '' == $pContentType) { - throw new \Exception('Invalid parameters passed.'); - } // Write content type $objWriter->startElement('Default'); $objWriter->writeAttribute('Extension', $pPartname); @@ -174,14 +169,9 @@ private function writeDefaultContentType(XMLWriter $objWriter, string $pPartname * @param XMLWriter $objWriter XML Writer * @param string $pPartname Part name * @param string $pContentType Content type - * - * @throws \Exception */ - private function writeOverrideContentType(XMLWriter $objWriter, string $pPartname = '', string $pContentType = ''): void + protected function writeOverrideContentType(XMLWriter $objWriter, string $pPartname, string $pContentType): void { - if ('' == $pPartname || '' == $pContentType) { - throw new \Exception('Invalid parameters passed.'); - } // Write content type $objWriter->startElement('Override'); $objWriter->writeAttribute('PartName', $pPartname); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsApp.php b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsApp.php index 4df4d6a55..fe81c5086 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsApp.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsApp.php @@ -20,16 +20,12 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; class DocPropsApp extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCore.php b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCore.php index 9aef8a6c7..f1afb28df 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCore.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCore.php @@ -20,16 +20,12 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; class DocPropsCore extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCustom.php b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCustom.php index e80b8fd0e..ea651ac40 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCustom.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsCustom.php @@ -20,17 +20,13 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\DocumentProperties; class DocPropsCustom extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { // Variables $pId = 0; diff --git a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsThumbnail.php b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsThumbnail.php index b9ec15199..6c54be1ec 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/DocPropsThumbnail.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/DocPropsThumbnail.php @@ -20,14 +20,11 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; + class DocPropsThumbnail extends AbstractDecoratorWriter { - /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception - */ - public function render() + public function render(): ZipInterface { $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath(); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php index 125bf1342..85dbc97d1 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptCharts.php @@ -23,6 +23,8 @@ use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\Common\XMLWriter; +use PhpOffice\PhpPresentation\Exception\FileRemoveException; +use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; @@ -47,7 +49,7 @@ class PptCharts extends AbstractDecoratorWriter { /** - * @throws \Exception + * @throws FileRemoveException */ public function render(): ZipInterface { @@ -63,7 +65,7 @@ public function render(): ZipInterface // remove temp file if (false === @unlink($pFilename)) { - throw new \Exception('The file ' . $pFilename . ' could not removed.'); + throw new FileRemoveException($pFilename); } } } @@ -76,10 +78,8 @@ public function render(): ZipInterface * Write chart to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeChart(Chart $chart): string + protected function writeChart(Chart $chart): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -230,15 +230,10 @@ public function writeChart(Chart $chart): string * * @return string String output * - * @throws \Exception + * @throws FileRemoveException */ - public function writeSpreadsheet(PhpPresentation $presentation, Chart $chart, string $tempName): string + protected function writeSpreadsheet(PhpPresentation $presentation, Chart $chart, string $tempName): string { - // Need output? - if (!$chart->hasIncludedSpreadsheet()) { - throw new \Exception('No spreadsheet output is required for the given chart.'); - } - // Create new spreadsheet $spreadsheet = new Spreadsheet(); @@ -288,7 +283,7 @@ public function writeSpreadsheet(PhpPresentation $presentation, Chart $chart, st // Load file in memory $returnValue = file_get_contents($tempName); if (false === @unlink($tempName)) { - throw new \Exception('The file ' . $tempName . ' could not removed.'); + throw new FileRemoveException($tempName); } return $returnValue; @@ -402,11 +397,7 @@ protected function writeMultipleValuesOrReference(XMLWriter $objWriter, bool $is } /** - * Write Title. - * - * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * Write Title */ protected function writeTitle(XMLWriter $objWriter, Title $subject): void { @@ -504,7 +495,7 @@ protected function writeTitle(XMLWriter $objWriter, Title $subject): void * * @param XMLWriter $objWriter XML Writer * - * @throws \Exception + * @throws UndefinedChartTypeException */ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart $chart): void { @@ -535,7 +526,7 @@ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart } elseif ($chartType instanceof Scatter) { $this->writeTypeScatter($objWriter, $chartType, $chart->hasIncludedSpreadsheet()); } else { - throw new \Exception('The chart type provided could not be rendered.'); + throw new UndefinedChartTypeException(); } // Write X axis? @@ -556,8 +547,6 @@ protected function writePlotArea(XMLWriter $objWriter, PlotArea $subject, Chart * * @param XMLWriter $objWriter XML Writer * @param Chart\Legend $subject - * - * @throws \Exception */ protected function writeLegend(XMLWriter $objWriter, Legend $subject): void { @@ -656,8 +645,6 @@ protected function writeLegend(XMLWriter $objWriter, Legend $subject): void * * @param XMLWriter $objWriter XML Writer * @param Legend|PlotArea|Title $subject - * - * @throws \Exception */ protected function writeLayout(XMLWriter $objWriter, $subject): void { @@ -713,8 +700,7 @@ protected function writeLayout(XMLWriter $objWriter, $subject): void * * @param XMLWriter $objWriter XML Writer * @param Chart\Type\Area $subject - * - * @throws \Exception + * @param bool $includeSheet */ protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $includeSheet = false): void { @@ -816,8 +802,7 @@ protected function writeTypeArea(XMLWriter $objWriter, Area $subject, bool $incl * * @param XMLWriter $objWriter XML Writer * @param Chart\Type\Bar $subject - * - * @throws \Exception + * @param bool $includeSheet */ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includeSheet = false): void { @@ -1029,8 +1014,7 @@ protected function writeTypeBar(XMLWriter $objWriter, Bar $subject, bool $includ * * @param XMLWriter $objWriter XML Writer * @param Chart\Type\Bar3D $subject - * - * @throws \Exception + * @param bool $includeSheet */ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $includeSheet = false): void { @@ -1222,8 +1206,8 @@ protected function writeTypeBar3D(XMLWriter $objWriter, Bar3D $subject, bool $in * Write Type Pie. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param Doughnut $subject + * @param bool $includeSheet */ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bool $includeSheet = false): void { @@ -1381,8 +1365,8 @@ protected function writeTypeDoughnut(XMLWriter $objWriter, Doughnut $subject, bo * Write Type Pie. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param Pie $subject + * @param bool $includeSheet */ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includeSheet = false): void { @@ -1546,8 +1530,8 @@ protected function writeTypePie(XMLWriter $objWriter, Pie $subject, bool $includ * Write Type Pie3D. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param Pie3D $subject + * @param bool $includeSheet */ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $includeSheet = false): void { @@ -1705,8 +1689,8 @@ protected function writeTypePie3D(XMLWriter $objWriter, Pie3D $subject, bool $in * Write Type Line. * * @param XMLWriter $objWriter XML Writer - * - * @throws \Exception + * @param Line $subject + * @param bool $includeSheet */ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $includeSheet = false): void { @@ -1876,8 +1860,6 @@ protected function writeTypeLine(XMLWriter $objWriter, Line $subject, bool $incl * @param XMLWriter $objWriter XML Writer * @param Radar $subject * @param bool $includeSheet - * - * @throws \Exception */ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $includeSheet = false): void { @@ -2046,7 +2028,9 @@ protected function writeTypeRadar(XMLWriter $objWriter, Radar $subject, bool $in /** * Write Type Scatter * - * @throws \Exception + * @param XMLWriter $objWriter + * @param Scatter $subject + * @param bool $includeSheet */ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool $includeSheet = false): void { @@ -2222,11 +2206,11 @@ protected function writeTypeScatter(XMLWriter $objWriter, Scatter $subject, bool /** * Write chart relationships to XML format. * - * @return string XML Output + * @param Chart $pChart * - * @throws \Exception + * @return string XML Output */ - public function writeChartRelationships(Chart $pChart): string + protected function writeChartRelationships(Chart $pChart): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -2249,6 +2233,10 @@ public function writeChartRelationships(Chart $pChart): string return $objWriter->getData(); } + /** + * @param XMLWriter $objWriter + * @param Chart\Marker $marker + */ protected function writeSeriesMarker(XMLWriter $objWriter, Chart\Marker $marker): void { // c:marker @@ -2289,7 +2277,10 @@ protected function writeSeriesMarker(XMLWriter $objWriter, Chart\Marker $marker) } /** - * @throws \Exception + * @param XMLWriter $objWriter + * @param Chart\Axis $oAxis + * @param string $typeAxis + * @param Chart\Type\AbstractType $typeChart */ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $typeAxis, Chart\Type\AbstractType $typeChart): void { @@ -2540,7 +2531,8 @@ protected function writeAxis(XMLWriter $objWriter, Chart\Axis $oAxis, string $ty } /** - * @throws \Exception + * @param XMLWriter $objWriter + * @param Gridlines $oGridlines */ protected function writeAxisGridlines(XMLWriter $objWriter, Gridlines $oGridlines): void { diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptComments.php b/src/PhpPresentation/Writer/PowerPoint2007/PptComments.php index 060fec0bf..5a30c08a4 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptComments.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptComments.php @@ -30,10 +30,8 @@ class PptComments extends AbstractDecoratorWriter { /** * @return ZipInterface - * - * @throws \Exception */ - public function render() + public function render(): ZipInterface { foreach ($this->getPresentation()->getAllSlides() as $numSlide => $oSlide) { $contentXml = $this->writeSlideComments($oSlide); @@ -49,7 +47,7 @@ public function render() /** * @return string */ - protected function writeSlideComments(Slide $oSlide) + protected function writeSlideComments(Slide $oSlide): string { /** * @var Comment[] diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php b/src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php index bbbe22f9f..85a7acecc 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php @@ -20,16 +20,15 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter; class PptMedia extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) { $shape = $this->getDrawingHashTable()->getByIndex($i); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptPresProps.php b/src/PhpPresentation/Writer/PowerPoint2007/PptPresProps.php index 10585295b..c28f12d22 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptPresProps.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptPresProps.php @@ -20,16 +20,15 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; class PptPresProps extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { $presentationPpts = $this->oPresentation->getPresentationProperties(); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptPresentation.php b/src/PhpPresentation/Writer/PowerPoint2007/PptPresentation.php index 63c138c2e..a6aa3c133 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptPresentation.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptPresentation.php @@ -20,17 +20,16 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\DocumentLayout; class PptPresentation extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptSlideLayouts.php b/src/PhpPresentation/Writer/PowerPoint2007/PptSlideLayouts.php index b9730e3aa..b482579d7 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptSlideLayouts.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptSlideLayouts.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Slide; @@ -30,11 +31,9 @@ class PptSlideLayouts extends AbstractSlide { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { foreach ($this->oPresentation->getAllMasterSlides() as $oSlideMaster) { foreach ($oSlideMaster->getAllSlideLayouts() as $oSlideLayout) { @@ -56,10 +55,8 @@ public function render() * Write slide layout relationships to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeSlideLayoutRelationships(SlideLayout $oSlideLayout) + protected function writeSlideLayoutRelationships(SlideLayout $oSlideLayout): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -98,10 +95,8 @@ public function writeSlideLayoutRelationships(SlideLayout $oSlideLayout) * Write slide to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeSlideLayout(SlideLayout $pSlideLayout) + protected function writeSlideLayout(SlideLayout $pSlideLayout): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php b/src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php index 5db12b952..a7758a898 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptSlideMasters.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Shape\RichText; @@ -30,11 +31,9 @@ class PptSlideMasters extends AbstractSlide { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { foreach ($this->oPresentation->getAllMasterSlides() as $oMasterSlide) { // Add the relations from the masterSlide to the ZIP file @@ -55,13 +54,11 @@ public function render() /** * Write slide master relationships to XML format. * - * @return string XML Output - * - * @throws \Exception + * @todo Set method in protected * - * @internal param int $masterId Master slide id + * @return string XML Output */ - public function writeSlideMasterRelationships(SlideMaster $oMasterSlide) + public function writeSlideMasterRelationships(SlideMaster $oMasterSlide): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -104,10 +101,8 @@ public function writeSlideMasterRelationships(SlideMaster $oMasterSlide) * Write slide to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeSlideMaster(SlideMaster $pSlide) + protected function writeSlideMaster(SlideMaster $pSlide): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php b/src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php index 7ef785e34..8c2999d04 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\Drawing as CommonDrawing; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Shape\Chart as ShapeChart; @@ -40,11 +41,9 @@ class PptSlides extends AbstractSlide /** * Add slides (drawings, ...) and slide relationships (drawings, ...). * - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { foreach ($this->oPresentation->getAllSlides() as $idx => $oSlide) { $this->oZip->addFromString('ppt/slides/_rels/slide' . ($idx + 1) . '.xml.rels', $this->writeSlideRelationships($oSlide)); @@ -71,10 +70,8 @@ public function render() * Write slide relationships to XML format. * * @return string XML Output - * - * @throws \Exception */ - protected function writeSlideRelationships(Slide $pSlide) + protected function writeSlideRelationships(Slide $pSlide): string { //@todo Group all getShapeCollection()->getIterator @@ -369,10 +366,8 @@ protected function writeSlideRelationships(Slide $pSlide) * Write slide to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeSlide(Slide $pSlide): string + protected function writeSlide(Slide $pSlide): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptTableProps.php b/src/PhpPresentation/Writer/PowerPoint2007/PptTableProps.php index c567721ef..ff2c5f851 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptTableProps.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptTableProps.php @@ -20,16 +20,15 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; class PptTableProps extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptTheme.php b/src/PhpPresentation/Writer/PowerPoint2007/PptTheme.php index efc07a618..c2c9e8e9b 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptTheme.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptTheme.php @@ -20,17 +20,16 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Slide; class PptTheme extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { foreach ($this->oPresentation->getAllMasterSlides() as $oMasterSlide) { $this->getZip()->addFromString('ppt/theme/theme' . $oMasterSlide->getRelsIndex() . '.xml', $this->writeTheme($oMasterSlide)); @@ -44,7 +43,7 @@ public function render() * * @return string XML Output */ - protected function writeTheme(Slide\SlideMaster $oMasterSlide) + protected function writeTheme(Slide\SlideMaster $oMasterSlide): string { $arrayFont = [ 'Jpan' => 'MS Pゴシック', diff --git a/src/PhpPresentation/Writer/PowerPoint2007/PptViewProps.php b/src/PhpPresentation/Writer/PowerPoint2007/PptViewProps.php index 1871fb31b..2a8947384 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/PptViewProps.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/PptViewProps.php @@ -20,16 +20,15 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; class PptViewProps extends AbstractDecoratorWriter { /** - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/PowerPoint2007/Relationships.php b/src/PhpPresentation/Writer/PowerPoint2007/Relationships.php index 34f70a978..9069ed485 100644 --- a/src/PhpPresentation/Writer/PowerPoint2007/Relationships.php +++ b/src/PhpPresentation/Writer/PowerPoint2007/Relationships.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007; +use PhpOffice\Common\Adapter\Zip\ZipInterface; use PhpOffice\Common\XMLWriter; use PhpOffice\PhpPresentation\Shape\Comment; use PhpOffice\PhpPresentation\Shape\Comment\Author; @@ -29,11 +30,9 @@ class Relationships extends AbstractDecoratorWriter /** * Add relationships to ZIP file. * - * @return \PhpOffice\Common\Adapter\Zip\ZipInterface - * - * @throws \Exception + * @return ZipInterface */ - public function render() + public function render(): ZipInterface { $this->getZip()->addFromString('_rels/.rels', $this->writeRelationships()); $this->getZip()->addFromString('ppt/_rels/presentation.xml.rels', $this->writePresentationRelationships()); @@ -45,10 +44,8 @@ public function render() * Write relationships to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writeRelationships() + protected function writeRelationships(): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); @@ -91,10 +88,8 @@ public function writeRelationships() * Write presentation relationships to XML format. * * @return string XML Output - * - * @throws \Exception */ - public function writePresentationRelationships() + protected function writePresentationRelationships(): string { // Create XML writer $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY); diff --git a/src/PhpPresentation/Writer/Serialized.php b/src/PhpPresentation/Writer/Serialized.php index 8d04d9423..5482c1e2e 100644 --- a/src/PhpPresentation/Writer/Serialized.php +++ b/src/PhpPresentation/Writer/Serialized.php @@ -22,6 +22,8 @@ use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; use PhpOffice\Common\XMLWriter; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Shape\Drawing\AbstractDrawingAdapter; use PhpOffice\PhpPresentation\Shape\Drawing\File; @@ -35,13 +37,11 @@ class Serialized extends AbstractWriter implements WriterInterface * Create a new \PhpOffice\PhpPresentation\Writer\Serialized. * * @param \PhpOffice\PhpPresentation\PhpPresentation $pPhpPresentation - * - * @throws \Exception */ public function __construct(PhpPresentation $pPhpPresentation = null) { // Set PhpPresentation - $this->setPhpPresentation($pPhpPresentation); + $this->setPhpPresentation($pPhpPresentation ?? new PhpPresentation()); // Set ZIP Adapter $this->setZipAdapter(new ZipArchiveAdapter()); @@ -50,15 +50,16 @@ public function __construct(PhpPresentation $pPhpPresentation = null) /** * Save PhpPresentation to file. * - * @throws \Exception + * @throws DirectoryNotFoundException + * @throws InvalidParameterException */ public function save(string $pFilename): void { if (empty($pFilename)) { - throw new \Exception('Filename is empty.'); + throw new InvalidParameterException('pFilename', ''); } if (!is_dir(dirname($pFilename))) { - throw new \Exception(sprintf('Could not open %s for writing.', $pFilename)); + throw new DirectoryNotFoundException(dirname($pFilename)); } $oPresentation = $this->getPhpPresentation(); @@ -92,14 +93,12 @@ public function save(string $pFilename): void /** * Serialize PhpPresentation object to XML. * - * @param PhpPresentation $pPhpPresentation + * @param PhpPresentation|null $pPhpPresentation * @param string $pFilename * * @return string XML Output - * - * @throws \Exception */ - private function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') + protected function writeSerialized(PhpPresentation $pPhpPresentation = null, $pFilename = '') { // Clone $pPhpPresentation $pPhpPresentation = clone $pPhpPresentation; diff --git a/src/PhpPresentation/Writer/WriterInterface.php b/src/PhpPresentation/Writer/WriterInterface.php index 55112facb..ab64192e1 100644 --- a/src/PhpPresentation/Writer/WriterInterface.php +++ b/src/PhpPresentation/Writer/WriterInterface.php @@ -26,9 +26,7 @@ interface WriterInterface { /** - * Save PhpPresentation to file. - * - * @throws \Exception + * Save PhpPresentation to file */ public function save(string $pFilename): void; } diff --git a/tests/PhpPresentation/Tests/AbstractShapeTest.php b/tests/PhpPresentation/Tests/AbstractShapeTest.php index 3b0c0eb23..8cb097b2d 100644 --- a/tests/PhpPresentation/Tests/AbstractShapeTest.php +++ b/tests/PhpPresentation/Tests/AbstractShapeTest.php @@ -21,6 +21,7 @@ namespace PhpOffice\PhpPresentation\Tests; use PhpOffice\PhpPresentation\AbstractShape; +use PhpOffice\PhpPresentation\Exception\ShapeContainerAlreadyAssignedException; use PhpOffice\PhpPresentation\Shape\Hyperlink; use PhpOffice\PhpPresentation\Shape\Placeholder; use PhpOffice\PhpPresentation\Shape\RichText; @@ -204,8 +205,8 @@ public function testContainerException(): void $this->assertNull($object->getContainer()); $this->assertInstanceOf(AbstractShape::class, $object->setContainer($oSlide)); $this->assertInstanceOf(Slide::class, $object->getContainer()); - $this->expectException(\Exception::class); - $this->expectExceptionMessage('A PhpOffice\PhpPresentation\ShapeContainerInterface has already been assigned. Shapes can only exist on one PhpOffice\PhpPresentation\ShapeContainerInterface.'); + $this->expectException(ShapeContainerAlreadyAssignedException::class); + $this->expectExceptionMessage('The shape PhpOffice\PhpPresentation\AbstractShape has already a container assigned'); $object->setContainer(null); } } diff --git a/tests/PhpPresentation/Tests/IOFactoryTest.php b/tests/PhpPresentation/Tests/IOFactoryTest.php index 223fc5532..d17f61548 100644 --- a/tests/PhpPresentation/Tests/IOFactoryTest.php +++ b/tests/PhpPresentation/Tests/IOFactoryTest.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests; +use PhpOffice\PhpPresentation\Exception\InvalidClassException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\IOFactory; use PhpOffice\PhpPresentation\PhpPresentation; use PHPUnit\Framework\TestCase; @@ -56,8 +58,8 @@ public function testCreateReader(): void */ public function testLoadClassException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('"" is not a valid reader'); + $this->expectException(InvalidClassException::class); + $this->expectExceptionMessage('The class PhpOffice\PhpPresentation\Reader\ is invalid (Reader: The class doesn\'t exist)'); IOFactory::createReader(''); } @@ -71,8 +73,12 @@ public function testLoad(): void */ public function testLoadException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not automatically determine \PhpOffice\PhpPresentation\Reader\ReaderInterface for file.'); - IOFactory::load(PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png'); + $file = PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png'; + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\IOFactory (Could not automatically determine the good PhpOffice\PhpPresentation\Reader\ReaderInterface)', + $file + )); + IOFactory::load($file); } } diff --git a/tests/PhpPresentation/Tests/PhpPresentationTest.php b/tests/PhpPresentation/Tests/PhpPresentationTest.php index 8d5acc2a0..3370f9246 100644 --- a/tests/PhpPresentation/Tests/PhpPresentationTest.php +++ b/tests/PhpPresentation/Tests/PhpPresentationTest.php @@ -22,6 +22,7 @@ use PhpOffice\PhpPresentation\DocumentLayout; use PhpOffice\PhpPresentation\DocumentProperties; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\PresentationProperties; use PHPUnit\Framework\TestCase; @@ -94,8 +95,8 @@ public function testCopy(): void */ public function testRemoveSlideByIndexException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Slide index is out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new PhpPresentation(); $object->removeSlideByIndex(1); @@ -106,8 +107,8 @@ public function testRemoveSlideByIndexException(): void */ public function testGetSlideException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Slide index is out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new PhpPresentation(); $object->getSlide(1); @@ -118,8 +119,8 @@ public function testGetSlideException(): void */ public function testSetActiveSlideIndexException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Active slide index is out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new PhpPresentation(); $object->setActiveSlideIndex(1); diff --git a/tests/PhpPresentation/Tests/Reader/ODPresentationTest.php b/tests/PhpPresentation/Tests/Reader/ODPresentationTest.php index ddbfdf03f..418215bbf 100644 --- a/tests/PhpPresentation/Tests/Reader/ODPresentationTest.php +++ b/tests/PhpPresentation/Tests/Reader/ODPresentationTest.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Reader; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PresentationProperties; use PhpOffice\PhpPresentation\Reader\ODPresentation; use PhpOffice\PhpPresentation\Shape\Drawing\Gd; @@ -59,8 +61,8 @@ public function testCanRead(): void public function testLoadFileNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new ODPresentation(); $object->load(''); @@ -68,18 +70,21 @@ public function testLoadFileNotExists(): void public function testLoadFileBadFormat(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid file format for PhpOffice\PhpPresentation\Reader\ODPresentation:'); - $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_00_01.ppt'; + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\Reader\ODPresentation', + $file + )); + $object = new ODPresentation(); $object->load($file); } public function testFileSupportsNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new ODPresentation(); $object->fileSupportsUnserializePhpPresentation(''); diff --git a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php index 57c5133ec..7db18c9e1 100644 --- a/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php +++ b/tests/PhpPresentation/Tests/Reader/PowerPoint2007Test.php @@ -21,6 +21,8 @@ namespace PhpOffice\PhpPresentation\Tests\Reader; use PhpOffice\PhpPresentation\DocumentLayout; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\PresentationProperties; use PhpOffice\PhpPresentation\Reader\PowerPoint2007; use PhpOffice\PhpPresentation\Shape\Drawing\Gd; @@ -57,8 +59,8 @@ public function testCanRead(): void public function testLoadFileNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new PowerPoint2007(); $object->load(''); @@ -66,18 +68,21 @@ public function testLoadFileNotExists(): void public function testLoadFileBadFormat(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid file format for PhpOffice\PhpPresentation\Reader\PowerPoint2007:'); - $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_00_01.ppt'; + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\Reader\PowerPoint2007', + $file + )); + $object = new PowerPoint2007(); $object->load($file); } public function testFileSupportsNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new PowerPoint2007(); $object->fileSupportsUnserializePhpPresentation(''); diff --git a/tests/PhpPresentation/Tests/Reader/PowerPoint97Test.php b/tests/PhpPresentation/Tests/Reader/PowerPoint97Test.php index 87c9c403c..73cfb5ab6 100644 --- a/tests/PhpPresentation/Tests/Reader/PowerPoint97Test.php +++ b/tests/PhpPresentation/Tests/Reader/PowerPoint97Test.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Reader; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\Reader\PowerPoint97; use PHPUnit\Framework\TestCase; @@ -54,8 +56,8 @@ public function testCantRead(): void public function testLoadFileNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new PowerPoint97(); $object->load(''); @@ -63,18 +65,21 @@ public function testLoadFileNotExists(): void public function testLoadFileBadFormat(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid file format for PhpOffice\PhpPresentation\Reader\PowerPoint97:'); - $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_01_Simple.pptx'; + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\Reader\PowerPoint97', + $file + )); + $object = new PowerPoint97(); $object->load($file); } public function testFileSupportsNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new PowerPoint97(); $object->fileSupportsUnserializePhpPresentation(''); diff --git a/tests/PhpPresentation/Tests/Reader/SerializedTest.php b/tests/PhpPresentation/Tests/Reader/SerializedTest.php index f1d057cd0..a4d79d0a7 100644 --- a/tests/PhpPresentation/Tests/Reader/SerializedTest.php +++ b/tests/PhpPresentation/Tests/Reader/SerializedTest.php @@ -20,8 +20,11 @@ namespace PhpOffice\PhpPresentation\Tests\Reader; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException; use PhpOffice\PhpPresentation\Reader\Serialized; use PHPUnit\Framework\TestCase; +use ZipArchive; /** * Test class for serialized reader. @@ -43,8 +46,8 @@ public function testCanRead(): void public function testLoadFileNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new Serialized(); $object->load(''); @@ -52,18 +55,21 @@ public function testLoadFileNotExists(): void public function testLoadFileBadFormat(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid file format for PhpOffice\PhpPresentation\Reader\Serialized:'); - $file = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/files/Sample_01_Simple.pptx'; + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\Reader\Serialized', + $file + )); + $object = new Serialized(); $object->load($file); } public function testFileSupportsNotExists(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open for reading! File does not exist.'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new Serialized(); $object->fileSupportsUnserializePhpPresentation(''); @@ -73,11 +79,14 @@ public function testLoadSerializedFileNotExists(): void { $file = tempnam(sys_get_temp_dir(), 'PhpPresentation_Serialized'); - $this->expectException(\Exception::class); - $this->expectExceptionMessage(sprintf('The file PhpPresentation.xml in the serialized file %s is malformed', $file)); + $this->expectException(InvalidFileFormatException::class); + $this->expectExceptionMessage(sprintf( + 'The file %s is not in the format supported by class PhpOffice\PhpPresentation\Reader\Serialized (The file PhpPresentation.xml is malformed)', + $file + )); - $oArchive = new \ZipArchive(); - $oArchive->open($file, \ZipArchive::CREATE); + $oArchive = new ZipArchive(); + $oArchive->open($file, ZipArchive::CREATE); $oArchive->addFromString('PhpPresentation.xml', ''); $oArchive->close(); diff --git a/tests/PhpPresentation/Tests/Shape/Chart/PlotAreaTest.php b/tests/PhpPresentation/Tests/Shape/Chart/PlotAreaTest.php index 5c2fdc898..42c6a193b 100644 --- a/tests/PhpPresentation/Tests/Shape/Chart/PlotAreaTest.php +++ b/tests/PhpPresentation/Tests/Shape/Chart/PlotAreaTest.php @@ -20,7 +20,10 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Chart; +use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException; +use PhpOffice\PhpPresentation\Shape\Chart\Axis; use PhpOffice\PhpPresentation\Shape\Chart\PlotArea; +use PhpOffice\PhpPresentation\Shape\Chart\Type\AbstractType; use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D; use PHPUnit\Framework\TestCase; @@ -35,8 +38,8 @@ public function testConstruct(): void { $object = new PlotArea(); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->getAxisX()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Axis', $object->getAxisY()); + $this->assertInstanceOf(Axis::class, $object->getAxisX()); + $this->assertInstanceOf(Axis::class, $object->getAxisY()); } public function testHashIndex(): void @@ -45,7 +48,7 @@ public function testHashIndex(): void $value = mt_rand(1, 100); $this->assertEmpty($object->getHashIndex()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setHashIndex($value)); + $this->assertInstanceOf(PlotArea::class, $object->setHashIndex($value)); $this->assertEquals($value, $object->getHashIndex()); } @@ -54,9 +57,9 @@ public function testHeight(): void $object = new PlotArea(); $value = mt_rand(0, 100); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setHeight()); + $this->assertInstanceOf(PlotArea::class, $object->setHeight()); $this->assertEquals(0, $object->getHeight()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setHeight($value)); + $this->assertInstanceOf(PlotArea::class, $object->setHeight($value)); $this->assertEquals($value, $object->getHeight()); } @@ -65,9 +68,9 @@ public function testOffsetX(): void $object = new PlotArea(); $value = mt_rand(0, 100); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setOffsetX()); + $this->assertInstanceOf(PlotArea::class, $object->setOffsetX()); $this->assertEquals(0, $object->getOffsetX()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setOffsetX($value)); + $this->assertInstanceOf(PlotArea::class, $object->setOffsetX($value)); $this->assertEquals($value, $object->getOffsetX()); } @@ -76,9 +79,9 @@ public function testOffsetY(): void $object = new PlotArea(); $value = mt_rand(0, 100); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setOffsetY()); + $this->assertInstanceOf(PlotArea::class, $object->setOffsetY()); $this->assertEquals(0, $object->getOffsetY()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setOffsetY($value)); + $this->assertInstanceOf(PlotArea::class, $object->setOffsetY($value)); $this->assertEquals($value, $object->getOffsetY()); } @@ -86,14 +89,14 @@ public function testType(): void { $object = new PlotArea(); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setType(new Bar3D())); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\Type\\AbstractType', $object->getType()); + $this->assertInstanceOf(PlotArea::class, $object->setType(new Bar3D())); + $this->assertInstanceOf(AbstractType::class, $object->getType()); } public function testTypeException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Chart type has not been set.'); + $this->expectException(UndefinedChartTypeException::class); + $this->expectExceptionMessage('The chart type has not been defined'); $object = new PlotArea(); $object->getType(); @@ -104,9 +107,9 @@ public function testWidth(): void $object = new PlotArea(); $value = mt_rand(0, 100); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setWidth()); + $this->assertInstanceOf(PlotArea::class, $object->setWidth()); $this->assertEquals(0, $object->getWidth()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Chart\\PlotArea', $object->setWidth($value)); + $this->assertInstanceOf(PlotArea::class, $object->setWidth($value)); $this->assertEquals($value, $object->getWidth()); } } diff --git a/tests/PhpPresentation/Tests/Shape/Drawing/Base64Test.php b/tests/PhpPresentation/Tests/Shape/Drawing/Base64Test.php index 916a1b452..c73b8af44 100644 --- a/tests/PhpPresentation/Tests/Shape/Drawing/Base64Test.php +++ b/tests/PhpPresentation/Tests/Shape/Drawing/Base64Test.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Drawing; +use PhpOffice\PhpPresentation\Exception\UnauthorizedMimetypeException; use PhpOffice\PhpPresentation\Shape\Drawing\Base64; use PHPUnit\Framework\TestCase; @@ -63,8 +64,8 @@ public function testExtension(): void public function testExtensionException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Type Mime not found : "fake/fake"'); + $this->expectException(UnauthorizedMimetypeException::class); + $this->expectExceptionMessage('The mime type fake/fake is not found in autorized values (jpg, png, gif, svg)'); $imgData = str_replace('image/jpeg', 'fake/fake', $this->imageDataPNG); diff --git a/tests/PhpPresentation/Tests/Shape/Drawing/FileTest.php b/tests/PhpPresentation/Tests/Shape/Drawing/FileTest.php index 81744cb21..479f64c1a 100644 --- a/tests/PhpPresentation/Tests/Shape/Drawing/FileTest.php +++ b/tests/PhpPresentation/Tests/Shape/Drawing/FileTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Drawing; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Shape\Drawing\File; use PHPUnit\Framework\TestCase; @@ -38,8 +39,8 @@ public function testConstruct(): void public function testPathBasic(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('File not found!'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "" doesn\'t exist'); $object = new File(); $this->assertInstanceOf(File::class, $object->setPath()); diff --git a/tests/PhpPresentation/Tests/Shape/Drawing/ZipFileTest.php b/tests/PhpPresentation/Tests/Shape/Drawing/ZipFileTest.php index 5bc76abb3..3a5a7fbb2 100644 --- a/tests/PhpPresentation/Tests/Shape/Drawing/ZipFileTest.php +++ b/tests/PhpPresentation/Tests/Shape/Drawing/ZipFileTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Drawing; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Shape\Drawing\ZipFile; use PHPUnit\Framework\TestCase; @@ -56,8 +57,11 @@ protected function setUp(): void public function testContentsException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('fileNotExist.pptx does not exist'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The file "%s" doesn\'t exist', + str_replace(['zip://', '#ppt/media/phppowerpoint_logo1.gif'], '', $this->fileKoZip) + )); $oDrawing = new ZipFile(); $oDrawing->setPath($this->fileKoZip); diff --git a/tests/PhpPresentation/Tests/Shape/RichTextTest.php b/tests/PhpPresentation/Tests/Shape/RichTextTest.php index c1b78f905..7019fc292 100644 --- a/tests/PhpPresentation/Tests/Shape/RichTextTest.php +++ b/tests/PhpPresentation/Tests/Shape/RichTextTest.php @@ -20,8 +20,11 @@ namespace PhpOffice\PhpPresentation\Tests\Shape; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\RichText; +use PhpOffice\PhpPresentation\Shape\RichText\BreakElement; use PhpOffice\PhpPresentation\Shape\RichText\Paragraph; +use PhpOffice\PhpPresentation\Shape\RichText\Run; use PhpOffice\PhpPresentation\Shape\RichText\TextElement; use PHPUnit\Framework\TestCase; @@ -56,8 +59,8 @@ public function testActiveParagraph(): void public function testActiveParagraphException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid paragraph count.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1000) is out of bounds (0, 1)'); $object = new RichText(); $object->setActiveParagraph(1000); @@ -65,8 +68,8 @@ public function testActiveParagraphException(): void public function testGetParagraphException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid paragraph count.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1000) is out of bounds (0, 1)'); $object = new RichText(); $object->getParagraph(1000); @@ -95,8 +98,8 @@ public function testColumnSpacing(): void public function testColumnsException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Number of columns should be 1-16'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1000) is out of bounds (1, 16)'); $object = new RichText(); $object->setColumns(1000); @@ -124,15 +127,15 @@ public function testText(): void $this->assertCount(1, $object->getActiveParagraph()->getRichTextElements()); $this->assertInstanceOf(RichText::class, $object->addText(new TextElement())); $this->assertCount(2, $object->getActiveParagraph()->getRichTextElements()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText()); + $this->assertInstanceOf(TextElement::class, $object->createText()); $this->assertCount(3, $object->getActiveParagraph()->getRichTextElements()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\TextElement', $object->createText('ALPHA')); + $this->assertInstanceOf(TextElement::class, $object->createText('ALPHA')); $this->assertCount(4, $object->getActiveParagraph()->getRichTextElements()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\BreakElement', $object->createBreak()); + $this->assertInstanceOf(BreakElement::class, $object->createBreak()); $this->assertCount(5, $object->getActiveParagraph()->getRichTextElements()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun()); + $this->assertInstanceOf(Run::class, $object->createTextRun()); $this->assertCount(6, $object->getActiveParagraph()->getRichTextElements()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\RichText\\Run', $object->createTextRun('BETA')); + $this->assertInstanceOf(Run::class, $object->createTextRun('BETA')); $this->assertCount(7, $object->getActiveParagraph()->getRichTextElements()); $this->assertEquals('ALPHA' . "\r\n" . 'BETA', $object->getPlainText()); $this->assertEquals('ALPHA' . "\r\n" . 'BETA', (string) $object); diff --git a/tests/PhpPresentation/Tests/Shape/Table/CellTest.php b/tests/PhpPresentation/Tests/Shape/Table/CellTest.php index 017fb61b6..7f0ccc502 100644 --- a/tests/PhpPresentation/Tests/Shape/Table/CellTest.php +++ b/tests/PhpPresentation/Tests/Shape/Table/CellTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Table; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\RichText\Paragraph; use PhpOffice\PhpPresentation\Shape\RichText\TextElement; use PhpOffice\PhpPresentation\Shape\Table\Cell; @@ -68,8 +69,8 @@ public function testActiveParagraph(): void public function testActiveParagraphException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid paragraph count.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1000) is out of bounds (0, 1)'); $object = new Cell(); $object->setActiveParagraph(1000); @@ -77,8 +78,8 @@ public function testActiveParagraphException(): void public function testGetParagraphException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid paragraph count.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1000) is out of bounds (0, 1)'); $object = new Cell(); $object->getParagraph(1000); diff --git a/tests/PhpPresentation/Tests/Shape/Table/RowTest.php b/tests/PhpPresentation/Tests/Shape/Table/RowTest.php index 2dce4c8d1..2e8ed0595 100644 --- a/tests/PhpPresentation/Tests/Shape/Table/RowTest.php +++ b/tests/PhpPresentation/Tests/Shape/Table/RowTest.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Shape\Table; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; +use PhpOffice\PhpPresentation\Shape\Table\Cell; use PhpOffice\PhpPresentation\Shape\Table\Row; use PhpOffice\PhpPresentation\Style\Fill; use PHPUnit\Framework\TestCase; @@ -38,26 +40,25 @@ public function testConstruct(): void { $object = new Row(); $this->assertCount(1, $object->getCells()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill()); + $this->assertInstanceOf(Fill::class, $object->getFill()); $value = mt_rand(1, 100); $object = new Row($value); $this->assertCount($value, $object->getCells()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill()); + $this->assertInstanceOf(Fill::class, $object->getFill()); } public function testGetCell(): void { $object = new Row(); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->getCell(0)); - $this->assertNull($object->getCell(1000, true)); + $this->assertInstanceOf(Cell::class, $object->getCell(0)); } public function testGetCellException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Cell number out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new Row(); $object->getCell(1); @@ -67,13 +68,13 @@ public function testNextCell(): void { $object = new Row(2); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Cell', $object->nextCell()); + $this->assertInstanceOf(Cell::class, $object->nextCell()); } public function testNextCellException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Cell count out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new Row(); $object->nextCell(); @@ -96,7 +97,7 @@ public function testGetSetFill(): void $object = new Row(); $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Row', $object->setFill(new Fill())); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Style\\Fill', $object->getFill()); + $this->assertInstanceOf(Fill::class, $object->getFill()); } public function testGetSetHeight(): void diff --git a/tests/PhpPresentation/Tests/Shape/TableTest.php b/tests/PhpPresentation/Tests/Shape/TableTest.php index 91e803c01..286b151e9 100644 --- a/tests/PhpPresentation/Tests/Shape/TableTest.php +++ b/tests/PhpPresentation/Tests/Shape/TableTest.php @@ -20,7 +20,9 @@ namespace PhpOffice\PhpPresentation\Tests\Shape; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Shape\Table; +use PhpOffice\PhpPresentation\Shape\Table\Row; use PHPUnit\Framework\TestCase; /** @@ -43,7 +45,7 @@ public function testNumColums(): void $object = new Table(); $this->assertEquals(1, $object->getNumColumns()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table', $object->setNumColumns($value)); + $this->assertInstanceOf(Table::class, $object->setNumColumns($value)); $this->assertEquals($value, $object->getNumColumns()); } @@ -51,20 +53,19 @@ public function testRows(): void { $object = new Table(); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Row', $object->createRow()); + $this->assertInstanceOf(Row::class, $object->createRow()); $this->assertCount(1, $object->getRows()); - $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Table\\Row', $object->getRow(0)); - $this->assertNull($object->getRow(1, true)); + $this->assertInstanceOf(Row::class, $object->getRow(0)); } public function testGetRowException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Row number out of bounds.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (1) is out of bounds (0, 0)'); $object = new Table(); - $object->getRow(); + $object->getRow(1); } public function testHashCode(): void diff --git a/tests/PhpPresentation/Tests/Slide/Background/ImageTest.php b/tests/PhpPresentation/Tests/Slide/Background/ImageTest.php index edeeae37c..be9251773 100644 --- a/tests/PhpPresentation/Tests/Slide/Background/ImageTest.php +++ b/tests/PhpPresentation/Tests/Slide/Background/ImageTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Slide\Background; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Slide\Background\Image; use PHPUnit\Framework\TestCase; @@ -52,8 +53,8 @@ public function testColor(): void public function testPathException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('File not found :'); + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage('The file "pathDoesntExist" doesn\'t exist'); $object = new Image(); $object->setPath('pathDoesntExist', true); diff --git a/tests/PhpPresentation/Tests/Style/AlignmentTest.php b/tests/PhpPresentation/Tests/Style/AlignmentTest.php index 2a1a16a90..510811fb4 100644 --- a/tests/PhpPresentation/Tests/Style/AlignmentTest.php +++ b/tests/PhpPresentation/Tests/Style/AlignmentTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Style; +use PhpOffice\PhpPresentation\Exception\OutOfBoundsException; use PhpOffice\PhpPresentation\Style\Alignment; use PHPUnit\Framework\TestCase; @@ -90,8 +91,8 @@ public function testSetGetVertical(): void */ public function testSetGetLevelExceptionMin(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Invalid value should be more than 0.'); + $this->expectException(OutOfBoundsException::class); + $this->expectExceptionMessage('The expected value (-1) is out of bounds (0, Infinite)'); $object = new Alignment(); $object->setLevel(-1); diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaInfManifestTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaInfManifestTest.php index be0019b23..6019d6666 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaInfManifestTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaInfManifestTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Writer\ODPresentation; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Shape\Drawing; use PhpOffice\PhpPresentation\Slide\Background\Image; use PhpOffice\PhpPresentation\Tests\PhpPresentationTestCase; @@ -48,11 +49,15 @@ public function testDrawing(): void public function testDrawingException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('does not exist'); + $path = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png'; + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The file "%s" doesn\'t exist', + $path + )); $oShape = $this->oPresentation->getActiveSlide()->createDrawingShape(); - $oShape->setPath(PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png', false); + $oShape->setPath($path, false); $this->writePresentationFile($this->oPresentation, 'ODPresentation'); } @@ -92,11 +97,15 @@ public function testDrawingZip(): void public function testDrawingZipException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('does not exist'); + $path = PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'filedoesntexist.zip'; + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The file "%s" doesn\'t exist', + $path + )); $oShape = new Drawing\ZipFile(); - $oShape->setPath('zip://' . PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'filedoesntexist.zip'); + $oShape->setPath('zip://' . $path); $this->oPresentation->getActiveSlide()->addShape($oShape); $this->writePresentationFile($this->oPresentation, 'ODPresentation'); diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php index cbcc67dc4..ba8724708 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Writer; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\Shape\Chart\Type\Bar3D; use PhpOffice\PhpPresentation\Tests\PhpPresentationTestCase; use PhpOffice\PhpPresentation\Writer\ODPresentation; @@ -74,25 +76,13 @@ public function testSave(): void */ public function testSaveEmpty(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Filename is empty'); + $this->expectException(InvalidParameterException::class); + $this->expectExceptionMessage('The parameter pFilename can\'t have the value ""'); $object = new ODPresentation(); $object->save(''); } - /** - * Test get PhpPresentation exception. - */ - public function testGetPhpPresentationException(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No PhpPresentation assigned.'); - - $object = new ODPresentation(); - $object->getPhpPresentation(); - } - /** * Test set/get disk caching. */ @@ -111,7 +101,7 @@ public function testSetGetUseDiskCaching(): void */ public function testSetUseDiskCachingException(): void { - $this->expectException(\Exception::class); + $this->expectException(DirectoryNotFoundException::class); $object = new ODPresentation($this->oPresentation); $object->setUseDiskCaching(true, 'foo'); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php index 01bb66e3f..a7a2fb53b 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptChartsTest.php @@ -20,8 +20,8 @@ namespace PhpPresentation\Tests\Writer\PowerPoint2007; -use Exception; use PhpOffice\Common\Drawing; +use PhpOffice\PhpPresentation\Exception\UndefinedChartTypeException; use PhpOffice\PhpPresentation\Shape\Chart; use PhpOffice\PhpPresentation\Shape\Chart\Axis; use PhpOffice\PhpPresentation\Shape\Chart\Gridlines; @@ -176,8 +176,8 @@ public function testChartIncludeSpreadsheet(): void public function testPlotAreaBadType(): void { - $this->expectException(Exception::class); - $this->expectExceptionMessage('The chart type provided could not be rendered'); + $this->expectException(UndefinedChartTypeException::class); + $this->expectExceptionMessage('The chart type has not been defined'); $oSlide = $this->oPresentation->getActiveSlide(); $oShape = $oSlide->createChartShape(); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptMediaTest.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptMediaTest.php index 4029b36b6..ca77f4d33 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptMediaTest.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007/PptMediaTest.php @@ -20,6 +20,7 @@ namespace PhpOffice\PhpPresentation\Tests\Writer\PowerPoint2007; +use PhpOffice\PhpPresentation\Exception\FileNotFoundException; use PhpOffice\PhpPresentation\Shape\Drawing; use PhpOffice\PhpPresentation\Tests\PhpPresentationTestCase; @@ -44,12 +45,17 @@ public function testDrawing(): void public function testDrawingException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('does not exist'); + $path = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png'; + + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The file "%s" doesn\'t exist', + $path + )); $oSlide = $this->oPresentation->getActiveSlide(); $oShape = $oSlide->createDrawingShape(); - $oShape->setPath(PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png', false); + $oShape->setPath($path, false); $this->writePresentationFile($this->oPresentation, 'PowerPoint2007'); } @@ -67,11 +73,16 @@ public function testDrawingZip(): void public function testDrawingZipException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('does not exist'); + $path = PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/filedoesntexist.png'; + + $this->expectException(FileNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The file "%s" doesn\'t exist', + $path + )); $oDrawing = new Drawing\ZipFile(); - $oDrawing->setPath('zip://' . PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'filedoesntexist.zip#secondpath'); + $oDrawing->setPath('zip://' . $path . '#secondpath'); $oSlide = $this->oPresentation->getActiveSlide(); $oSlide->addShape($oDrawing); diff --git a/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php b/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php index bb13cfdcf..e8b283b57 100644 --- a/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php +++ b/tests/PhpPresentation/Tests/Writer/PowerPoint2007Test.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Writer; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\Tests\PhpPresentationTestCase; use PhpOffice\PhpPresentation\Writer\PowerPoint2007; @@ -63,37 +65,13 @@ public function testSave(): void */ public function testSaveEmptyException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Filename is empty'); + $this->expectException(InvalidParameterException::class); + $this->expectExceptionMessage('The parameter pFilename can\'t have the value ""'); $object = new PowerPoint2007($this->oPresentation); $object->save(''); } - /** - * Test save with empty assignation. - */ - public function testSaveUnassignedException(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No PhpPresentation assigned.'); - - $object = new PowerPoint2007(); - $object->save('filename.pptx'); - } - - /** - * Test get PhpPresentation exception. - */ - public function testGetPhpPresentationException(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No PhpPresentation assigned.'); - - $object = new PowerPoint2007(); - $object->getPhpPresentation(); - } - /** * Test disk caching. */ @@ -116,8 +94,8 @@ public function testDiskCaching(): void */ public function testCachingException(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Directory does not exist: foo'); + $this->expectException(DirectoryNotFoundException::class); + $this->expectExceptionMessage('The directory foo doesn\'t exist'); $object = new PowerPoint2007($this->oPresentation); $object->setUseDiskCaching(true, 'foo'); diff --git a/tests/PhpPresentation/Tests/Writer/SerializedTest.php b/tests/PhpPresentation/Tests/Writer/SerializedTest.php index b0274e6b6..4d495d6d1 100644 --- a/tests/PhpPresentation/Tests/Writer/SerializedTest.php +++ b/tests/PhpPresentation/Tests/Writer/SerializedTest.php @@ -20,6 +20,8 @@ namespace PhpOffice\PhpPresentation\Tests\Writer; +use PhpOffice\PhpPresentation\Exception\DirectoryNotFoundException; +use PhpOffice\PhpPresentation\Exception\InvalidParameterException; use PhpOffice\PhpPresentation\PhpPresentation; use PhpOffice\PhpPresentation\Writer\Serialized; use PHPUnit\Framework\TestCase; @@ -37,33 +39,15 @@ public function testConstruct(): void $this->assertInstanceOf('PhpOffice\\PhpPresentation\\PhpPresentation', $object->getPhpPresentation()); } - public function testEmptyConstruct(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No PhpPresentation assigned.'); - - $object = new Serialized(); - $object->getPhpPresentation(); - } - public function testSaveEmpty(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Filename is empty.'); + $this->expectException(InvalidParameterException::class); + $this->expectExceptionMessage('The parameter pFilename can\'t have the value ""'); $object = new Serialized(new PhpPresentation()); $object->save(''); } - public function testSaveNoObject(): void - { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('No PhpPresentation assigned.'); - - $object = new Serialized(); - $object->save('file.phpppt'); - } - public function testSave(): void { $oPhpPresentation = new PhpPresentation(); @@ -79,18 +63,21 @@ public function testSave(): void public function testSaveNotExistingDir(): void { - $this->expectException(\Exception::class); - $this->expectExceptionMessage('Could not open'); + $path = tempnam(sys_get_temp_dir(), 'PhpPresentation_Serialized' . DIRECTORY_SEPARATOR . 'test'); + + $this->expectException(DirectoryNotFoundException::class); + $this->expectExceptionMessage(sprintf( + 'The directory %s doesn\'t exist', + $path + )); $oPhpPresentation = new PhpPresentation(); $oSlide = $oPhpPresentation->getActiveSlide(); $oImage = $oSlide->createDrawingShape(); $oImage->setPath(PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png'); - $object = new Serialized($oPhpPresentation); - $file = tempnam(sys_get_temp_dir(), 'PhpPresentation_Serialized'); - - $object->save($file . DIRECTORY_SEPARATOR . 'test' . DIRECTORY_SEPARATOR . 'test'); + $object = new Serialized($oPhpPresentation); + $object->save($path . DIRECTORY_SEPARATOR . 'test'); } public function testSaveOverwriting(): void