|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Reports\Core; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Formatters\Contracts\FormatterInterface; |
|
6
|
|
|
use AlecRabbit\Formatters\Core\AbstractFormatter; |
|
7
|
|
|
use AlecRabbit\Formatters\DefaultFormatter; |
|
8
|
|
|
use AlecRabbit\Reports\Contracts\ReportableInterface; |
|
9
|
|
|
use AlecRabbit\Reports\DefaultReport; |
|
10
|
|
|
use Illuminate\Contracts\Container\BindingResolutionException; |
|
11
|
|
|
use function AlecRabbit\container; |
|
12
|
|
|
|
|
13
|
|
|
abstract class AbstractReportable implements ReportableInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $reportClass; |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $formatterClass; |
|
19
|
|
|
|
|
20
|
2 |
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
2 |
|
$this->setBindings(); |
|
23
|
2 |
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
protected function setBindings(string $reportClass = null, string $formatterClass = null): void |
|
26
|
|
|
{ |
|
27
|
2 |
|
$this->reportClass = $reportClass ?? DefaultReport::class; |
|
28
|
2 |
|
$this->formatterClass = $formatterClass ?? DefaultFormatter::class; |
|
29
|
2 |
|
$this->setReportFormatterDependencies( |
|
30
|
2 |
|
$this->reportClass, |
|
31
|
2 |
|
FormatterInterface::class, |
|
32
|
2 |
|
$this->formatterClass, |
|
33
|
2 |
|
null |
|
34
|
|
|
); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $reportClass |
|
39
|
|
|
* @param string $formatterClass |
|
40
|
|
|
* @param AbstractFormatter|string|\Closure $formatter |
|
41
|
|
|
* @param null|int $options |
|
42
|
|
|
* |
|
43
|
|
|
* @psalm-suppress InvalidScalarArgument |
|
44
|
|
|
*/ |
|
45
|
2 |
|
protected function setReportFormatterDependencies( |
|
46
|
|
|
string $reportClass, |
|
47
|
|
|
string $formatterClass, |
|
48
|
|
|
$formatter, |
|
49
|
|
|
?int $options |
|
50
|
|
|
): void { |
|
51
|
2 |
|
if ($formatter instanceof AbstractFormatter) { |
|
52
|
|
|
$formatter = static function () use ($formatter): AbstractFormatter { |
|
53
|
1 |
|
return $formatter; |
|
54
|
1 |
|
}; |
|
55
|
|
|
} |
|
56
|
2 |
|
container() |
|
57
|
2 |
|
->when($reportClass) |
|
58
|
2 |
|
->needs($formatterClass) |
|
59
|
2 |
|
->give($formatter); |
|
60
|
2 |
|
if (is_string($formatter)) { |
|
61
|
2 |
|
container() |
|
62
|
2 |
|
->when($formatter) |
|
63
|
2 |
|
->needs('$options') |
|
64
|
2 |
|
->give($options); |
|
65
|
|
|
} |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param null|AbstractFormatter|string|\Closure $formatter |
|
70
|
|
|
* @param null|int $options |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function setFormatter($formatter = null, ?int $options = null): void |
|
73
|
|
|
{ |
|
74
|
|
|
/** @noinspection ProperNullCoalescingOperatorUsageInspection */ |
|
75
|
1 |
|
$formatter = $formatter ?? $this->formatterClass; |
|
76
|
|
|
|
|
77
|
1 |
|
$this->setReportFormatterDependencies( |
|
78
|
1 |
|
$this->reportClass, |
|
79
|
1 |
|
FormatterInterface::class, |
|
80
|
1 |
|
$formatter, |
|
81
|
1 |
|
$options |
|
82
|
|
|
); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @noinspection ReturnTypeCanBeDeclaredInspection |
|
87
|
|
|
* |
|
88
|
|
|
* @return AbstractReport |
|
89
|
|
|
* @throws BindingResolutionException |
|
90
|
|
|
* |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function report(): AbstractReport |
|
93
|
|
|
{ |
|
94
|
2 |
|
return container()->make($this->reportClass, ['reportable' => $this]); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|