Skip to content

Commit c1b4b2e

Browse files
committed
Skeleton copied from PHPExcel :)
1 parent 0b876a3 commit c1b4b2e

File tree

4 files changed

+2245
-1
lines changed

4 files changed

+2245
-1
lines changed

Classes/PHPWord/IOFactory.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class PHPWord_IOFactory
3737
* @var array
3838
*/
3939
private static $_searchLocations = array(
40-
array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}')
40+
array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}'),
41+
array( 'type' => 'IReader', 'path' => 'PHPWord/Reader/{0}.php', 'class' => 'PHPWord_Reader_{0}' ),
4142
);
4243

4344
/**
@@ -118,4 +119,28 @@ public static function createWriter(PHPWord $PHPWord, $writerType = '')
118119

119120
throw new Exception("No $searchType found for type $writerType");
120121
}
122+
123+
/**
124+
* Create PHPWord_Reader_IReader
125+
*
126+
* @param string $readerType Example: Word2007
127+
* @return PHPWord_Reader_IReader
128+
*/
129+
public static function createReader($readerType = '') {
130+
$searchType = 'IReader';
131+
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+
}
141+
}
142+
143+
throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
144+
}
145+
121146
}

Classes/PHPWord/Reader/Abstract.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2014 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 2014 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version 0.7.0
26+
*/
27+
28+
29+
/**
30+
* PHPWord_Reader_Abstract
31+
*/
32+
abstract class PHPWord_Reader_Abstract implements PHPWord_Reader_IReader
33+
{
34+
/**
35+
* Read data only?
36+
*
37+
* @var boolean
38+
*/
39+
protected $_readDataOnly = FALSE;
40+
41+
protected $_fileHandle = NULL;
42+
43+
44+
/**
45+
* Read data only?
46+
*
47+
* @return boolean
48+
*/
49+
public function getReadDataOnly() {
50+
return $this->_readDataOnly;
51+
}
52+
53+
/**
54+
* Set read data only
55+
*
56+
* @param boolean $pValue
57+
* @return PHPWord_Reader_IReader
58+
*/
59+
public function setReadDataOnly($pValue = FALSE) {
60+
$this->_readDataOnly = $pValue;
61+
return $this;
62+
}
63+
64+
/**
65+
* Open file for reading
66+
*
67+
* @param string $pFilename
68+
* @throws PHPWord_Reader_Exception
69+
* @return resource
70+
*/
71+
protected function _openFile($pFilename)
72+
{
73+
// Check if file exists
74+
if (!file_exists($pFilename) || !is_readable($pFilename)) {
75+
throw new PHPWord_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
76+
}
77+
78+
// Open file
79+
$this->_fileHandle = fopen($pFilename, 'r');
80+
if ($this->_fileHandle === FALSE) {
81+
throw new PHPWord_Reader_Exception("Could not open file " . $pFilename . " for reading.");
82+
}
83+
}
84+
85+
/**
86+
* Can the current PHPWord_Reader_IReader read the file?
87+
*
88+
* @param string $pFilename
89+
* @return boolean
90+
* @throws PHPWord_Reader_Exception
91+
*/
92+
public function canRead($pFilename)
93+
{
94+
// Check if file exists
95+
try {
96+
$this->_openFile($pFilename);
97+
} catch (Exception $e) {
98+
return FALSE;
99+
}
100+
101+
$readable = $this->_isValidFormat();
102+
fclose ($this->_fileHandle);
103+
return $readable;
104+
}
105+
106+
}

Classes/PHPWord/Reader/IReader.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2014 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 2014 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version 0.7.0
26+
*/
27+
28+
29+
/**
30+
* PHPWord_Reader_IReader
31+
*/
32+
interface PHPWord_Reader_IReader
33+
{
34+
/**
35+
* Can the current PHPWord_Reader_IReader read the file?
36+
*
37+
* @param string $pFilename
38+
* @return boolean
39+
*/
40+
public function canRead($pFilename);
41+
42+
/**
43+
* Loads PHPWord from file
44+
*
45+
* @param string $pFilename
46+
*/
47+
public function load($pFilename);
48+
}

0 commit comments

Comments
 (0)