1<?php
2
3declare(strict_types=1);
4
5namespace Sabre\DAV;
6
7/**
8 * SimpleFile.
9 *
10 * The 'SimpleFile' class is used to easily add read-only immutable files to
11 * the directory structure. One usecase would be to add a 'readme.txt' to a
12 * root of a webserver with some standard content.
13 *
14 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
15 * @author Evert Pot (http://evertpot.com/)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class SimpleFile extends File
19{
20    /**
21     * File contents.
22     *
23     * @var string
24     */
25    protected $contents = [];
26
27    /**
28     * Name of this resource.
29     *
30     * @var string
31     */
32    protected $name;
33
34    /**
35     * A mimetype, such as 'text/plain' or 'text/html'.
36     *
37     * @var string
38     */
39    protected $mimeType;
40
41    /**
42     * Creates this node.
43     *
44     * The name of the node must be passed, as well as the contents of the
45     * file.
46     *
47     * @param string      $name
48     * @param string      $contents
49     * @param string|null $mimeType
50     */
51    public function __construct($name, $contents, $mimeType = null)
52    {
53        $this->name = $name;
54        $this->contents = $contents;
55        $this->mimeType = $mimeType;
56    }
57
58    /**
59     * Returns the node name for this file.
60     *
61     * This name is used to construct the url.
62     *
63     * @return string
64     */
65    public function getName()
66    {
67        return $this->name;
68    }
69
70    /**
71     * Returns the data.
72     *
73     * This method may either return a string or a readable stream resource
74     *
75     * @return mixed
76     */
77    public function get()
78    {
79        return $this->contents;
80    }
81
82    /**
83     * Returns the size of the file, in bytes.
84     *
85     * @return int
86     */
87    public function getSize()
88    {
89        return strlen($this->contents);
90    }
91
92    /**
93     * Returns the ETag for a file.
94     *
95     * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
96     * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
97     *
98     * Return null if the ETag can not effectively be determined
99     *
100     * @return string
101     */
102    public function getETag()
103    {
104        return '"'.sha1($this->contents).'"';
105    }
106
107    /**
108     * Returns the mime-type for a file.
109     *
110     * If null is returned, we'll assume application/octet-stream
111     *
112     * @return string
113     */
114    public function getContentType()
115    {
116        return $this->mimeType;
117    }
118}
119