1 | <?php |
||
31 | class NodeObject implements TreeishInterface |
||
32 | { |
||
33 | const TYPE_BLOB = 'blob'; |
||
34 | const TYPE_TREE = 'tree'; |
||
35 | const TYPE_LINK = 'commit'; |
||
36 | |||
37 | /** |
||
38 | * @var \GitElephant\Repository |
||
39 | */ |
||
40 | protected $repository; |
||
41 | |||
42 | /** |
||
43 | * permissions |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $permissions; |
||
48 | |||
49 | /** |
||
50 | * type |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $type; |
||
55 | |||
56 | /** |
||
57 | * sha |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | private $sha; |
||
62 | |||
63 | /** |
||
64 | * size |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | private $size; |
||
69 | |||
70 | /** |
||
71 | * name |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | private $name; |
||
76 | |||
77 | /** |
||
78 | * path |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | private $path; |
||
83 | |||
84 | /** |
||
85 | * create a Object from a single outputLine of the git ls-tree command |
||
86 | * |
||
87 | * @param \GitElephant\Repository $repository repository instance |
||
88 | * @param string $outputLine output from ls-tree command |
||
89 | * |
||
90 | * @see LsTreeCommand::tree |
||
91 | * @return NodeObject |
||
92 | */ |
||
93 | 8 | public static function createFromOutputLine(Repository $repository, string $outputLine) |
|
94 | { |
||
95 | 8 | $slices = static::getLineSlices($outputLine); |
|
96 | 8 | $fullPath = $slices['fullPath']; |
|
97 | 8 | if (false === $pos = mb_strrpos($fullPath, '/')) { |
|
98 | // repository root |
||
99 | 6 | $path = ''; |
|
100 | 6 | $name = $fullPath; |
|
101 | } else { |
||
102 | 4 | $path = substr($fullPath, 0, $pos); |
|
103 | 4 | $name = substr($fullPath, $pos + 1); |
|
104 | } |
||
105 | 8 | return new static( |
|
106 | 8 | $repository, |
|
107 | 8 | $slices['permissions'], |
|
108 | 8 | $slices['type'], |
|
109 | 8 | $slices['sha'], |
|
110 | 8 | $slices['size'], |
|
111 | 8 | $name, |
|
112 | 8 | $path |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Take a line and turn it in slices |
||
118 | * |
||
119 | * @param string $line a single line output from the git binary |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | 14 | public static function getLineSlices(string $line): array |
|
150 | |||
151 | /** |
||
152 | * Class constructor |
||
153 | * |
||
154 | * @param \GitElephant\Repository $repository repository instance |
||
155 | * @param string $permissions node permissions |
||
156 | * @param string $type node type |
||
157 | * @param string $sha node sha |
||
158 | * @param string $size node size in bytes |
||
159 | * @param string $name node name |
||
160 | * @param string $path node path |
||
161 | */ |
||
162 | 14 | public function __construct(Repository $repository, string $permissions, string $type, string $sha, string $size, string $name, string $path) |
|
172 | |||
173 | /** |
||
174 | * toString magic method |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | 1 | public function __toString(): string |
|
182 | |||
183 | /** |
||
184 | * Mime Type getter |
||
185 | * |
||
186 | * @param string $basePath the base path of the repository |
||
187 | * |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getMimeType(string $basePath) |
||
194 | |||
195 | /** |
||
196 | * get extension if it's a blob |
||
197 | * |
||
198 | * @return string|null |
||
199 | */ |
||
200 | public function getExtension(): ?string |
||
209 | |||
210 | /** |
||
211 | * whether the node is a tree |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | 9 | public function isTree(): bool |
|
219 | |||
220 | /** |
||
221 | * whether the node is a link |
||
222 | * |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isLink(): bool |
||
229 | |||
230 | /** |
||
231 | * whether the node is a blob |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 1 | public function isBlob(): bool |
|
239 | |||
240 | /** |
||
241 | * Full path getter |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 12 | public function getFullPath(): string |
|
252 | |||
253 | /** |
||
254 | * permissions getter |
||
255 | * |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getPermissions(): string |
||
262 | |||
263 | /** |
||
264 | * sha getter |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 6 | public function getSha(): string |
|
272 | |||
273 | /** |
||
274 | * type getter |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | 13 | public function getType(): string |
|
282 | |||
283 | /** |
||
284 | * name getter |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 5 | public function getName(): string |
|
292 | |||
293 | /** |
||
294 | * path getter |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getPath(): string |
||
302 | |||
303 | /** |
||
304 | * size getter |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | public function getSize(): string |
||
312 | |||
313 | /** |
||
314 | * gets the last commit in this object |
||
315 | * |
||
316 | * @return Commit |
||
317 | */ |
||
318 | 3 | public function getLastCommit(): ?\GitElephant\Objects\Commit |
|
324 | |||
325 | /** |
||
326 | * rev-parse command - often used to return a commit tag. |
||
327 | * |
||
328 | * @param array $options the options to apply to rev-parse |
||
329 | * |
||
330 | * @throws \RuntimeException |
||
331 | * @throws \InvalidArgumentException |
||
332 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
333 | * @return array |
||
334 | */ |
||
335 | 1 | public function revParse(array $options = []): array |
|
342 | |||
343 | /* |
||
344 | * Repository setter |
||
345 | * |
||
346 | * @param \GitElephant\Repository $repository the repository variable |
||
347 | */ |
||
348 | 1 | public function setRepository(Repository $repository): void |
|
352 | |||
353 | /** |
||
354 | * Repository getter |
||
355 | * |
||
356 | * @return \GitElephant\Repository |
||
357 | */ |
||
358 | 59 | public function getRepository(): \GitElephant\Repository |
|
362 | } |
||
363 |