-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonWriter.php
More file actions
33 lines (25 loc) · 728 Bytes
/
JsonWriter.php
File metadata and controls
33 lines (25 loc) · 728 Bytes
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
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Json;
/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
class JsonWriter
{
private readonly JsonEncoder $encoder;
public function __construct(JsonEncoder $encoder = null)
{
$this->encoder = $encoder ?? new JsonEncoder();
}
public function write(string $filename, $value, int $depth = null, int $flags = null): int|false
{
$encoder = $this->encoder;
if (null !== $depth) {
$encoder = $encoder->withDepth($depth);
}
if (null !== $flags) {
$encoder = $encoder->withFlags($flags);
}
return file_put_contents($filename, $encoder->encode($value));
}
}