Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.00% covered (success)
95.00%
19 / 20
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
IOFactory
95.00% covered (success)
95.00%
19 / 20
80.00% covered (warning)
80.00%
4 / 5
11
0.00% covered (danger)
0.00%
0 / 1
 createWriter
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createReader
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 loadClass
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 isConcreteClass
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation;
21
22use PhpOffice\PhpPresentation\Exception\InvalidClassException;
23use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException;
24use PhpOffice\PhpPresentation\Reader\ReaderInterface;
25use PhpOffice\PhpPresentation\Writer\WriterInterface;
26use ReflectionClass;
27
28/**
29 * IOFactory.
30 */
31class IOFactory
32{
33    /**
34     * Autoresolve classes.
35     *
36     * @var array<int, string>
37     */
38    private static $autoResolveClasses = ['Serialized', 'ODPresentation', 'PowerPoint97', 'PowerPoint2007'];
39
40    /**
41     * Create writer.
42     */
43    public static function createWriter(PhpPresentation $phpPresentation, string $name = 'PowerPoint2007'): WriterInterface
44    {
45        return self::loadClass('PhpOffice\\PhpPresentation\\Writer\\' . $name, 'Writer', $phpPresentation);
46    }
47
48    /**
49     * Create reader.
50     */
51    public static function createReader(string $name): ReaderInterface
52    {
53        return self::loadClass('PhpOffice\\PhpPresentation\\Reader\\' . $name, 'Reader');
54    }
55
56    /**
57     * Loads PhpPresentation from file using automatic ReaderInterface resolution.
58     */
59    public static function load(string $pFilename): PhpPresentation
60    {
61        // Try loading using self::$autoResolveClasses
62        foreach (self::$autoResolveClasses as $autoResolveClass) {
63            $reader = self::createReader($autoResolveClass);
64            if ($reader->canRead($pFilename)) {
65                return $reader->load($pFilename);
66            }
67        }
68
69        throw new InvalidFileFormatException(
70            $pFilename,
71            self::class,
72            'Could not automatically determine the good ' . ReaderInterface::class
73        );
74    }
75
76    /**
77     * Load class.
78     *
79     * @return object
80     */
81    private static function loadClass(string $class, string $type, ?PhpPresentation $phpPresentation = null)
82    {
83        if (!class_exists($class)) {
84            throw new InvalidClassException($class, $type . ': The class doesn\'t exist');
85        }
86        if (!self::isConcreteClass($class)) {
87            throw new InvalidClassException($class, $type . ': The class is an abstract class or an interface');
88        }
89        if (null === $phpPresentation) {
90            return new $class();
91        }
92
93        return new $class($phpPresentation);
94    }
95
96    /**
97     * Is it a concrete class?
98     */
99    private static function isConcreteClass(string $class): bool
100    {
101        $reflection = new ReflectionClass($class);
102
103        return !$reflection->isAbstract() && !$reflection->isInterface();
104    }
105}