-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dd5ea0
commit 23d307a
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace BernskioldMedia\LaravelHighcharts\Concerns\Livewire; | ||
|
||
use BernskioldMedia\LaravelHighcharts\Exceptions\ChartExportException; | ||
use function method_exists; | ||
|
||
trait ExportsChart | ||
{ | ||
|
||
/** | ||
* @throws ChartExportException | ||
*/ | ||
public function exportChart(string $type, ?string $chartKey = null, array $exportSettings = [], array $chartOptions = []): void | ||
{ | ||
if ($chartKey === null && property_exists($this, 'chartKey')) { | ||
$chartKey = $this->chartKey; | ||
} | ||
|
||
if ($chartKey === null) { | ||
throw ChartExportException::missingChartKey(); | ||
} | ||
|
||
if (method_exists($this, 'getChartExportSettings')) { | ||
$exportSettings = array_merge( | ||
$this->getChartExportSettings(), | ||
$exportSettings | ||
); | ||
} | ||
|
||
if (method_exists($this, 'getChartOptionsForExport')) { | ||
$chartOptions = array_merge( | ||
$this->getChartOptionsForExport(), | ||
$chartOptions | ||
); | ||
} | ||
|
||
// Livewire 3. | ||
if (method_exists($this, 'dispatch')) { | ||
$this->dispatch( | ||
'exportChart', | ||
chartId: $chartKey, | ||
type: $type, | ||
exportSettings: $exportSettings, | ||
options: $chartOptions, | ||
); | ||
|
||
return; | ||
} | ||
|
||
// Livewire 2. | ||
if (method_exists($this, 'emit')) { | ||
$this->emit('exportChart', [ | ||
'chartId' => $chartKey, | ||
'type' => $type, | ||
'exportSettings' => $exportSettings, | ||
'options' => $chartOptions, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace BernskioldMedia\LaravelHighcharts\Contracts; | ||
|
||
interface CustomizesChartExportOptions | ||
{ | ||
|
||
public function getChartOptionsForExport(): array; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace BernskioldMedia\LaravelHighcharts\Contracts; | ||
|
||
interface CustomizesChartExportSettings | ||
{ | ||
|
||
public function getChartExportSettings(): array; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace BernskioldMedia\LaravelHighcharts\Exceptions; | ||
|
||
use Exception; | ||
|
||
class ChartExportException extends Exception | ||
{ | ||
|
||
public static function missingChartKey(): self | ||
{ | ||
return new static('The chart key is missing. Please provide a chart key to the exportChart() method or set a chartKey property on the component.'); | ||
} | ||
} |