1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Soluble\Spreadsheet\Library; |
4
|
|
|
|
5
|
|
|
use ExcelBook; |
6
|
|
|
|
7
|
|
|
class LibXL |
8
|
|
|
{ |
9
|
|
|
const FILE_FORMAT_XLS = 'xls'; |
10
|
|
|
const FILE_FORMAT_XLSX = 'xlsx'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* |
14
|
|
|
* @var array|null |
15
|
|
|
*/ |
16
|
|
|
protected static $default_license; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var array|null |
21
|
|
|
*/ |
22
|
|
|
protected $license; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $supportedFormats = [ |
29
|
|
|
self::FILE_FORMAT_XLS, |
30
|
|
|
self::FILE_FORMAT_XLSX |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @throws Exception\InvalidArgumentException |
36
|
|
|
* @param array $license associative array with 'name' and 'key' |
37
|
|
|
*/ |
38
|
10 |
|
public function __construct(array $license = null) |
39
|
|
|
{ |
40
|
10 |
|
if ($license !== null) { |
41
|
6 |
|
$this->setLicense($license); |
42
|
5 |
|
} |
43
|
10 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return an empty ExcelBook instance |
47
|
|
|
* |
48
|
|
|
* @throws Exception\RuntimeException if no excel extension is found |
49
|
|
|
* @throws Exception\InvalidArgumentException if unsupported format |
50
|
|
|
* |
51
|
|
|
* @param string $file_format by default xlsx, see constants FILE_FORMAT_* |
52
|
|
|
* @param string $locale by default utf-8 |
53
|
|
|
* @return ExcelBook |
54
|
|
|
*/ |
55
|
3 |
|
public function getExcelBook($file_format = self::FILE_FORMAT_XLSX, $locale = 'UTF-8') |
56
|
|
|
{ |
57
|
|
|
//@codeCoverageIgnoreStart |
58
|
|
|
if (!extension_loaded('excel')) { |
59
|
|
|
throw new Exception\RuntimeException(__METHOD__ . ' LibXL requires excel extension (https://github.com/iliaal/php_excel) and http://libxl.com/.'); |
60
|
|
|
} |
61
|
|
|
//@codeCoverageIgnoreEnd |
62
|
3 |
|
if (!$this->isSupportedFormat($file_format)) { |
63
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " Unsupported file format '$file_format'."); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$license = $this->getLicense(); |
67
|
1 |
|
$license_name = $license['name']; |
68
|
1 |
|
$license_key = $license['key']; |
69
|
1 |
|
$excel2007 = true; |
70
|
|
|
switch ($file_format) { |
71
|
1 |
|
case self::FILE_FORMAT_XLS: |
72
|
1 |
|
$excel2007 = false; |
73
|
1 |
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
$book = new ExcelBook($license_name, $license_key, $excel2007); |
77
|
1 |
|
if ($locale !== null) { |
78
|
1 |
|
$book->setLocale($locale); |
79
|
1 |
|
} |
80
|
1 |
|
return $book; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return libxl license |
85
|
|
|
* |
86
|
|
|
* @return array|null |
87
|
|
|
*/ |
88
|
4 |
|
public function getLicense() |
89
|
|
|
{ |
90
|
4 |
|
if ($this->license === null) { |
91
|
1 |
|
return self::getDefaultLicense(); |
92
|
|
|
} |
93
|
4 |
|
return $this->license; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check whether the format is supported |
98
|
|
|
* |
99
|
|
|
* @throws Exception\InvalidArgumentException |
100
|
|
|
* @param string $format |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
3 |
|
public static function isSupportedFormat($format) |
104
|
|
|
{ |
105
|
3 |
|
if (!is_string($format)) { |
106
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " file_format must be a string"); |
107
|
|
|
} |
108
|
2 |
|
return in_array((string) $format, self::$supportedFormats); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set license |
113
|
|
|
* |
114
|
|
|
* @throws Exception\InvalidArgumentException |
115
|
|
|
* @param array $license associative array with 'name' and 'key' |
116
|
|
|
*/ |
117
|
8 |
|
public function setLicense(array $license) |
118
|
|
|
{ |
119
|
8 |
|
if (!array_key_exists('name', $license) || !array_key_exists('key', $license)) { |
120
|
2 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " In order to set a libxl license you must provide an associative array with 'name' and 'key' set."); |
121
|
|
|
} |
122
|
6 |
|
$this->license = $license; |
123
|
6 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return default license information |
127
|
|
|
* |
128
|
|
|
* @return array|null |
129
|
|
|
*/ |
130
|
2 |
|
public static function getDefaultLicense() |
131
|
|
|
{ |
132
|
2 |
|
return self::$default_license; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Set default license information |
137
|
|
|
* |
138
|
|
|
* @throws Exception\InvalidArgumentException |
139
|
|
|
* @param array $license associative array with 'name' and 'key' |
140
|
|
|
*/ |
141
|
3 |
|
public static function setDefaultLicense(array $license) |
142
|
|
|
{ |
143
|
3 |
|
if (!array_key_exists('name', $license) || !array_key_exists('key', $license)) { |
144
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " In order to set a default libxl license you must provide an associative array with 'name' and 'key' set."); |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
self::$default_license = $license; |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Unset default license, useful for unit tests only |
152
|
|
|
* |
153
|
|
|
*/ |
154
|
10 |
|
public static function unsetDefaultLicense() |
155
|
|
|
{ |
156
|
10 |
|
self::$default_license = null; |
157
|
10 |
|
} |
158
|
|
|
} |
159
|
|
|
|