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
16use DateTime;
17
18/**
19 * Class File
20 * @package ComboStrap
21 * @deprecated for {@link LocalPath} and {@link FileSystems}
22 */
23class File
24{
25
26    protected $path;
27
28
29    /**
30     * File constructor.
31     * @param $absolutePath
32     */
33    protected function __construct($absolutePath)
34    {
35        $this->path = $absolutePath;
36    }
37
38    /**
39     * @deprecated uses {@link Path::toAbsolutePath()} instead
40     * @return mixed
41     */
42    public function getAbsoluteFileSystemPath()
43    {
44        return $this->path;
45    }
46
47    /**
48     * @deprecated use {@link FileSystems::getSize()} instead
49     * @return false|int
50     */
51    public function getSize()
52    {
53        return filesize($this->path);
54    }
55
56    /**
57     * @return bool
58     * @deprecated uses {@link FileSystems::exists()} instead
59     */
60    public function exists()
61    {
62        return file_exists($this->path);
63    }
64
65    public function __toString()
66    {
67        return $this->path;
68    }
69
70    /**
71     * @return null|DateTime - The date time
72     * @deprecated use {@link FileSystems::getModifiedTime()} instead
73     */
74    public function getModifiedTime(): ?DateTime
75    {
76        if(!$this->exists()){
77            return null;
78        }
79        return Iso8601Date::createFromTimestamp(filemtime($this->path))->getDateTime();
80    }
81
82
83
84
85    /**
86     * @deprecated use {@link FileSystems::getContent()} instead
87     * @return false|string
88     */
89    public function getTextContent()
90    {
91        return file_get_contents($this->getAbsoluteFileSystemPath());
92    }
93
94    /**
95     * @deprecated use {@link FileSystems::delete()} instead
96     */
97    public function remove()
98    {
99        unlink($this->getAbsoluteFileSystemPath());
100    }
101
102    /**
103     * @return File|null
104     * @deprecated use {@link LocalPath::getParent()} instead
105     */
106    public function getParent(): ?File
107    {
108        $absolutePath = pathinfo($this->path, PATHINFO_DIRNAME);
109        if(empty($absolutePath)){
110            return null;
111        }
112        return new File($absolutePath);
113    }
114
115    public function createAsDirectory(): bool
116    {
117
118        return mkdir($this->getAbsoluteFileSystemPath(), $mode = 0770, $recursive = true);
119    }
120
121    public static function createFromPath($path): File
122    {
123        return new File($path);
124
125    }
126
127    /**
128     * @deprecated for {@link ResourceCombo::getBuster()}
129     * @return string
130     */
131    public function getBuster(): string
132    {
133        return strval($this->getModifiedTime()->getTimestamp());
134    }
135
136    /**
137     * @deprecated uses {@link FileSystems::getCreationTime()} instead
138     * @return DateTime|false|mixed
139     */
140    public function getCreationTime()
141    {
142        return Iso8601Date::createFromTimestamp(filectime($this->path))->getDateTime();
143    }
144
145    /**
146     * @deprecated use {@link FileSystems::deleteIfExists()} instead
147     */
148    public function removeIfExists(): File
149    {
150        if($this->exists()){
151            $this->remove();
152        }
153        return $this;
154    }
155
156
157}
158