-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathFormattedBoolWriter.php
49 lines (42 loc) · 1.07 KB
/
FormattedBoolWriter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\Exporter\Writer;
/**
* Format boolean before use another writer.
*
* @author Robin Chalas <[email protected]>
*/
final class FormattedBoolWriter implements WriterInterface
{
public function __construct(
private WriterInterface $writer,
private string $trueLabel = 'yes',
private string $falseLabel = 'no',
) {
}
public function open(): void
{
$this->writer->open();
}
public function close(): void
{
$this->writer->close();
}
public function write(array $data): void
{
foreach ($data as $key => $value) {
if (\is_bool($value)) {
$data[$key] = true === $value ? $this->trueLabel : $this->falseLabel;
}
}
$this->writer->write($data);
}
}