1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16class File
17{
18
19    protected $path;
20
21
22    /**
23     * File constructor.
24     * @param $absolutePath
25     */
26    protected function __construct($absolutePath)
27    {
28        $this->path = $absolutePath;
29    }
30
31    /**
32     * @return mixed
33     */
34    public function getFileSystemPath()
35    {
36        return $this->path;
37    }
38
39    /**
40     * @return false|int
41     */
42    public function getSize()
43    {
44        return filesize($this->path);
45    }
46
47    public function exists()
48    {
49        return file_exists($this->path);
50    }
51
52    public function __toString()
53    {
54        return $this->path;
55    }
56
57    /**
58     * @return \DateTime - The date time
59     */
60    public function getModifiedTime(): \DateTime
61    {
62        return Iso8601Date::createFromTimestamp(filemtime($this->path))->getDateTime();
63    }
64
65    /**
66     * @return string the last part of the path without the extension
67     */
68    public function getBaseNameWithoutExtension()
69    {
70
71        return pathinfo($this->path, PATHINFO_FILENAME);
72    }
73
74    public function getExtension()
75    {
76        return pathinfo($this->path, PATHINFO_EXTENSION);
77    }
78
79
80    /**
81     * @return array|string|string[] the last part of the path (ie name + extension)
82     */
83    public function getBaseName()
84    {
85        return pathinfo($this->path, PATHINFO_BASENAME);
86    }
87
88    public function isImage(): bool
89    {
90        return substr($this->getMime(), 0, 5) == 'image';
91    }
92
93    public function getMime()
94    {
95        if ($this->getExtension() == ImageSvg::EXTENSION) {
96            /**
97             * Svg is authorized when viewing but is not part
98             * of the {@link File::getKnownMime()}
99             */
100            return ImageSvg::MIME;
101        } else {
102            return mimetype($this->getBaseName(), false)[1];
103        }
104    }
105
106    public function getKnownMime()
107    {
108        return mimetype($this->getBaseName(), true)[1];
109    }
110
111    public function getContent()
112    {
113        return file_get_contents($this->getFileSystemPath());
114    }
115
116    public function remove()
117    {
118        unlink($this->getFileSystemPath());
119    }
120
121    public function getParent()
122    {
123        return new File(pathinfo($this->path, PATHINFO_DIRNAME));
124    }
125
126    public function createAsDirectory()
127    {
128
129        return mkdir($this->getFileSystemPath(), $mode = 0770, $recursive = true);
130    }
131
132    public static function createFromPath($path)
133    {
134        return new File($path);
135
136    }
137
138
139
140
141
142}
143