beautify code

This commit is contained in:
2024-11-12 23:00:03 +05:00
parent c4a2a947e2
commit 582c5dada5
10 changed files with 123 additions and 51 deletions

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Modules\Excell;
use App\Modules\Excell\Types\iRichText\RichTextWrapper;
class Excell
{
/**
* RichText wrapper
*/
public static function richText(): RichTextWrapper
{
return new RichTextWrapper;
}
}

View File

@@ -1,11 +1,11 @@
<?php
namespace App\Modules\PHPExcell;
namespace App\Modules\Excell;
use App\Modules\Makeable;
use App\Modules\ModuleContract;
class PHPExcellModule implements ModuleContract
class ExcellModule implements ModuleContract
{
use Makeable;

View File

@@ -0,0 +1,8 @@
<?php
namespace App\Modules\Excell\Fonts;
class FONTS
{
public const TIMES_NEW_ROMAN = 'Times New Roman';
}

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Modules\Excell\Types\iRichText;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Style\Font;
class RichTextWrapper
{
public function __construct(
protected RichText $richText = new RichText,
protected string $defaultFontName = 'Calibri',
protected int $defaultSize = 12,
) {}
/**
* Default font name
*/
public function setDefaultFont(string $fontname): self
{
$this->defaultFontName = $fontname;
return $this;
}
/**
* Default font size
*/
public function setDefaultSize(int $size): self
{
$this->defaultSize = $size;
return $this;
}
/**
* Add or attach text
*
* @param array<int, string> $styles
*/
public function add(?string $text, array $styles = [], bool $bold = false): self
{
if (! $text) {
return $this;
}
$textRun = $this->richText->createTextRun($text);
$font = $textRun->getFont() ?: new Font;
$font->setName($this->defaultFontName)
->setSize($this->defaultSize);
if ($bold) {
$font->setBold(true);
}
return $this;
}
/**
* Return RichText
*/
public function toRichText(): RichText
{
return $this->richText;
}
}

View File

@@ -0,0 +1,5 @@
<?php
namespace App\Modules\Excell\Types\RichText\Traits;
trait HasExcellRichTextWrappers {}

View File

@@ -2,6 +2,8 @@
namespace App\Modules\Invoice\Actions;
use App\Modules\Excell\Excell;
use App\Modules\Excell\Fonts\FONTS;
use App\Modules\Invoice\Data\InvoiceExcellData;
use App\Modules\Invoice\Data\InvoiceItem;
use App\Modules\Makeable;
@@ -99,22 +101,6 @@ class GenerateInvoiceExcell
$this->worksheet = $this->spreadsheet->getActiveSheet();
}
/**
* Create a RichText object with specified text and font settings.
*/
protected function addToRichText(RichText $richText, string $text, bool $isBold = false): RichText
{
$textRun = $richText->createTextRun($text);
$font = $textRun->getFont() ?: new Font;
$font->setName('Times New Roman')->setSize(10);
if ($isBold) {
$font->setBold(true);
}
return $richText;
}
/**
* Fill the cells
*/
@@ -244,7 +230,7 @@ class GenerateInvoiceExcell
);
$this->worksheet->getStyle('A'.$itemCellNumber)->applyFromArray([
'font' => ['bold' => false, 'size' => 8, 'name' => 'Times New Roman'],
'alignment' => ['vertical' => Alignment::VERTICAL_TOP]
'alignment' => ['vertical' => Alignment::VERTICAL_TOP],
]);
}
@@ -304,17 +290,12 @@ class GenerateInvoiceExcell
*/
public function satyjySalgytBelgi(): RichText
{
$richText = new RichText;
$satyjy_salgyt_belgi = $this->addToRichText(
richText: $richText,
text: 'Satyjynyň şahsy salgyt belgisi: '
);
return $this->addToRichText(
richText: $satyjy_salgyt_belgi,
text: $this->data->seller_ssb,
isBold: true
);
return Excell::richText()
->setDefaultFont(FONTS::TIMES_NEW_ROMAN)
->setDefaultSize(10)
->add('Satyjynyň şahsy salgyt belgisi: ')
->add($this->data->seller_ssb, bold: true)
->toRichText();
}
/**
@@ -322,17 +303,12 @@ class GenerateInvoiceExcell
*/
public function satyjyBanky(): RichText
{
$richText = new RichText;
$satyjy_banky = $this->addToRichText(
richText: $richText,
text: sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name)
);
return $this->addToRichText(
richText: $satyjy_banky,
text: $this->data->seller_bank_hb_1,
isBold: true
);
return Excell::richText()
->setDefaultFont(FONTS::TIMES_NEW_ROMAN)
->setDefaultSize(10)
->add(sprintf('Satyjynyň bankynyň ady %s, hasap № ', $this->data->seller_bank_name))
->add($this->data->seller_bank_hb_1, bold: true)
->toRichText();
}
/**
@@ -340,16 +316,16 @@ class GenerateInvoiceExcell
*/
public function satyjyBankyMaglumatlary(): RichText
{
$richText = new RichText;
$city_label = $this->addToRichText($richText, 'şäherde (etrapda) ');
$city = $this->addToRichText($city_label, $this->data->seller_bank_city, isBold: true);
$bab_label = $this->addToRichText($city, ' BAB ');
$bab = $this->addToRichText($bab_label, $this->data->seller_bank_bab, isBold: true);
$hb_2_label = $this->addToRichText($bab, ' bankyň kor.hasap ');
return $this->addToRichText($hb_2_label, $this->data->seller_bank_hb_2, isBold: true);
return Excell::richText()
->setDefaultFont(FONTS::TIMES_NEW_ROMAN)
->setDefaultSize(10)
->add('şäherde (etrapda) ')
->add($this->data->seller_bank_city, bold: true)
->add(' BAB ')
->add($this->data->seller_bank_bab, bold: true)
->add(' bankyň kor.hasap ')
->add($this->data->seller_bank_hb_2, bold: true)
->toRichText();
}
/**

Binary file not shown.

Binary file not shown.