xref: /plugin/combo/ComboStrap/File.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
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    private $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 false|int - unix time stamp
59     */
60    public function getModifiedTime()
61    {
62        return filemtime($this->path);
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()
89    {
90        return substr($this->getMime(), 0, 5) == 'image';
91    }
92
93    public function getMime()
94    {
95        return mimetype($this->getBaseName(), false)[1];
96    }
97
98    public function getKnownMime()
99    {
100        return mimetype($this->getBaseName(), true)[1];
101    }
102
103    public function getContent()
104    {
105        return file_get_contents($this->getFileSystemPath());
106    }
107
108    public function remove()
109    {
110        unlink($this->getFileSystemPath());
111    }
112
113    public function getParent()
114    {
115        return new File(pathinfo($this->path, PATHINFO_DIRNAME));
116    }
117
118    public function createAsDirectory()
119    {
120
121        return mkdir($this->getFileSystemPath(), $mode = 0770, $recursive = true);
122    }
123
124    public static function createFromPath($path)
125    {
126        return new File($path);
127
128    }
129
130
131
132
133
134}
135