-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFactory.php
126 lines (112 loc) · 2.96 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace GGGGino\WordBundle;
use Symfony\Component\HttpFoundation\StreamedResponse;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\WriterInterface;
use PhpOffice\PhpWord\Reader\ReaderInterface;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\IOFactory;
/**
* Factory for PhpWord objects, StreamedResponse, and WriterInterface.
*
* @package GGGGino\WordBundle
*/
class Factory
{
private $phpWordIO;
public function __construct($phpWordIO = '\PhpOffice\PhpWord\IOFactory')
{
$this->phpWordIO = $phpWordIO;
}
/**
* Creates an empty PhpWord Object if the filename is empty, otherwise loads the file into the object.
*
* @param string $filename
*
* @return PhpWord
*/
public function createPHPWordObject($filename = null)
{
return (null === $filename) ? new PhpWord() : call_user_func(array($this->phpWordIO, 'load'), $filename);
}
/**
* Load a file as template
*
* @param string $filename
*
* @return TemplateProcessor
*/
public function createTemplateObject($filename = null)
{
return new TemplateProcessor($filename);
}
/**
* From the template load the
*
* @param TemplateProcessor $templateobj
*
* @return PhpWord
*/
public function getPhpWordObjFromTemplate($templateobj)
{
$fileName = $templateobj->save();
$phpWordObject = IOFactory::load($fileName);
unlink($fileName);
return $phpWordObject;
}
/**
* Create a reader
*
* @param string $type
*
*
* @return ReaderInterface
*/
public function createReader($type = 'Word2007')
{
return call_user_func(array($this->phpWordIO, 'createReader'), $type);
}
/**
* Create a writer given the PHPExcelObject and the type,
* the type could be one of PHPExcel_IOFactory::$_autoResolveClasses
*
* @param PhpWord $phpWordObject
* @param string $type
*
*
* @return WriterInterface
*/
public function createWriter(PhpWord $phpWordObject, $type = 'Word2007')
{
return call_user_func(array($this->phpWordIO, 'createWriter'), $phpWordObject, $type);
}
/**
* Stream the file as Response.
*
* @param WriterInterface $writer
* @param int $status
* @param array $headers
*
* @return StreamedResponse
*/
public function createStreamedResponse(WriterInterface $writer, $status = 200, $headers = array())
{
return new StreamedResponse(
function () use ($writer) {
$writer->save('php://output');
},
$status,
$headers
);
}
/**
* Create a PHPExcel Helper HTML Object
*
* @return \PHPExcel_Helper_HTML
* @deprecated
*/
public function createHelperHTML()
{
return new \PHPExcel_Helper_HTML();
}
}