1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/data-file |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/data-file/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/data-file |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\DataFile\Node; |
15
|
|
|
|
16
|
|
|
use Graze\DataFile\Format\FormatAwareInterface; |
17
|
|
|
use Graze\DataFile\Format\FormatAwareTrait; |
18
|
|
|
use Graze\DataFile\Modify\Exception\CopyFailedException; |
19
|
|
|
use League\Flysystem\File; |
20
|
|
|
|
21
|
|
|
class FileNode extends File implements FileNodeInterface, FormatAwareInterface |
22
|
|
|
{ |
23
|
|
|
use FormatAwareTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
14 |
|
public function __toString() |
29
|
|
|
{ |
30
|
14 |
|
return $this->getPath(); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
10 |
|
public function getDirectory() |
37
|
|
|
{ |
38
|
10 |
|
return pathinfo($this->path, PATHINFO_DIRNAME) . '/'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
18 |
|
public function getFilename() |
45
|
|
|
{ |
46
|
18 |
|
return pathinfo($this->path, PATHINFO_BASENAME); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns the contents of the file as an array. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
21 |
|
public function getContents() |
55
|
|
|
{ |
56
|
21 |
|
if ($this->exists()) { |
57
|
19 |
|
return explode("\n", trim($this->read())); |
58
|
|
|
} else { |
59
|
2 |
|
return []; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string|null $newPath |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
* @throws CopyFailedException When it is unable to copy the file |
68
|
|
|
*/ |
69
|
4 |
|
public function copy($newPath = null) |
70
|
|
|
{ |
71
|
4 |
|
if (is_null($newPath)) { |
72
|
1 |
|
$newPath = $this->path . '-copy'; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
4 |
|
if (@$this->filesystem->copy($this->path, $newPath)) { |
76
|
3 |
|
return $this->getClone()->setPath($newPath); |
77
|
|
|
} else { |
78
|
1 |
|
$lastError = error_get_last(); |
79
|
1 |
|
throw new CopyFailedException($this, $newPath, $lastError['message']); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return a clone of this object |
85
|
|
|
* |
86
|
|
|
* @return static |
87
|
|
|
*/ |
88
|
56 |
|
public function getClone() |
89
|
|
|
{ |
90
|
56 |
|
return clone $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Clone sub objects |
95
|
|
|
*/ |
96
|
56 |
|
public function __clone() |
97
|
|
|
{ |
98
|
56 |
|
if ($this->format) { |
99
|
1 |
|
$this->format = clone $this->format; |
100
|
1 |
|
} |
101
|
56 |
|
} |
102
|
|
|
} |
103
|
|
|
|