1 | <?php |
||
24 | final class File implements FileInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $path; |
||
30 | |||
31 | /** |
||
32 | * @var string; |
||
33 | */ |
||
34 | private $rootPath; |
||
35 | |||
36 | /** |
||
37 | * @var string; |
||
38 | */ |
||
39 | private $relativePath; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | private $size; |
||
45 | |||
46 | /** |
||
47 | * @var \DateTimeInterface |
||
48 | */ |
||
49 | private $createdAt; |
||
50 | |||
51 | /** |
||
52 | * @var \DateTimeInterface |
||
53 | */ |
||
54 | private $modifiedAt; |
||
55 | |||
56 | 32 | public function __construct($path, $rootPath, $size, $createdAt, $modifiedAt) |
|
57 | { |
||
58 | 32 | $this->path = $path; |
|
59 | 32 | $this->rootPath = rtrim(is_null($rootPath) ? '' : $rootPath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
|
60 | 32 | $this->size = $size; |
|
61 | 32 | $this->createdAt = (is_numeric($createdAt)) ? date_timestamp_set(new \DateTime(), $createdAt) : clone $createdAt; |
|
62 | 32 | $this->modifiedAt = (is_numeric($modifiedAt)) ? date_timestamp_set(new \DateTime(), $modifiedAt) : clone $modifiedAt; |
|
63 | 32 | $this->relativePath = null; |
|
64 | 32 | } |
|
65 | |||
66 | /** |
||
67 | * {@inheritdoc} |
||
68 | */ |
||
69 | 24 | public function getPath() |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function getRootPath() |
||
78 | { |
||
79 | return $this->rootPath; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | 24 | public function getRelativePath() |
|
86 | { |
||
87 | 24 | if (is_null($this->relativePath)) { |
|
88 | |||
89 | 24 | $pos = strpos($this->path, $this->rootPath); |
|
90 | |||
91 | 24 | if ($pos === 0) { |
|
92 | 24 | $this->relativePath = substr_replace($this->path, '', $pos, strlen($this->rootPath)); |
|
93 | 12 | } else { |
|
94 | $this->relativePath = $this->path; |
||
95 | } |
||
96 | 12 | } |
|
97 | |||
98 | 24 | return $this->relativePath; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 18 | public function getSize() |
|
105 | { |
||
106 | 18 | return $this->size; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function getCreatedAt() |
||
113 | { |
||
114 | return clone $this->createdAt; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 2 | public function getModifiedAt() |
|
121 | { |
||
122 | 2 | return clone $this->modifiedAt; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Create File instance from local, mounted filesystem. |
||
127 | * |
||
128 | * @param string $path Path to file. |
||
129 | * @param null|string $rootPath Root path of file. |
||
130 | * @return File Created backup file instance. |
||
131 | */ |
||
132 | 32 | public static function fromLocal($path, $rootPath = null) |
|
133 | { |
||
134 | 32 | return new static( |
|
135 | 16 | $path, |
|
136 | 16 | $rootPath, |
|
137 | 16 | filesize($path), |
|
138 | 16 | filectime($path), |
|
139 | 16 | filemtime($path) |
|
140 | 16 | ); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * Create file instance from \SplFileInfo instance. |
||
145 | * |
||
146 | * @param \SplFileInfo $file |
||
147 | * @param null|string $rootPath Root path of file. |
||
148 | * @return File |
||
149 | */ |
||
150 | 10 | public static function fromSplFileInfo(\SplFileInfo $file, $rootPath = null) |
|
151 | { |
||
152 | 10 | return new static( |
|
153 | 10 | $file->getPathname(), |
|
154 | 5 | $rootPath, |
|
155 | 10 | $file->getSize(), |
|
156 | 10 | $file->getCTime(), |
|
157 | 10 | $file->getMTime() |
|
158 | 5 | ); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Create file instance from Flysystem file metadata. |
||
163 | * |
||
164 | * @param array $metadata Flysystem file metadata. |
||
165 | * @param null|string $rootPath Root path of file. |
||
166 | * @return File |
||
167 | */ |
||
168 | 4 | public static function fromFlysystemMetadata(array $metadata, $rootPath = null) |
|
178 | } |
||
179 |