Skip to content

Commit 32ed6a3

Browse files
author
Roman Syroeshko
committed
#58 - Part V (IOFactory).
1 parent 4e546d1 commit 32ed6a3

File tree

3 files changed

+60
-143
lines changed

3 files changed

+60
-143
lines changed

Classes/PHPWord/IOFactory.php

Lines changed: 29 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -25,134 +25,55 @@
2525
* @version 0.8.0
2626
*/
2727

28+
namespace PhpOffice\PhpWord;
29+
2830
use PhpOffice\PhpWord\Exceptions\Exception;
2931

30-
/**
31-
* Class PHPWord_IOFactory
32-
*/
33-
class PHPWord_IOFactory
32+
abstract class IOFactory
3433
{
3534
/**
36-
* Search locations
37-
*
38-
* @var array
39-
*/
40-
private static $_searchLocations = array(
41-
array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}'),
42-
array('type' => 'IReader', 'path' => 'PHPWord/Reader/{0}.php', 'class' => 'PHPWord_Reader_{0}' ),
43-
);
44-
45-
/**
46-
* Autoresolve classes
47-
*
48-
* @var array
49-
*/
50-
private static $_autoResolveClasses = array(
51-
'Serialized'
52-
);
53-
54-
/**
55-
* Private constructor for PHPWord_IOFactory
56-
*/
57-
private function __construct()
58-
{
59-
}
60-
61-
/**
62-
* Get search locations
63-
*
64-
* @return array
65-
*/
66-
public static function getSearchLocations()
67-
{
68-
return self::$_searchLocations;
69-
}
70-
71-
/**
72-
* Set search locations
73-
*
74-
* @param array $value
75-
* @throws Exception
76-
*/
77-
public static function setSearchLocations(array $value)
78-
{
79-
self::$_searchLocations = $value;
80-
}
81-
82-
/**
83-
* Add search location
84-
*
85-
* @param string $type Example: IWriter
86-
* @param string $location Example: PHPWord/Writer/{0}.php
87-
* @param string $classname Example: PHPWord_Writer_{0}
88-
*/
89-
public static function addSearchLocation($type = '', $location = '', $classname = '')
90-
{
91-
self::$_searchLocations[] = array('type' => $type, 'path' => $location, 'class' => $classname);
92-
}
93-
94-
/**
95-
* Create PHPWord_Writer_IWriter
96-
*
97-
* @param PHPWord $PHPWord
98-
* @param string $writerType Example: Word2007
99-
* @return PHPWord_Writer_IWriter
100-
* @throws Exception
35+
* @param PHPWord $phpWord
36+
* @param string $name
37+
* @return PhpOffice\PhpWord\Writer\IWriter
38+
* @throws PhpOffice\PhpWord\Exceptions\Exception
10139
*/
102-
public static function createWriter(PHPWord $PHPWord, $writerType = '')
40+
public static function createWriter(PHPWord $phpWord, $name)
10341
{
104-
$searchType = 'IWriter';
42+
try {
43+
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
10544

106-
foreach (self::$_searchLocations as $searchLocation) {
107-
if ($searchLocation['type'] == $searchType) {
108-
$className = str_replace('{0}', $writerType, $searchLocation['class']);
109-
$classFile = str_replace('{0}', $writerType, $searchLocation['path']);
110-
111-
$instance = new $className($PHPWord);
112-
if (!is_null($instance)) {
113-
return $instance;
114-
}
115-
}
45+
return new $fqName($phpWord);
46+
} catch (\Exception $ex) {
47+
throw new Exception("Could not instantiate \"{$name}\" class.");
11648
}
117-
118-
throw new Exception("No $searchType found for type $writerType");
11949
}
12050

12151
/**
122-
* Create PHPWord_Reader_IReader
123-
*
124-
* @param string $readerType Example: Word2007
125-
* @return PHPWord_Reader_IReader
126-
* @throws Exception
52+
* @param string $name
53+
* @return PhpOffice\PhpWord\Reader\IReader
54+
* @throws PhpOffice\PhpWord\Exceptions\Exception
12755
*/
128-
public static function createReader($readerType = '')
56+
public static function createReader($name)
12957
{
130-
$searchType = 'IReader';
58+
try {
59+
$fqName = "PhpOffice\\PhpWord\\Reader\\{$name}";
13160

132-
foreach (self::$_searchLocations as $searchLocation) {
133-
if ($searchLocation['type'] == $searchType) {
134-
$className = str_replace('{0}', $readerType, $searchLocation['class']);
135-
136-
$instance = new $className();
137-
if ($instance !== null) {
138-
return $instance;
139-
}
140-
}
61+
return new $fqName();
62+
} catch (\Exception $ex) {
63+
throw new Exception("Could not instantiate \"{$name}\" class.");
14164
}
142-
143-
throw new Exception("No $searchType found for type $readerType");
14465
}
14566

14667
/**
147-
* Loads PHPWord from file
68+
* Loads PhpWord from file
14869
*
149-
* @param string $pFilename The name of the file
150-
* @param string $readerType
151-
* @return PHPWord
70+
* @param string $filename The name of the file
71+
* @param string $readerName
72+
* @return PhpOffice\PHPWord
15273
*/
153-
public static function load($pFilename, $readerType = 'Word2007')
74+
public static function load($filename, $readerName = 'Word2007')
15475
{
155-
$reader = self::createReader($readerType);
156-
return $reader->load($pFilename);
76+
$reader = self::createReader($readerName);
77+
return $reader->load($filename);
15778
}
15879
}

Tests/PHPWord/IOFactoryTest.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,55 @@
11
<?php
22
namespace PHPWord\Tests;
33

4-
use PHPWord;
4+
use PhpOffice\PHPWord;
55
use PhpOffice\PhpWord\IOFactory;
6-
use PhpOffice\PhpWord\Writer\Word2007;
7-
use Exception;
86

97
/**
8+
* @@coversDefaultClass PhpOffice\PhpWord\IOFactory
109
* @package PHPWord\Tests
1110
* @runTestsInSeparateProcesses
1211
*/
13-
class IOFactoryTest extends \PHPUnit_Framework_TestCase
12+
final class IOFactoryTest extends \PHPUnit_Framework_TestCase
1413
{
15-
public function testGetSearchLocations()
14+
/**
15+
* @covers ::createWriter
16+
*/
17+
final public function testExistingWriterCanBeCreated()
1618
{
17-
$this->assertAttributeEquals(
18-
IOFactory::getSearchLocations(),
19-
'_searchLocations',
20-
'PhpOffice\\PhpWord\\IOFactory'
19+
$this->assertInstanceOf(
20+
'PhpOffice\\PhpWord\\Writer\\Word2007',
21+
IOFactory::createWriter(new PHPWord(), 'Word2007')
2122
);
2223
}
2324

24-
public function testSetSearchLocationsWithArray()
25+
/**
26+
* @covers ::createWriter
27+
* @expectedException PhpOffice\PhpWord\Exceptions\Exception
28+
* @expectedExceptionMessage Could not instantiate "Word2006" class.
29+
*/
30+
final public function testNonexistentWriterCanNotBeCreated()
2531
{
26-
IOFactory::setSearchLocations(array());
27-
$this->assertAttributeEquals(array(), '_searchLocations', 'PhpOffice\\PhpWord\\IOFactory');
32+
IOFactory::createWriter(new PHPWord(), 'Word2006');
2833
}
2934

30-
public function testAddSearchLocation()
35+
/**
36+
* @covers ::createReader
37+
*/
38+
final public function testExistingReaderCanBeCreated()
3139
{
32-
IOFactory::setSearchLocations(array());
33-
IOFactory::addSearchLocation('interface', 'classname');
34-
$this->assertAttributeEquals(
35-
array(array('interface' => 'interface', 'class' => 'classname')),
36-
'_searchLocations',
37-
'PhpOffice\\PhpWord\\IOFactory'
40+
$this->assertInstanceOf(
41+
'PhpOffice\\PhpWord\\Reader\\Word2007',
42+
IOFactory::createReader('Word2007')
3843
);
3944
}
4045

4146
/**
42-
* @expectedException Exception
43-
* @expectedExceptionMessage No IWriter found for type
47+
* @covers ::createReader
48+
* @expectedException PhpOffice\PhpWord\Exceptions\Exception
49+
* @expectedExceptionMessage Could not instantiate "Word2006" class.
4450
*/
45-
public function testCreateWriterException()
51+
final public function testNonexistentReaderCanNotBeCreated()
4652
{
47-
$oPHPWord = new PHPWord();
48-
49-
IOFactory::setSearchLocations(array());
50-
IOFactory::createWriter($oPHPWord);
51-
}
52-
53-
public function testCreateWriter()
54-
{
55-
$oPHPWord = new PHPWord();
56-
57-
$this->assertEquals(IOFactory::createWriter($oPHPWord, 'Word2007'), new Word2007($oPHPWord));
53+
IOFactory::createReader('Word2006');
5854
}
5955
}

Tests/_inc/TestHelperDOCX.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class TestHelperDOCX
1313
* @param \PHPWord $PHPWord
1414
* @return \PHPWord\Tests\XmlDocument
1515
*/
16-
public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007')
16+
public static function getDocument(PHPWord $PHPWord, $writerName = 'Word2007')
1717
{
1818
self::$file = tempnam(sys_get_temp_dir(), 'PHPWord');
1919
if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) {
2020
mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
2121
}
2222

23-
$xmlWriter = IOFactory::createWriter($PHPWord, $writer);
23+
$xmlWriter = IOFactory::createWriter($PHPWord, $writerName);
2424
$xmlWriter->save(self::$file);
2525

2626
$zip = new \ZipArchive;

0 commit comments

Comments
 (0)