1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Writer\Ods; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright (c) 2006 - 2015 PhpSpreadsheet |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU Lesser General Public |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 2.1 of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16
|
|
|
* Lesser General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Lesser General Public |
19
|
|
|
* License along with this library; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21
|
|
|
* |
22
|
|
|
* @category PhpSpreadsheet |
23
|
|
|
* @copyright Copyright (c) 2006 - 2015 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet) |
24
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL |
25
|
|
|
* @version ##VERSION##, ##DATE## |
26
|
|
|
*/ |
27
|
|
|
class Meta extends WriterPart |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Write meta.xml to XML format |
31
|
|
|
* |
32
|
|
|
* @param \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet |
33
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
34
|
|
|
* @return string XML Output |
35
|
|
|
*/ |
36
|
|
|
public function write(\PhpOffice\PhpSpreadsheet\SpreadSheet $spreadsheet = null) |
37
|
|
|
{ |
38
|
|
|
if (!$spreadsheet) { |
39
|
|
|
$spreadsheet = $this->getParentWriter()->getSpreadsheet(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$objWriter = null; |
|
|
|
|
43
|
|
View Code Duplication |
if ($this->getParentWriter()->getUseDiskCaching()) { |
|
|
|
|
44
|
|
|
$objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); |
|
|
|
|
45
|
|
|
} else { |
46
|
|
|
$objWriter = new \PhpOffice\PhpSpreadsheet\Shared\XMLWriter(\PhpOffice\PhpSpreadsheet\Shared\XMLWriter::STORAGE_MEMORY); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// XML header |
50
|
|
|
$objWriter->startDocument('1.0', 'UTF-8'); |
51
|
|
|
|
52
|
|
|
// Meta |
53
|
|
|
$objWriter->startElement('office:document-meta'); |
54
|
|
|
|
55
|
|
|
$objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'); |
56
|
|
|
$objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink'); |
57
|
|
|
$objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); |
58
|
|
|
$objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0'); |
59
|
|
|
$objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office'); |
60
|
|
|
$objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#'); |
61
|
|
|
$objWriter->writeAttribute('office:version', '1.2'); |
62
|
|
|
|
63
|
|
|
$objWriter->startElement('office:meta'); |
64
|
|
|
|
65
|
|
|
$objWriter->writeElement('meta:initial-creator', $spreadsheet->getProperties()->getCreator()); |
66
|
|
|
$objWriter->writeElement('dc:creator', $spreadsheet->getProperties()->getCreator()); |
67
|
|
|
$objWriter->writeElement('meta:creation-date', date(DATE_W3C, $spreadsheet->getProperties()->getCreated())); |
68
|
|
|
$objWriter->writeElement('dc:date', date(DATE_W3C, $spreadsheet->getProperties()->getCreated())); |
69
|
|
|
$objWriter->writeElement('dc:title', $spreadsheet->getProperties()->getTitle()); |
70
|
|
|
$objWriter->writeElement('dc:description', $spreadsheet->getProperties()->getDescription()); |
71
|
|
|
$objWriter->writeElement('dc:subject', $spreadsheet->getProperties()->getSubject()); |
72
|
|
|
$keywords = explode(' ', $spreadsheet->getProperties()->getKeywords()); |
73
|
|
|
foreach ($keywords as $keyword) { |
74
|
|
|
$objWriter->writeElement('meta:keyword', $keyword); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
//<meta:document-statistic meta:table-count="XXX" meta:cell-count="XXX" meta:object-count="XXX"/> |
78
|
|
|
$objWriter->startElement('meta:user-defined'); |
79
|
|
|
$objWriter->writeAttribute('meta:name', 'Company'); |
80
|
|
|
$objWriter->writeRaw($spreadsheet->getProperties()->getCompany()); |
81
|
|
|
$objWriter->endElement(); |
82
|
|
|
|
83
|
|
|
$objWriter->startElement('meta:user-defined'); |
84
|
|
|
$objWriter->writeAttribute('meta:name', 'category'); |
85
|
|
|
$objWriter->writeRaw($spreadsheet->getProperties()->getCategory()); |
86
|
|
|
$objWriter->endElement(); |
87
|
|
|
|
88
|
|
|
$objWriter->endElement(); |
89
|
|
|
|
90
|
|
|
$objWriter->endElement(); |
91
|
|
|
|
92
|
|
|
return $objWriter->getData(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: