1<?php
2
3namespace Sabre\DAV\Mock;
4
5use Sabre\DAV;
6
7/**
8 * Mock File
9 *
10 * See the Collection in this directory for more details.
11 *
12 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
13 * @author Evert Pot (http://evertpot.com/)
14 * @license http://sabre.io/license/ Modified BSD License
15 */
16class File extends DAV\File {
17
18    protected $name;
19    protected $contents;
20    protected $parent;
21
22    /**
23     * Creates the object
24     *
25     * @param string $name
26     * @param array $children
27     * @return void
28     */
29    function __construct($name, $contents, Collection $parent = null) {
30
31        $this->name = $name;
32        $this->put($contents);
33        $this->parent = $parent;
34
35    }
36
37    /**
38     * Returns the name of the node.
39     *
40     * This is used to generate the url.
41     *
42     * @return string
43     */
44    function getName() {
45
46        return $this->name;
47
48    }
49
50    /**
51     * Changes the name of the node.
52     *
53     * @return void
54     */
55    function setName($name) {
56
57        $this->name = $name;
58
59    }
60
61    /**
62     * Updates the data
63     *
64     * The data argument is a readable stream resource.
65     *
66     * After a succesful put operation, you may choose to return an ETag. The
67     * etag must always be surrounded by double-quotes. These quotes must
68     * appear in the actual string you're returning.
69     *
70     * Clients may use the ETag from a PUT request to later on make sure that
71     * when they update the file, the contents haven't changed in the mean
72     * time.
73     *
74     * If you don't plan to store the file byte-by-byte, and you return a
75     * different object on a subsequent GET you are strongly recommended to not
76     * return an ETag, and just return null.
77     *
78     * @param resource $data
79     * @return string|null
80     */
81    function put($data) {
82
83        if (is_resource($data)) {
84            $data = stream_get_contents($data);
85        }
86        $this->contents = $data;
87        return '"' . md5($data) . '"';
88
89    }
90
91    /**
92     * Returns the data
93     *
94     * This method may either return a string or a readable stream resource
95     *
96     * @return mixed
97     */
98    function get() {
99
100        return $this->contents;
101
102    }
103
104    /**
105     * Returns the ETag for a file
106     *
107     * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
108     *
109     * Return null if the ETag can not effectively be determined
110     *
111     * @return void
112     */
113    function getETag() {
114
115        return '"' . md5($this->contents) . '"';
116
117    }
118
119    /**
120     * Returns the size of the node, in bytes
121     *
122     * @return int
123     */
124    function getSize() {
125
126        return strlen($this->contents);
127
128    }
129
130    /**
131     * Delete the node
132     *
133     * @return void
134     */
135    function delete() {
136
137        $this->parent->deleteChild($this->name);
138
139    }
140
141}
142