1 | <?php |
||||
2 | |||||
3 | namespace MathieuTu\JsonSyncer\Traits; |
||||
4 | |||||
5 | use Illuminate\Support\Collection; |
||||
6 | use Illuminate\Support\Str; |
||||
7 | use MathieuTu\JsonSyncer\Helpers\JsonExporter as ExporterHelper; |
||||
8 | use MathieuTu\JsonSyncer\Helpers\RelationsInModelFinder; |
||||
9 | |||||
10 | trait JsonExporter |
||||
11 | { |
||||
12 | protected $jsonExportableRelations; |
||||
13 | protected $jsonExportableAttributes; |
||||
14 | |||||
15 | 2 | public function exportToJson($jsonOptions = 0): string |
|||
16 | { |
||||
17 | 2 | return ExporterHelper::exportToJson($this, $jsonOptions); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
18 | } |
||||
19 | |||||
20 | 1 | public function exportToCollection(): Collection |
|||
21 | { |
||||
22 | 1 | return ExporterHelper::exportToCollection($this); |
|||
0 ignored issues
–
show
$this of type MathieuTu\JsonSyncer\Traits\JsonExporter is incompatible with the type MathieuTu\JsonSyncer\Contracts\JsonExportable expected by parameter $exportable of MathieuTu\JsonSyncer\Hel...r::exportToCollection() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
23 | } |
||||
24 | |||||
25 | 3 | public function getJsonExportableAttributes(): array |
|||
26 | { |
||||
27 | 3 | return $this->jsonExportableAttributes ?? array_filter($this->getFillable(), function ($attribute) { |
|||
0 ignored issues
–
show
It seems like
getFillable() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
28 | 3 | return ! Str::endsWith($attribute, '_id'); |
|||
29 | 3 | }); |
|||
30 | } |
||||
31 | |||||
32 | 2 | public function getJsonExportableRelations(): array |
|||
33 | { |
||||
34 | 2 | return $this->jsonExportableRelations ?? RelationsInModelFinder::hasOneOrMany($this); |
|||
0 ignored issues
–
show
$this of type MathieuTu\JsonSyncer\Traits\JsonExporter is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of MathieuTu\JsonSyncer\Hel...lFinder::hasOneOrMany() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
35 | } |
||||
36 | |||||
37 | 1 | public function isExporting(): bool |
|||
38 | { |
||||
39 | 1 | return collect(debug_backtrace())->contains('class', ExporterHelper::class); |
|||
40 | } |
||||
41 | } |
||||
42 |