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