1 | <?php |
||
27 | class FileWriter implements WriterInterface, LoggerAwareInterface |
||
28 | { |
||
29 | use OptionalLoggerTrait; |
||
30 | |||
31 | /** @var FileNodeInterface */ |
||
32 | private $file; |
||
33 | /** @var FormatInterface */ |
||
34 | private $format; |
||
35 | /** @var StreamWriter */ |
||
36 | private $writer; |
||
37 | |||
38 | /** |
||
39 | * FileReader constructor. |
||
40 | * |
||
41 | * @param FileNodeInterface $file |
||
42 | * @param FormatInterface|null $format |
||
43 | * @param FormatterFactoryInterface|null $formatterFactory |
||
44 | */ |
||
45 | 7 | public function __construct( |
|
46 | FileNodeInterface $file, |
||
47 | FormatInterface $format = null, |
||
48 | FormatterFactoryInterface $formatterFactory = null |
||
49 | ) { |
||
50 | 7 | $this->file = $file; |
|
51 | 7 | $this->format = $format; |
|
52 | |||
53 | 7 | if ($this->file instanceof NodeStreamInterface) { |
|
54 | 6 | $stream = $this->file->getStream('a+'); |
|
55 | } else { |
||
56 | 1 | throw new InvalidArgumentException( |
|
57 | 1 | "Only files that implement " . NodeStreamInterface::class . "can be written to" |
|
58 | ); |
||
59 | } |
||
60 | |||
61 | 6 | if (is_null($this->format) |
|
62 | 6 | && $file instanceof FormatAwareInterface |
|
63 | ) { |
||
64 | 1 | $this->format = $file->getFormat(); |
|
65 | } |
||
66 | |||
67 | 6 | if (is_null($this->format)) { |
|
68 | 1 | throw new InvalidArgumentException("No format could be determined from \$file or \$format"); |
|
69 | } |
||
70 | |||
71 | 5 | $factory = $formatterFactory ?: new FormatterFactory(); |
|
72 | 5 | $formatter = $factory->getFormatter($this->format); |
|
73 | |||
74 | 5 | $this->writer = new StreamWriter($stream, $formatter); |
|
75 | 5 | } |
|
76 | |||
77 | /** |
||
78 | * Adds multiple items to the file |
||
79 | * |
||
80 | * @param Traversable|array $rows a multidimensional array or a Traversable object |
||
81 | * |
||
82 | * @throws InvalidArgumentException If the given rows format is invalid |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | 1 | public function insertAll($rows) |
|
91 | |||
92 | /** |
||
93 | * Adds a single item |
||
94 | * |
||
95 | * @param mixed $row an item to insert |
||
96 | * |
||
97 | * @return static |
||
98 | */ |
||
99 | 1 | public function insertOne($row) |
|
104 | } |
||
105 |