|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LPDFBuilder\Generation; |
|
4
|
|
|
|
|
5
|
|
|
use Barryvdh\Snappy\Facades\SnappyImage; |
|
6
|
|
|
use Barryvdh\Snappy\Facades\SnappyPdf; |
|
7
|
|
|
use Illuminate\Http\Response; |
|
8
|
|
|
use Illuminate\Support\Arr; |
|
9
|
|
|
use Illuminate\Support\Facades\Storage; |
|
10
|
|
|
use Illuminate\Support\Facades\View; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractDocumentFromHtml implements DocumentGenerator |
|
14
|
|
|
{ |
|
15
|
|
|
protected string $fileExtension = 'pdf'; |
|
16
|
|
|
|
|
17
|
|
|
protected mixed $certificateData = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* List of possible extensions. |
|
21
|
|
|
* |
|
22
|
|
|
* @return array |
|
23
|
|
|
*/ |
|
24
|
|
|
public function possibleExtensions(): array |
|
25
|
|
|
{ |
|
26
|
|
|
return [Extension::PDF->value, Extension::JPG->value]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function extension(string|Extension $fileExtension = 'pdf'): static |
|
33
|
|
|
{ |
|
34
|
|
|
if ($fileExtension instanceof Extension) { |
|
35
|
|
|
$fileExtension = $fileExtension->value; |
|
36
|
|
|
} |
|
37
|
|
|
if (!in_array($fileExtension, $this->possibleExtensions())) { |
|
38
|
|
|
throw new \Exception('Not valid extension. Allowed: '.implode(', ', $this->possibleExtensions())); |
|
39
|
|
|
} |
|
40
|
|
|
$this->fileExtension = $fileExtension; |
|
41
|
|
|
|
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
abstract public function viewName(): string; |
|
47
|
|
|
|
|
48
|
|
|
public function headerViewName(): ?string |
|
49
|
|
|
{ |
|
50
|
|
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function footerViewName(): ?string |
|
54
|
|
|
{ |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function viewData(): array |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
'certificate' => $this->certificateData, |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @inheritDoc |
|
67
|
|
|
*/ |
|
68
|
|
|
public function asView(): ?\Illuminate\Contracts\View\View |
|
69
|
|
|
{ |
|
70
|
|
|
return View::make($this->viewName(), $this->viewData()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return \Barryvdh\Snappy\PdfWrapper|\Barryvdh\Snappy\ImageWrapper |
|
75
|
|
|
*/ |
|
76
|
|
|
public function file(): \Barryvdh\Snappy\PdfWrapper|\Barryvdh\Snappy\ImageWrapper |
|
77
|
|
|
{ |
|
78
|
|
|
$file = match ($this->fileExtension) { |
|
79
|
|
|
Extension::JPG->value => SnappyImage::loadView($this->viewName(), $this->viewData()), |
|
80
|
|
|
default => SnappyPdf::loadView($this->viewName(), $this->viewData()), |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
try { |
|
84
|
|
|
if ($headerViewName = $this->headerViewName()) { |
|
|
|
|
|
|
85
|
|
|
$file->setOption('header-html', view($headerViewName, $this->viewData())->render()); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
if ($footerViewName = $this->footerViewName()) { |
|
|
|
|
|
|
88
|
|
|
$file->setOption('footer-html', view($footerViewName, $this->viewData())->render()); |
|
89
|
|
|
} |
|
90
|
|
|
} catch (\InvalidArgumentException) { |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $file; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritDoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function temporalFile(?string $filename = null): ?string |
|
100
|
|
|
{ |
|
101
|
|
|
$filePath = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) |
|
102
|
|
|
.DIRECTORY_SEPARATOR |
|
103
|
|
|
.ltrim($this->generateName($filename), DIRECTORY_SEPARATOR); |
|
104
|
|
|
$this->file()->save($filePath, true); |
|
105
|
|
|
|
|
106
|
|
|
return $filePath; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritDoc |
|
111
|
|
|
*/ |
|
112
|
|
|
public function inline(?string $filename = null): Response |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->file()->inline(Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @inheritDoc |
|
119
|
|
|
*/ |
|
120
|
|
|
public function download(?string $filename = null): Response |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->file()->download(Str::afterLast($this->generateName($filename), DIRECTORY_SEPARATOR)); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
|
|
public function save(?string $disc = null, ?string $filename = null, $overwrite = false, array $options = []): static |
|
129
|
|
|
{ |
|
130
|
|
|
$storage = Storage::disk($disc); |
|
131
|
|
|
$this->file()->save($storage->path($this->generateName($filename)), $overwrite); |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritDoc |
|
138
|
|
|
*/ |
|
139
|
|
|
public function generateName(?string $defaultName = null): string |
|
140
|
|
|
{ |
|
141
|
|
|
if ($defaultName) { |
|
142
|
|
|
if (Str::endsWith($defaultName, ".{$this->fileExtension}")) { |
|
143
|
|
|
$defaultName = Str::beforeLast($defaultName, ".{$this->fileExtension}"); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $defaultName.'.'.$this->fileExtension; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return Arr::get($this->certificateData, 'uuid', Arr::get($this->certificateData, 'id', Str::random())).".{$this->fileExtension}"; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.