I am using PHP to create a file structure in which to store XLSX files, for this I have created this class:
<?php
class XLSX
{
/** @var resource */
private $handle;
/** @var array */
private $data;
/** @var array */
private $headings;
/** @var bool */
private $finished = true;
/** @var string */
private $filename;
/**
* Creates the class, must be done each time a file is written to as the handle is stored.
* @param string $filename The file to be written to, will create the file if it does not exist
*/
public function __construct($filename)
{
$dirname = dirname($filename);
if (!is_dir($dirname))
{
mkdir($dirname, 0777, true) ?: die("Failed to create the required directory.");
}
if (file_exists($filename))
{
for ($i = 1; $i <= 200; $i++)
{
$fname = explode(".", $filename);
if (!file_exists("{$fname[0]}_{$i}.{$fname[1]}"))
{
$this->handle = fopen("{$fname[0]}_{$i}.{$fname[1]}", "w");
$this->filename = str_replace(" ", "%20", "{$fname[0]}_{$i}.{$fname[1]}");
break;
}
}
}
else
{
$this->handle = fopen($filename, "w");
$this->filename = $filename;
}
$this->finished = false;
}
/**
* Set the headers for the XSLX
* @param array $headers
* @return bool
*/
public function SetHeaders($headers)
{
return $this->AddToArray("headings", $headers);
}
/**
* Get the headers that were set, if not set return null.
* @return array|null
*/
public function GetHeaders()
{
return !empty($this->headings) ? $this->headings : null;
}
public function SetData($data)
{
return $this->AddToArray("data", $data);
}
/**
* Get the data that was set, if not set return null.
* @return array|null
*/
public function GetData()
{
return !empty($this->data) ? $this->data : null;
}
public function GetFilename()
{
return $this->filename;
}
/**
* Set the handle and all data back to default, ready to recall a new instance.
* @return void
*/
public function Finish()
{
fclose($this->handle);
$this->handle = null;
$this->data = null;
$this->headings = null;
$this->finished = true;
$this->filename = null;
}
public function Write()
{
if (!$this->finished && isset($this->handle))
{
if ($this->GetHeaders() == null || $this->GetData() == null)
{
return false;
}
foreach ($this->GetHeaders() as $header)
{
fwrite($this->handle, $header . "\t");
}
fwrite($this->handle, "\n");
foreach ($this->GetData() as $data)
{
if (is_array($data))
{
foreach ($data as $d)
{
fwrite($this->handle, $d . "\t");
}
fwrite($this->handle, "\n");
}
else
{
fwrite($this->handle, $data . "\t");
}
}
}
return false;
}
/**
* Add items to an array
* @param string $arrayName
* @param mixed $values
* @return bool
*/
private function AddToArray($arrayName, $values)
{
if (!$this->finished)
{
foreach($values as $val)
{
$this->{$arrayName}[] = $val;
}
}
return !empty($this->{$arrayName}) ? true : false;
}
}
Currently I am only testing the __construct
method, and this works, but I wanted to know if there is a better way to do what I am doing?
I know it is incomplete, but I need to know if this is right for the __construct
method
Edit
I have completed the class and tested, it works perfect, but it seems there has to be a better way to do the __construct
method and also the Write
method. I have updated the code to show these changes
For anyone who may want to know, here is how this is used:
<?php require_once 'xlsx/XLSX.php'; $xlsx = new XLSX("downloads/" . date("j M Y") . "/test.xls"); ?>
<?php $xlsx->SetHeaders(['firstname', 'lastname', 'middlename']); ?>
<?php $xlsx->SetData([['FName', 'LName', 'MName'], ['FName', 'LName', 'MName'], ['FName', 'LName', 'MName']]); ?>
<?php $xlsx->Write(); ?>
<?php $xlsx->Finish(); ?>