Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
CRAP
94.74% covered (success)
94.74%
18 / 19
IOFactory
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
11.02
94.74% covered (success)
94.74%
18 / 19
 createWriter
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 createReader
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 load
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
 loadClass
0.00% covered (danger)
0.00%
0 / 1
4.05
85.71% covered (warning)
85.71%
6 / 7
 isConcreteClass
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 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 * @copyright   2009-2015 PHPPresentation contributors
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19declare(strict_types=1);
20
21namespace PhpOffice\PhpPresentation;
22
23use PhpOffice\PhpPresentation\Exception\InvalidClassException;
24use PhpOffice\PhpPresentation\Exception\InvalidFileFormatException;
25use PhpOffice\PhpPresentation\Reader\ReaderInterface;
26use PhpOffice\PhpPresentation\Writer\WriterInterface;
27use ReflectionClass;
28
29/**
30 * IOFactory.
31 */
32class IOFactory
33{
34    /**
35     * Autoresolve classes.
36     *
37     * @var array<int, string>
38     */
39    private static $autoResolveClasses = ['Serialized', 'ODPresentation', 'PowerPoint97', 'PowerPoint2007'];
40
41    /**
42     * Create writer.
43     *
44     * @param PhpPresentation $phpPresentation
45     * @param string $name
46     */
47    public static function createWriter(PhpPresentation $phpPresentation, string $name = 'PowerPoint2007'): WriterInterface
48    {
49        return self::loadClass('PhpOffice\\PhpPresentation\\Writer\\' . $name, 'Writer', $phpPresentation);
50    }
51
52    /**
53     * Create reader.
54     *
55     * @param string $name
56     */
57    public static function createReader(string $name): ReaderInterface
58    {
59        return self::loadClass('PhpOffice\\PhpPresentation\\Reader\\' . $name, 'Reader');
60    }
61
62    /**
63     * Loads PhpPresentation from file using automatic ReaderInterface resolution.
64     *
65     * @throws InvalidFileFormatException
66     */
67    public static function load(string $pFilename): PhpPresentation
68    {
69        // Try loading using self::$autoResolveClasses
70        foreach (self::$autoResolveClasses as $autoResolveClass) {
71            $reader = self::createReader($autoResolveClass);
72            if ($reader->canRead($pFilename)) {
73                return $reader->load($pFilename);
74            }
75        }
76
77        throw new InvalidFileFormatException(
78            $pFilename,
79            IOFactory::class,
80            'Could not automatically determine the good ' . ReaderInterface::class
81        );
82    }
83
84    /**
85     * Load class
86     *
87     * @param string $class
88     * @param string $type
89     * @param PhpPresentation|null $phpPresentation
90     *
91     * @return object
92     *
93     * @throws InvalidClassException
94     */
95    private static function loadClass(string $class, string $type, PhpPresentation $phpPresentation = null)
96    {
97        if (!class_exists($class)) {
98            throw new InvalidClassException($class, $type . ': The class doesn\'t exist');
99        }
100        if (!self::isConcreteClass($class)) {
101            throw new InvalidClassException($class, $type . ': The class is an abstract class or an interface');
102        }
103        if (is_null($phpPresentation)) {
104            return new $class();
105        }
106
107        return new $class($phpPresentation);
108    }
109
110    /**
111     * Is it a concrete class?
112     */
113    private static function isConcreteClass(string $class): bool
114    {
115        $reflection = new ReflectionClass($class);
116
117        return !$reflection->isAbstract() && !$reflection->isInterface();
118    }
119}