|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the CMS Kernel package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-present LIN3S <[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 LIN3S\CMSKernel\Application\Query\Translation; |
|
13
|
|
|
|
|
14
|
|
|
use LIN3S\CMSKernel\Application\DataTransformer\Translation\TranslatableDataTransformer; |
|
15
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\Locale; |
|
16
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\TranslatableRepository; |
|
17
|
|
|
use LIN3S\CMSKernel\Domain\Model\Translation\TranslatableSpecificationFactory; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Beñat Espiña <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class FilterTranslatablesHandler |
|
23
|
|
|
{ |
|
24
|
|
|
private $dataTransformer; |
|
25
|
|
|
private $repository; |
|
26
|
|
|
private $specificationFactory; |
|
27
|
|
|
private $totalPerPage; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct( |
|
30
|
|
|
TranslatableRepository $repository, |
|
31
|
|
|
TranslatableDataTransformer $dataTransformer, |
|
32
|
|
|
TranslatableSpecificationFactory $specificationFactory, |
|
33
|
|
|
$totalPerPage = 10 |
|
34
|
|
|
) { |
|
35
|
|
|
$this->repository = $repository; |
|
36
|
|
|
$this->dataTransformer = $dataTransformer; |
|
37
|
|
|
$this->specificationFactory = $specificationFactory; |
|
38
|
|
|
$this->totalPerPage = $totalPerPage; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function __invoke(FilterTranslatablesQuery $query) |
|
42
|
|
|
{ |
|
43
|
|
|
$locale = new Locale($query->locale()); |
|
44
|
|
|
|
|
45
|
|
|
$specification = $this->specificationFactory->createFilterTranslatable( |
|
46
|
|
|
$query->filters(), |
|
47
|
|
|
$query->page(), |
|
48
|
|
|
$this->totalPerPage |
|
49
|
|
|
); |
|
50
|
|
|
$translatables = $this->repository->query($specification); |
|
51
|
|
|
$totalTranslatables = $this->repository->count($specification); |
|
52
|
|
|
|
|
53
|
|
|
if (!is_array($translatables)) { |
|
54
|
|
|
$translatables = [$translatables]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return array_merge( |
|
58
|
|
|
[ |
|
59
|
|
|
'translatables' => array_map(function ($translatable) use ($locale) { |
|
60
|
|
|
$this->dataTransformer->write($translatable, $locale); |
|
61
|
|
|
|
|
62
|
|
|
return $this->dataTransformer->read(); |
|
63
|
|
|
}, $translatables), |
|
64
|
|
|
|
|
65
|
|
|
], |
|
66
|
|
|
$this->paginate($translatables, $totalTranslatables, $query->page()) |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function paginate($translatables, $totalTranslatables, $page) |
|
71
|
|
|
{ |
|
72
|
|
|
if (null === $page) { |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
'totalTranslatables' => $totalTranslatables, |
|
78
|
|
|
'initialTranslatableOfPage' => ($page - 1) * $this->totalPerPage + 1, |
|
79
|
|
|
'lastTranslatableOfPage' => ($page - 1) * $this->totalPerPage + count($translatables), |
|
80
|
|
|
'totalPages' => (int) ceil($totalTranslatables / $this->totalPerPage), |
|
81
|
|
|
'currentPage' => $page, |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|