1<?php
2
3namespace Sabre\DAV\Mock;
4
5/**
6 * Mock Streaming File File
7 *
8 * Works similar to the mock file, but this one works with streams and has no
9 * content-length or etags.
10 *
11 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
12 * @author Evert Pot (http://evertpot.com/)
13 * @license http://sabre.io/license/ Modified BSD License
14 */
15class StreamingFile extends File {
16
17    protected $size;
18
19    /**
20     * Updates the data
21     *
22     * The data argument is a readable stream resource.
23     *
24     * After a successful put operation, you may choose to return an ETag. The
25     * etag must always be surrounded by double-quotes. These quotes must
26     * appear in the actual string you're returning.
27     *
28     * Clients may use the ETag from a PUT request to later on make sure that
29     * when they update the file, the contents haven't changed in the mean
30     * time.
31     *
32     * If you don't plan to store the file byte-by-byte, and you return a
33     * different object on a subsequent GET you are strongly recommended to not
34     * return an ETag, and just return null.
35     *
36     * @param resource $data
37     * @return string|null
38     */
39    function put($data) {
40
41        if (is_string($data)) {
42            $stream = fopen('php://memory', 'r+');
43            fwrite($stream, $data);
44            rewind($stream);
45            $data = $stream;
46        }
47        $this->contents = $data;
48
49    }
50
51    /**
52     * Returns the data
53     *
54     * This method may either return a string or a readable stream resource
55     *
56     * @return mixed
57     */
58    function get() {
59
60        return $this->contents;
61
62    }
63
64    /**
65     * Returns the ETag for a file
66     *
67     * An ETag is a unique identifier representing the current version of the file. If the file changes, the ETag MUST change.
68     *
69     * Return null if the ETag can not effectively be determined
70     *
71     * @return void
72     */
73    function getETag() {
74
75        return null;
76
77    }
78
79    /**
80     * Returns the size of the node, in bytes
81     *
82     * @return int
83     */
84    function getSize() {
85
86        return $this->size;
87
88    }
89
90    /**
91     * Allows testing scripts to set the resource's file size.
92     *
93     * @param int $size
94     * @return void
95     */
96    function setSize($size) {
97
98        $this->size = $size;
99
100    }
101
102}
103