|
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\Etl\Streams\Traits; |
|
11
|
|
|
|
|
12
|
|
|
trait StreamWrite |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $destinationPath = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $destinationFileName; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var resource|null |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $destinationStream = null; |
|
28
|
|
|
|
|
29
|
|
|
public function getDestinationFileName(): string |
|
30
|
|
|
{ |
|
31
|
|
|
if (!isset($this->destinationFileName)) { |
|
32
|
|
|
if (is_callable([$this, 'getSourceFileName'])) { |
|
33
|
|
|
return $this->destinationFileName = $this->getSourceFileName(); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this->destinationFileName; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setDestinationFileName(string $destinationFileName): self |
|
41
|
|
|
{ |
|
42
|
|
|
$this->destinationFileName = basename($destinationFileName); |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* @return null|false|resource |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getDestinationStream(bool $fresh = false) |
|
50
|
|
|
{ |
|
51
|
|
|
if (!$fresh && !empty($this->destinationStream)) { |
|
52
|
|
|
return $this->destinationStream; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->destinationStream = fopen($this->getDestinationFilePath(), 'wb'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getDestinationFilePath(): string |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getDestinationPath() . $this->getDestinationFileName(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getDestinationPath(): string |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->destinationPath; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function setDestinationPath(string $destinationPath): self |
|
69
|
|
|
{ |
|
70
|
|
|
$this->destinationPath = $this->slashPath($destinationPath); |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
abstract public function slashPath(string $path) : string; |
|
76
|
|
|
} |
|
77
|
|
|
|