1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of YaEtl |
||
5 | * (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
||
6 | * This source file is licensed under the MIT license which you will |
||
7 | * find in the LICENSE file or at https://opensource.org/licenses/MIT |
||
8 | */ |
||
9 | |||
10 | namespace fab2s\YaEtl\Loaders\File; |
||
11 | |||
12 | use fab2s\NodalFlow\NodalFlowException; |
||
13 | use fab2s\YaEtl\Loaders\LoaderAbstract; |
||
14 | use fab2s\YaEtl\Traits\FileHandlerTrait; |
||
15 | use fab2s\YaEtl\YaEtlException; |
||
16 | |||
17 | /** |
||
18 | * Class FileLoaderAbstract |
||
19 | */ |
||
20 | abstract class FileLoaderAbstract extends LoaderAbstract |
||
21 | { |
||
22 | use FileHandlerTrait; |
||
23 | |||
24 | /** |
||
25 | * @param resource|string $input |
||
26 | * |
||
27 | * @throws YaEtlException |
||
28 | * @throws NodalFlowException |
||
29 | */ |
||
30 | public function __construct($input) |
||
31 | { |
||
32 | $this->checkHandle($input) |
||
33 | ->initHandle($input, 'wb'); |
||
34 | parent::__construct(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return static |
||
39 | */ |
||
40 | public function writeBom(): self |
||
41 | { |
||
42 | if ($this->useBom && ($bom = $this->prependBom(''))) { |
||
43 | fwrite($this->handle, $bom); |
||
44 | } |
||
45 | |||
46 | return $this; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @param resource|string $input |
||
51 | * |
||
52 | * @throws YaEtlException |
||
53 | * |
||
54 | * @return static |
||
55 | */ |
||
56 | protected function checkHandle($input): self |
||
57 | { |
||
58 | if (is_resource($input)) { |
||
59 | $metaData = stream_get_meta_data($input); |
||
60 | if (!is_writable($metaData['uri'])) { |
||
61 | throw new YaEtlException((new \ReflectionClass($this))->getShortName() . ' : destination cannot be opened in write mode'); |
||
62 | } |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | if ( |
||
68 | !is_string($input) || |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
69 | ( |
||
70 | !is_file($input) && |
||
71 | !touch($input) |
||
72 | ) |
||
73 | ) { |
||
74 | throw new YaEtlException((new \ReflectionClass($this))->getShortName() . ' : destination cannot be created'); |
||
75 | } |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | } |
||
80 |