Skip to content

Commit

Permalink
Add a download function (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
cable8mm authored Feb 14, 2025
1 parent adf04f3 commit 02d4a84
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
51 changes: 43 additions & 8 deletions src/Waybill.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ private function __construct(
$this->mpdf->useActiveForms = $config['use_active_forms'];
}

/**
* Run WriteHTML function
*
* @param ?array<string,Tvalue> $args The array of arguments for a model or table fields
*/
private function write(?array $args = null): void
{
$data = [];

if (! is_null($args)) {
$data = $args;
} elseif (! empty($this->state)) {
$data = $this->parcelService->factoryClass()::make()->state($this->state)->definition();
} else {
$data = $this->parcelService->factoryClass()::make()->definition();
}

$this->mpdf->WriteHTML(
Stub::of(
$this->parcelService->stub(),
$data
)->render()
);
}

/**
* Setter for $state
*
Expand Down Expand Up @@ -134,7 +159,8 @@ public function path(string $path): static
*/
public function toArray(): array
{
return $this->parcelService->factoryClass()::make()->state($this->state)->create();
return $this->parcelService->factoryClass()::make()
->state($this->state)->create();
}

/**
Expand All @@ -146,16 +172,25 @@ public function toArray(): array
*/
public function save(string $filename = 'waybills.pdf'): mixed
{
$this->mpdf->WriteHTML(
$html = Stub::of(
$this->parcelService->stub(),
$this->parcelService->factoryClass()::make()->definition()
)->render()
);
$this->write();

$path = empty($this->path) ? '' : $this->path.DIRECTORY_SEPARATOR;

return $this->mpdf->Output($path.$filename, 'F');
return $this->mpdf->Output($path.$filename, false);
}

/**
* Download the waybills data to PDF
*
* @param string $filename A filename to create
*
* @example $waybill = Waybill::of(ParcelService::Cj)->path(realpath(__DIR__.'/../dist'))->save('test.pdf');
*/
public function download(string $filename = 'waybills.pdf'): mixed
{
$this->write();

return $this->mpdf->Output($filename, true);
}

/**
Expand Down
17 changes: 13 additions & 4 deletions tests/WaybillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,24 @@ public function test_path_exists(): void
$this->assertStringContainsString(DIRECTORY_SEPARATOR.'dist', $path->getValue($waybill));
}

public function test_it_export_to_array(): void
public function test_to_array(): void
{
$orderSheet = Waybill::of(ParcelService::Cj)
$waybill = Waybill::of(ParcelService::Cj)
->toArray();

$this->assertIsArray($waybill);
}

public function test_state(): void
{
$waybill = Waybill::of(ParcelService::Cj)
->state(['6' => 'new value'])
->toArray();

$this->assertIsArray($orderSheet);
$this->assertSame('new value', $waybill['6']);
}

public function test_it_export_to_file(): void
public function test_save(): void
{
$waybill = Waybill::of(ParcelService::Cj)
->path(realpath(__DIR__.'/../dist'))
Expand Down

0 comments on commit 02d4a84

Please sign in to comment.