Skip to content

Commit 2260199

Browse files
author
Denis
committed
[*] mini models upd
1 parent c5dc52c commit 2260199

File tree

6 files changed

+107
-50
lines changed

6 files changed

+107
-50
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class ExportController extends AbstractController
8585
```
8686
![](doc/0000.PNG)
8787

88-
8988
# Customization
9089

9190
| Method | Return | Official Documentation |
@@ -94,8 +93,17 @@ class ExportController extends AbstractController
9493
| getSpreadsheet() | Class Spreadsheet | https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet.html |
9594
| getActiveSheet() | Class Worksheet | https://phpoffice.github.io/PhpSpreadsheet/namespaces/phpoffice-phpspreadsheet-worksheet.html |
9695

97-
9896
```php
97+
use \Denisok94\SymfonyExportXlsxBundle\Service\XlsxService;
98+
/** @var XlsxService */
99+
private $export;
100+
/**
101+
* @param XlsxService $export
102+
*/
103+
public function __construct(XlsxService $export)
104+
{
105+
$this->export = $export;
106+
}
99107
/**
100108
* @return Response
101109
*/
@@ -161,7 +169,6 @@ services:
161169
- { name: sonata.exporter.writer }
162170
```
163171
164-
165172
add in `YourAdmin` class [according to the documentation](https://docs.sonata-project.org/projects/SonataAdminBundle/en/4.x/reference/action_export/):
166173
```php
167174
public function getExportFormats(): array
@@ -181,6 +188,7 @@ protected function configureExportFields(): array
181188
];
182189
}
183190
```
191+
184192
# Errors
185193

186194
If you see the error:

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"email": "denisok94@inbox.ru"
1111
}
1212
],
13-
"version": "0.0.6",
13+
"version": "0.0.7",
1414
"require": {
1515
"denisok94/helper-composer": "^0.0.7",
1616
"phpoffice/phpspreadsheet": "^1.23"

src/Model/BaseExport.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Denisok94\SymfonyExportXlsxBundle\Export\ExportInterface;
99
use Denisok94\SymfonyExportXlsxBundle\Service\XlsxService;
1010
use PhpOffice\PhpSpreadsheet\Spreadsheet;
11+
use PhpOffice\PhpSpreadsheet\RichText\RichText;
1112

1213
abstract class BaseExport implements ExportBaseInterface
1314
{
@@ -66,24 +67,55 @@ public function preCallback(ExportInterface $export): void
6667
/**
6768
* {@inheritdoc}
6869
*/
69-
public function preCallbackItem($item, $result, $i): void
70+
public function preCallbackItem($item, $export, $i): void
7071
{
7172
}
7273

7374
/**
7475
* {@inheritdoc}
7576
*/
76-
public function postCallbackItem($item, $result, $i): void
77+
public function getItem($line): ItemExport
78+
{
79+
$item = [];
80+
try {
81+
$this->parse($item, $line);
82+
} catch (\Throwable $th) {
83+
throw new ExportException($th->getMessage());
84+
}
85+
return (new ItemExport())->setPageName('Лист 1')->setPageHeaders(array_keys($item))->setPageData($item);
86+
}
87+
88+
/**
89+
* @param array $array
90+
* @param array $line
91+
*/
92+
public function parse(&$array, $line): void
93+
{
94+
foreach ($line as $key => $value) {
95+
if ($value instanceof RichText) {
96+
$array[$key] = $value;
97+
} elseif (!is_object($value) && !is_array($value)) {
98+
$array[$key] = $value;
99+
} elseif (is_object($value) || is_array($value)) {
100+
$this->parse($array, $value);
101+
}
102+
}
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function postCallbackItem($item, $export, $i): void
77109
{
78110
}
79111

80112
/**
81113
* {@inheritdoc}
82114
*/
83-
public function callback($result): void
115+
public function callback($export): void
84116
{
85-
if ($result instanceof XlsxService) {
86-
$worksheet = $result->getActiveSheet();
117+
if ($export instanceof XlsxService) {
118+
$worksheet = $export->getActiveSheet();
87119
$x = $y = 1;
88120
while ($worksheet->getCell([$y, 1])->getValue() != null) {
89121
$y++;

src/Model/ExportBaseInterface.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ public function getCountItems(): int;
6161

6262
/**
6363
* Готовые данные для вставки в файл экспорта
64-
* @param mixed $item
64+
* @param mixed $line
6565
* @return ExportItemInterface
6666
* @throws ExportException
6767
*/
68-
public function getItem($item): ExportItemInterface;
68+
public function getItem($line): ExportItemInterface;
6969

7070
/**
7171
* Подготовка к экспорту
@@ -75,25 +75,25 @@ public function getItem($item): ExportItemInterface;
7575
public function preCallback(ExportInterface $export): void;
7676

7777
/**
78-
* @param mixed $item
79-
* @param mixed $result объект экспорта
78+
* @param mixed $line
79+
* @param ExportInterface $export объект экспорта
8080
* @param int $i
8181
* @throws ExportException
8282
*/
83-
public function preCallbackItem($item, $result, $i): void;
83+
public function preCallbackItem($line, $export, $i): void;
8484

8585
/**
86-
* @param mixed $item
87-
* @param mixed $result объект экспорта
86+
* @param mixed $line
87+
* @param ExportInterface $export объект экспорта
8888
* @param int $i
8989
* @throws ExportException
9090
*/
91-
public function postCallbackItem($item, $result, $i): void;
91+
public function postCallbackItem($line, $export, $i): void;
9292

9393
/**
9494
* Получить готовый объект/файл полученного экспорта
95-
* @param mixed $export объект экспорта
95+
* @param ExportInterface $export объект экспорта
9696
* @throws ExportException
9797
*/
98-
public function callback($result): void;
98+
public function callback($export): void;
9999
}

src/Model/ExportItemInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
interface ExportItemInterface
66
{
77
/**
8-
* Задать название таблицы
8+
* Название таблицы
99
* @return string
1010
*/
1111
public function getPageName(): string;

src/Model/ItemExport.php

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,84 @@
1010
*/
1111
class ItemExport implements ExportItemInterface
1212
{
13-
public string $pageName;
14-
public $pageData;
15-
public $pageHeaders;
13+
/**
14+
* Название страницы
15+
* @var string
16+
*/
17+
public string $pageName = '';
18+
19+
/**
20+
* Массив с данными
21+
* @var array
22+
* ```php
23+
* $item = [
24+
* 'name' => 'Ivanov',
25+
* 'value' => 32
26+
* ];
27+
* ```
28+
*/
29+
public array $pageData = [];
30+
31+
/**
32+
* Список заголовков столбцов
33+
* @var array
34+
* ```php
35+
* $headers = [
36+
* 'name',
37+
* 'value',
38+
* ];
39+
* ```
40+
*/
41+
public array $pageHeaders = [];
1642

1743
/**
18-
* Get the value of pageData
19-
* @return mixed
44+
* {@inheritdoc}
2045
*/
21-
public function getPageData(): mixed
46+
public function getPageName(): string
2247
{
23-
return $this->pageData;
48+
return $this->pageName;
2449
}
2550

2651
/**
27-
* Set the value of pageData
28-
* @param mixed $pageData
29-
* @return self
52+
* {@inheritdoc}
3053
*/
31-
public function setPageData($pageData): self
54+
public function setPageName(string $pageName): self
3255
{
33-
$this->pageData = $pageData;
56+
$this->pageName = $pageName;
3457
return $this;
3558
}
3659

3760
/**
38-
* Get the value of pageHeaders
39-
* @return mixed
61+
* {@inheritdoc}
4062
*/
41-
public function getPageHeaders(): mixed
63+
public function getPageData(): array
4264
{
43-
return $this->pageHeaders;
65+
return $this->pageData;
4466
}
4567

4668
/**
47-
* Set the value of pageHeaders
48-
* @param mixed $pageHeaders
49-
* @return self
69+
* {@inheritdoc}
5070
*/
51-
public function setPageHeaders($pageHeaders): self
71+
public function setPageData($pageData): self
5272
{
53-
$this->pageHeaders = $pageHeaders;
73+
$this->pageData = $pageData;
5474
return $this;
5575
}
5676

5777
/**
58-
* Get the value of pageName
59-
* @return string
78+
* {@inheritdoc}
6079
*/
61-
public function getPageName(): string
80+
public function getPageHeaders(): array
6281
{
63-
return $this->pageName;
82+
return $this->pageHeaders;
6483
}
6584

6685
/**
67-
* Set the value of pageName
68-
* @param string $pageName
69-
* @return self
86+
* {@inheritdoc}
7087
*/
71-
public function setPageName(string $pageName): self
88+
public function setPageHeaders($pageHeaders): self
7289
{
73-
$this->pageName = $pageName;
90+
$this->pageHeaders = $pageHeaders;
7491
return $this;
7592
}
7693
}

0 commit comments

Comments
 (0)