Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Apr 3, 2022
1 parent 9433753 commit a123891
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 267 deletions.
85 changes: 70 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ composer require mpdf/mpdf: "^8.0"

### One-dimensional table

**template.xlsx:**
**template1.xlsx:**

![Demo](https://anour.ru/resources/office-v1-10.png)

Expand Down Expand Up @@ -58,18 +58,20 @@ $data = [

// Save as XLSX (Excel)
(new \AnourValar\Office\TemplateService())
->render(
'template.xlsx', // path to template
$data, // input data
\AnourValar\Office\SaveFormat::Xlsx // save format
->generate(
'template1.xlsx', // path to template
$data // input data
)
->save('generated_document.xlsx');

// Available save formats:
// \AnourValar\Office\SaveFormat::Xlsx
// \AnourValar\Office\SaveFormat::Pdf
// \AnourValar\Office\SaveFormat::Html
// \AnourValar\Office\SaveFormat::Ods
->saveToFile(
'generated_document.xlsx', // path to save
\AnourValar\Office\Format::Xlsx // save format
);

// Available formats:
// \AnourValar\Office\Format::Xlsx
// \AnourValar\Office\Format::Pdf
// \AnourValar\Office\Format::Html
// \AnourValar\Office\Format::Ods
```

**generated_document.xlsx:**
Expand All @@ -84,7 +86,7 @@ $data = [

### Two-dimensional table

**template.xlsx:**
**template2.xlsx:**

![Demo](https://anour.ru/resources/office-v1-20.png)

Expand Down Expand Up @@ -115,11 +117,64 @@ $data = [

// Save as XLSX (Excel)
(new \AnourValar\Office\TemplateService())
->render('template.xlsx' $data)
->save('generated_document.xlsx');
->generate('template2.xlsx', $data)
->saveToFile('generated_document.xlsx'); // xlsx as save format is set by default
```

**generated_document.xlsx:**

![Demo](https://anour.ru/resources/office-v1-21.png)

### Additional Features

**template3.xlsx:**

![Demo](https://anour.ru/resources/office-v1-30.png)

```php
$data = [
'foo' => 'Hello',

'bar' => function (\AnourValar\Office\Drivers\TemplateInterface $driver, $cell) {
$driver->insertImage('logo.png', $cell, ['width' => 100, 'offset_y' => -45]);
return 'Logo!'; // replace marker "[bar]" with return value
}
];

(new \AnourValar\Office\TemplateService())
->hookValue(function (TemplateInterface $driver, $cell, $value) {
// Hook will be called for every cell's value which is changing

$value .= ' world';
return $value;
})
->generate(
'template3.ods',
$data,
\AnourValar\Office\Format::Ods // template's format
)
->saveToFile('generated_document.xlsx');

// Available hooks:
// hookLoad: Closure(TemplateInterface $driver, string $templateFile, Format $templateFormat)
// hookBefore: Closure(TemplateInterface $templateDriver, array &$data)
// hookValue: Closure(TemplateInterface $templateDriver, string $cell, mixed $value)
// hookAfter: Closure(TemplateInterface $templateDriver)
```

**generated_document.xlsx:**

![Demo](https://anour.ru/resources/office-v1-31.png)

### Merge (union)

```php
$dataA = ['foo' => 'hello'];
$dataB = ['foo' => 'world'];

$documentA = (new \AnourValar\Office\TemplateService())->generate('template.xlsx', $dataA);
$documentB = (new \AnourValar\Office\TemplateService())->generate('template.xlsx', $dataB);

$mixer = new \AnourValar\Office\MixerService();
$mixer($documentA, $documentB)->saveToFile('generated_document.xlsx');
```
15 changes: 15 additions & 0 deletions src/Drivers/LoadInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AnourValar\Office\Drivers;

interface LoadInterface
{
/**
* Load a template with specific format
*
* @param string $file
* @param \AnourValar\Office\Format $format
* @return self
*/
public function load(string $file, \AnourValar\Office\Format $format): self;
}
29 changes: 29 additions & 0 deletions src/Drivers/MixInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace AnourValar\Office\Drivers;

interface MixInterface
{
/**
* Set title for an active sheet
*
* @param string $title
* @return self
*/
public function setSheetTitle(string $title): self;

/**
* Get title of an active sheet
*
* @return string
*/
public function getSheetTitle(): string;

/**
* Merge (union) a sheet from another instanceof of driver
*
* @param \AnourValar\Office\Drivers\MixInterface $driver
* @return self
*/
public function mergeDriver(\AnourValar\Office\Drivers\MixInterface $driver): self;
}
122 changes: 58 additions & 64 deletions src/Drivers/PhpSpreadsheetDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace AnourValar\Office\Drivers;

class PhpSpreadsheetDriver implements TemplateInterface
class PhpSpreadsheetDriver implements TemplateInterface, MixInterface
{
/**
* @var string
*/
protected const PDF_DRIVER = 'Mpdf';

/**
* @var string
*/
Expand All @@ -26,62 +21,29 @@ class PhpSpreadsheetDriver implements TemplateInterface

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::loadXlsx()
* @see \AnourValar\Office\Drivers\LoadInterface::load()
*/
public function loadXlsx(string $file): self
public function load(string $file, \AnourValar\Office\Format $format): self
{
$this->spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx')->load($file);
$this->spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($this->getFormat($format))->load($file);
$this->sheet = $this->spreadsheet->getActiveSheet();

return $this;
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::loadOds()
* @see \AnourValar\Office\Drivers\SaveInterface::save()
*/
public function loadOds(string $file): self
public function save(string $file, \AnourValar\Office\Format $format): void
{
$this->spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Ods')->load($file);
$this->sheet = $this->spreadsheet->getActiveSheet();
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, $this->getFormat($format));

return $this;
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::saveXlsx()
*/
public function saveXlsx(string $file): void
{
\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, 'Xlsx')->save($file);
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::savePdf()
*/
public function savePdf(string $file): void
{
\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, static::PDF_DRIVER)->save($file);
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::saveHtml()
*/
public function saveHtml(string $file): void
{
\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, 'Html')->save($file);
}
if (method_exists($writer, 'writeAllSheets')) {
$writer->writeAllSheets();
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::saveOds()
*/
public function saveOds(string $file): void
{
\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->spreadsheet, 'Ods')->save($file);
$writer->save($file);
}

/**
Expand Down Expand Up @@ -217,26 +179,26 @@ public function addRow(int $rowBefore, int $qty = 1): self
}

/**
* Add a column
*
* @param string $columnBefore
* @param int $qty
* @return self
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::deleteRow()
*/
public function addColumn(string $columnBefore, int $qty = 1): self
public function deleteRow(int $row, int $qty = 1): self
{
$this->sheet->insertNewColumnBefore($columnBefore, $qty);
$this->sheet->removeRow($row, $qty);

return $this;
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\TemplateInterface::deleteRow()
* Add a column
*
* @param string $columnBefore
* @param int $qty
* @return self
*/
public function deleteRow(int $row, int $qty = 1): self
public function addColumn(string $columnBefore, int $qty = 1): self
{
$this->sheet->removeRow($row, $qty);
$this->sheet->insertNewColumnBefore($columnBefore, $qty);

return $this;
}
Expand Down Expand Up @@ -274,10 +236,8 @@ public function setWidth(string $column, mixed $width = null): self
}

/**
* Set title for active a sheet
*
* @param string $title
* @return self
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\MixInterface::setSheetTitle()
*/
public function setSheetTitle(string $title): self
{
Expand All @@ -286,6 +246,26 @@ public function setSheetTitle(string $title): self
return $this;
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\MixInterface::getSheetTitle()
*/
public function getSheetTitle(): string
{
return $this->sheet->getTitle();
}

/**
* {@inheritDoc}
* @see \AnourValar\Office\Drivers\MixInterface::mergeDriver()
*/
public function mergeDriver(\AnourValar\Office\Drivers\MixInterface $driver): self
{
$this->spreadsheet->addExternalSheet($driver->sheet);

return $this;
}

/**
* Place an image
*
Expand Down Expand Up @@ -333,6 +313,20 @@ public function insertImage(string $filename, string $cell, array $options = [])
return $this;
}

/**
* @param \AnourValar\Office\Format $format
* @return string
*/
protected function getFormat(\AnourValar\Office\Format $format): string
{
return match($format) {
\AnourValar\Office\Format::Xlsx => 'Xlsx',
\AnourValar\Office\Format::Pdf => 'Mpdf',
\AnourValar\Office\Format::Html => 'Html',
\AnourValar\Office\Format::Ods => 'Ods',
};
}

/**
* Установка формата ячейки
*
Expand Down
15 changes: 15 additions & 0 deletions src/Drivers/SaveInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace AnourValar\Office\Drivers;

interface SaveInterface
{
/**
* Save in specific format
*
* @param string $file
* @param \AnourValar\Office\Format $format
* @return void
*/
public function save(string $file, \AnourValar\Office\Format $format): void;
}
Loading

0 comments on commit a123891

Please sign in to comment.