|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Export\Project\Issue; |
|
13
|
|
|
|
|
14
|
|
|
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet; |
|
15
|
|
|
use Tinyissue\Model\Project; |
|
16
|
|
|
use Tinyissue\Services\Exporter; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* CsvHandler is an export class for exporting a project issues into a csv file. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class CsvHandler |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* CSV columns. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $columns = [ |
|
31
|
|
|
'tinyissue.id' => 'id', |
|
32
|
|
|
'tinyissue.project' => 'project', |
|
33
|
|
|
'tinyissue.title' => 'title', |
|
34
|
|
|
'tinyissue.time_quote' => 'time_quote', |
|
35
|
|
|
'tinyissue.label_created' => 'created_at', |
|
36
|
|
|
'tinyissue.updated' => 'updated_at', |
|
37
|
|
|
'tinyissue.label_closed' => 'closed_at', |
|
38
|
|
|
'tinyissue.status' => 'status', |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Exporter $exporter |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function handle(Exporter $exporter) |
|
47
|
|
|
{ |
|
48
|
|
|
// Translate export columns |
|
49
|
3 |
|
$this->columns = array_combine(array_map('trans', array_keys($this->columns)), $this->columns); |
|
50
|
|
|
|
|
51
|
|
|
// Export parameters |
|
52
|
3 |
|
$params = $exporter->getParams(); |
|
53
|
|
|
|
|
54
|
|
|
/** @var Project $project */ |
|
55
|
3 |
|
$project = $params['route']['project']; |
|
56
|
|
|
|
|
57
|
|
|
// Query issues select specific columns |
|
58
|
|
|
$query = $project->issues()->select(array_filter($this->columns, function ($column) { |
|
|
|
|
|
|
59
|
3 |
|
return $column !== 'project'; |
|
60
|
3 |
|
})); |
|
61
|
|
|
|
|
62
|
|
|
// Filter issues |
|
63
|
3 |
|
$project->filterTags($query, $params['tags']); |
|
64
|
3 |
|
$project->filterAssignTo($query, $params['assignto']); |
|
65
|
3 |
|
$project->filterTitleOrBody($query, $params['keyword']); |
|
66
|
|
|
|
|
67
|
|
|
// Fetch issues |
|
68
|
|
|
$issues = $query->get()->map(function (Project\Issue $issue) use ($project) { |
|
69
|
|
|
return array_map(function ($column) use ($issue, $project) { |
|
70
|
|
|
|
|
71
|
3 |
|
if ($column == 'project') { |
|
72
|
3 |
|
return $project->name; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return (string) $issue->$column; |
|
76
|
3 |
|
}, $this->columns); |
|
77
|
3 |
|
}); |
|
78
|
|
|
|
|
79
|
|
|
// Create CSV file |
|
80
|
3 |
|
$exporter->sheet('issues', function (LaravelExcelWorksheet $sheet) use ($issues) { |
|
81
|
3 |
|
$sheet->fromArray($issues); |
|
82
|
3 |
|
}); |
|
83
|
3 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.