Skip to content

Commit be08f71

Browse files
Merge pull request #14 from ebanx/improvement/add-file-properties
[t:DCWIbRRX] added properties to xlsx file
2 parents a1aa396 + 522453e commit be08f71

File tree

5 files changed

+205
-23
lines changed

5 files changed

+205
-23
lines changed

Test/FilePropertyTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Test;
4+
5+
6+
use PHPUnit\Framework\TestCase;
7+
8+
class FilePropertyTest extends TestCase {
9+
10+
public function testAuthor() {
11+
$expected_author = "John Doe";
12+
$writer = new \XLSXWriter();
13+
$writer->setAuthor($expected_author);
14+
15+
$xlsx_properties = $writer->getFileProperties();
16+
17+
$this->assertEquals($expected_author, $xlsx_properties["author"]);
18+
}
19+
20+
public function testTitle() {
21+
$expected_title = "My Spreadsheet";
22+
$writer = new \XLSXWriter();
23+
$writer->setTitle($expected_title);
24+
25+
$xlsx_properties = $writer->getFileProperties();
26+
27+
$this->assertEquals($expected_title, $xlsx_properties["title"]);
28+
}
29+
30+
public function testSubject() {
31+
$expected_subject = "My Spreadsheet is Wonderful";
32+
$writer = new \XLSXWriter();
33+
$writer->setSubject($expected_subject);
34+
35+
$xlsx_properties = $writer->getFileProperties();
36+
37+
$this->assertEquals($expected_subject, $xlsx_properties["subject"]);
38+
}
39+
40+
public function testCompany() {
41+
$expected_company = "EBANX";
42+
$writer = new \XLSXWriter();
43+
$writer->setCompany($expected_company);
44+
45+
$xlsx_properties = $writer->getFileProperties();
46+
47+
$this->assertEquals($expected_company, $xlsx_properties["company"]);
48+
}
49+
50+
public function testKeywords() {
51+
$expected_keywords = ["spreadsheet", "php", "EBANX"];
52+
$writer = new \XLSXWriter();
53+
$writer->setKeywords($expected_keywords);
54+
55+
$xlsx_properties = $writer->getFileProperties();
56+
57+
$this->assertEquals($expected_keywords, $xlsx_properties["keywords"]);
58+
}
59+
60+
}

Test/XLSXWriterTest.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Test;
4+
5+
include_once __DIR__.'/../vendor/autoload.php';
6+
7+
use PHPUnit\Framework\TestCase;
8+
use SimpleXMLElement;
9+
use XLSXWriter;
10+
use XLSXWriter_BuffererWriter;
11+
use ZipArchive;
12+
13+
class _XLSXWriter_ extends XLSXWriter
14+
{
15+
public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) {
16+
return call_user_func_array('parent::writeCell', [&$file, $row_number, $column_number, $value, $cell_format]);
17+
}
18+
}
19+
//Just a simple test, by no means comprehensive
20+
21+
class XLSXWriterTest extends TestCase
22+
{
23+
/**
24+
* @covers XLSXWriter::writeCell
25+
*/
26+
public function testWriteCell() {
27+
$filename = tempnam("/tmp", "xlsx_writer");
28+
$file_writer = new XLSXWriter_BuffererWriter($filename);
29+
30+
$xlsx_writer = new _XLSXWriter_();
31+
$xlsx_writer->writeCell($file_writer, 0, 0, '0123', 'string');
32+
$file_writer->close();
33+
$cell_xml = file_get_contents($filename);
34+
$this->assertNotEquals('<c r="A1" s="0" t="n"><v>123</v></c>', $cell_xml);
35+
$this->assertEquals('<c r="A1" s="0" t="s"><v>0</v></c>', $cell_xml);//0123 should be the 0th index of the shared string array
36+
@unlink($filename);
37+
}
38+
39+
/**
40+
* @covers XLSXWriter::writeToFile
41+
*/
42+
public function testWriteToFile() {
43+
$filename = tempnam("/tmp", "xlsx_writer");
44+
45+
$header = ['0'=>'string','1'=>'string','2'=>'string','3'=>'string'];
46+
$sheet = [
47+
['55','66','77','88'],
48+
['10','11','12','13'],
49+
];
50+
51+
$xlsx_writer = new XLSXWriter();
52+
$xlsx_writer->writeSheet($sheet,'mysheet',$header);
53+
$xlsx_writer->writeToFile($filename);
54+
55+
$zip = new ZipArchive();
56+
$r = $zip->open($filename);
57+
$this->assertTrue($r);
58+
59+
$this->assertNotEmpty(($zip->numFiles));
60+
61+
$out_sheet = [];
62+
for($z=0; $z < $zip->numFiles; $z++) {
63+
$inside_zip_filename = $zip->getNameIndex($z);
64+
65+
if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) {
66+
$out_sheet = $this->stripCellsFromSheetXML($zip->getFromName($inside_zip_filename));
67+
array_shift($out_sheet);
68+
$out_sheet = array_values($out_sheet);
69+
}
70+
}
71+
72+
$zip->close();
73+
@unlink($filename);
74+
75+
$r1 = self::array_diff_assoc_recursive($out_sheet, $sheet);
76+
$r2 = self::array_diff_assoc_recursive($sheet, $out_sheet);
77+
$this->assertEmpty($r1);
78+
$this->assertEmpty($r2);
79+
}
80+
81+
private function stripCellsFromSheetXML($sheet_xml) {
82+
$output = [];
83+
84+
$xml = new SimpleXMLElement($sheet_xml);
85+
86+
for ($i = 0; $i < count($xml->sheetData->row); $i++) {
87+
$row = $xml->sheetData->row[$i];
88+
for ($j = 0; $j < count($row->c); $j ++) {
89+
$output[$i][$j] = (string)$row->c[$j]->v;
90+
}
91+
}
92+
93+
return $output;
94+
}
95+
96+
public static function array_diff_assoc_recursive($array1, $array2) {
97+
$difference = [];
98+
foreach($array1 as $key => $value) {
99+
if(is_array($value)) {
100+
if(!isset($array2[$key]) || !is_array($array2[$key])) {
101+
$difference[$key] = $value;
102+
} else {
103+
$new_diff = self::array_diff_assoc_recursive($value, $array2[$key]);
104+
if(!empty($new_diff)) {
105+
$difference[$key] = $new_diff;
106+
}
107+
}
108+
} else if(!isset($array2[$key]) || $array2[$key] != $value) {
109+
$difference[$key] = $value;
110+
}
111+
}
112+
113+
return empty($difference) ? [] : $difference;
114+
}
115+
}

Test/XlsxWriterTest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

testbench/xlsxwriter.class.Test.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
include_once __DIR__.'/../vendor/autoload.php';
44

5+
use PHPUnit\Framework\TestCase;
6+
57
//TODO test double:writeSheetHeader
68
//TODO test invalid UTF8
79
//TODO test outoforder writeSheetRow('Sheet1',());
810

911
class _XLSXWriter_ extends XLSXWriter
1012
{
1113
public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) {
12-
return call_user_func_array('parent::writeCell', func_get_args());
14+
return call_user_func_array('parent::writeCell', [&$file, $row_number, $column_number, $value, $cell_format]);
1315
}
1416
}
1517
//Just a simple test, by no means comprehensive
1618

17-
class XLSXWriterTest extends PHPUnit_Framework_TestCase
19+
class XLSXWriterTest extends TestCase
1820
{
1921
/**
2022
* @covers XLSXWriter::writeCell

xlsxwriter.class.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ class XLSXWriter
2020

2121
protected $current_sheet = '';
2222

23+
protected $title;
24+
protected $subject;
25+
protected $company;
26+
protected $description;
27+
protected $keywords = [];
28+
2329
public function __construct()
2430
{
2531
if(!ini_get('date.timezone'))
@@ -30,10 +36,20 @@ public function __construct()
3036
}
3137

3238
public function setAuthor($author='') { $this->author=$author; }
39+
public function setTitle($title='') { $this->title=$title; }
40+
public function setSubject($subject='') { $this->subject=$subject; }
41+
public function setCompany($company='') { $this->company=$company; }
42+
public function setKeywords($keywords=[]) { $this->keywords=$keywords; }
43+
public function setDescription($description='') { $this->description=$description; }
3344

3445
public function getFileProperties(): array {
3546
return [
3647
"author" => $this->author,
48+
"title" => $this->title,
49+
"subject" => $this->subject,
50+
"company" => $this->company,
51+
"keywords" => $this->keywords,
52+
"description" => $this->description,
3753
];
3854
}
3955

@@ -415,7 +431,10 @@ protected function buildAppXML()
415431
{
416432
$app_xml="";
417433
$app_xml.='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
418-
$app_xml.='<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><TotalTime>0</TotalTime></Properties>';
434+
$app_xml.='<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">';
435+
$app_xml.='<TotalTime>0</TotalTime>';
436+
$app_xml.='<Company>'.self::xmlspecialchars($this->company).'</Company>';
437+
$app_xml.='</Properties>';
419438
return $app_xml;
420439
}
421440

@@ -425,7 +444,13 @@ protected function buildCoreXML()
425444
$core_xml.='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
426445
$core_xml.='<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
427446
$core_xml.='<dcterms:created xsi:type="dcterms:W3CDTF">'.date("Y-m-d\TH:i:s.00\Z").'</dcterms:created>';//$date_time = '2014-10-25T15:54:37.00Z';
447+
$core_xml.='<dc:title>'.self::xmlspecialchars($this->title).'</dc:title>';
448+
$core_xml.='<dc:subject>'.self::xmlspecialchars($this->subject).'</dc:subject>';
428449
$core_xml.='<dc:creator>'.self::xmlspecialchars($this->author).'</dc:creator>';
450+
if (!empty($this->keywords)) {
451+
$core_xml.='<cp:keywords>'.self::xmlspecialchars(implode (", ", (array)$this->keywords)).'</cp:keywords>';
452+
}
453+
$core_xml.='<dc:description>'.self::xmlspecialchars($this->description).'</dc:description>';
429454
$core_xml.='<cp:revision>0</cp:revision>';
430455
$core_xml.='</cp:coreProperties>';
431456
return $core_xml;

0 commit comments

Comments
 (0)