|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pace\Report; |
| 4 | + |
| 5 | +use Pace\Model; |
| 6 | +use InvalidArgumentException; |
| 7 | +use Pace\Enum\ReportExportType; |
| 8 | +use Pace\Services\ReportService; |
| 9 | + |
| 10 | +class Builder |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The report service. |
| 14 | + * |
| 15 | + * @var ReportService |
| 16 | + */ |
| 17 | + protected $service; |
| 18 | + |
| 19 | + /** |
| 20 | + * The report model. |
| 21 | + * |
| 22 | + * @var Model |
| 23 | + */ |
| 24 | + protected $report; |
| 25 | + |
| 26 | + /** |
| 27 | + * The base object key (if applicable). |
| 28 | + * |
| 29 | + * @var null |
| 30 | + */ |
| 31 | + protected $baseObjectKey = null; |
| 32 | + |
| 33 | + /** |
| 34 | + * The report parameters. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + protected $parameters = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * The report export media types. |
| 42 | + * |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + protected $mediaTypes = [ |
| 46 | + ReportExportType::PDF => 'application/pdf', |
| 47 | + ReportExportType::RTF => 'text/rtf', |
| 48 | + ReportExportType::HTML => 'text/html', |
| 49 | + ReportExportType::CSV => 'text/csv', |
| 50 | + ReportExportType::XLS => 'application/vnd.ms-excel', |
| 51 | + ReportExportType::XLSX => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 52 | + ReportExportType::XML => 'application/xml', |
| 53 | + ReportExportType::TXT => 'text/plain', |
| 54 | + ]; |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a new report builder instance. |
| 58 | + * |
| 59 | + * @param ReportService $service |
| 60 | + * @param Model $report |
| 61 | + */ |
| 62 | + public function __construct(ReportService $service, Model $report) |
| 63 | + { |
| 64 | + $this->service = $service; |
| 65 | + $this->report = $report; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the specified parameter. |
| 70 | + * |
| 71 | + * @param int $id |
| 72 | + * @param mixed $value |
| 73 | + * @return $this |
| 74 | + */ |
| 75 | + public function parameter(int $id, $value): self |
| 76 | + { |
| 77 | + $this->parameters[$id] = $value; |
| 78 | + |
| 79 | + return $this; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Set the specified parameter by looking up its name. |
| 84 | + * |
| 85 | + * @param string $name |
| 86 | + * @param mixed $value |
| 87 | + * @return $this |
| 88 | + */ |
| 89 | + public function namedParameter(string $name, $value): self |
| 90 | + { |
| 91 | + $id = $this->report->reportParameters()->filter('@name', $name)->get()->key(); |
| 92 | + |
| 93 | + if (false === $id) { |
| 94 | + throw new InvalidArgumentException("Parameter [$name] does not exist"); |
| 95 | + } |
| 96 | + |
| 97 | + return $this->parameter($id, $value); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Bulk set parameters. |
| 102 | + * |
| 103 | + * @param array $parameters |
| 104 | + * @return $this |
| 105 | + */ |
| 106 | + public function parameters(array $parameters): self |
| 107 | + { |
| 108 | + foreach ($parameters as $id => $value) { |
| 109 | + $this->parameter($id, $value); |
| 110 | + } |
| 111 | + |
| 112 | + return $this; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Set the base object key. |
| 117 | + * |
| 118 | + * @param mixed $key |
| 119 | + * @return $this |
| 120 | + */ |
| 121 | + public function baseObjectKey($key): self |
| 122 | + { |
| 123 | + $this->baseObjectKey = $key instanceof Model ? $key->key() : $key; |
| 124 | + |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Run the report and get the file. |
| 130 | + * |
| 131 | + * @return File |
| 132 | + */ |
| 133 | + public function get(): File |
| 134 | + { |
| 135 | + $report = $this->service->executeReport($this->toWrapper()); |
| 136 | + |
| 137 | + return File::fromBase64($report['content'], $this->mediaTypes[$this->report->exportType] ?? null); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Print the report. |
| 142 | + */ |
| 143 | + public function print(): void |
| 144 | + { |
| 145 | + $this->service->printReport($this->toWrapper()); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Convert the instance to a report wrapper. |
| 150 | + * |
| 151 | + * @return array |
| 152 | + */ |
| 153 | + public function toWrapper(): array |
| 154 | + { |
| 155 | + $parameters = []; |
| 156 | + |
| 157 | + foreach ($this->parameters as $id => $value) { |
| 158 | + $parameters[] = [ |
| 159 | + 'reportParameterId' => $id, |
| 160 | + 'value' => $value, |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + return [ |
| 165 | + 'baseObjectKey' => $this->baseObjectKey, |
| 166 | + 'reportId' => $this->report->key(), |
| 167 | + 'reportParameterWrappers' => $parameters, |
| 168 | + ]; |
| 169 | + } |
| 170 | +} |
0 commit comments