1<?php 2 3namespace Sabre\DAV\PartialUpdate; 4use Sabre\DAV; 5 6class FileMock implements IPatchSupport { 7 8 protected $data = ''; 9 10 function put($str) { 11 12 if (is_resource($str)) { 13 $str = stream_get_contents($str); 14 } 15 $this->data = $str; 16 17 } 18 19 /** 20 * Updates the file based on a range specification. 21 * 22 * The first argument is the data, which is either a readable stream 23 * resource or a string. 24 * 25 * The second argument is the type of update we're doing. 26 * This is either: 27 * * 1. append 28 * * 2. update based on a start byte 29 * * 3. update based on an end byte 30 *; 31 * The third argument is the start or end byte. 32 * 33 * After a successful put operation, you may choose to return an ETag. The 34 * etag must always be surrounded by double-quotes. These quotes must 35 * appear in the actual string you're returning. 36 * 37 * Clients may use the ETag from a PUT request to later on make sure that 38 * when they update the file, the contents haven't changed in the mean 39 * time. 40 * 41 * @param resource|string $data 42 * @param int $rangeType 43 * @param int $offset 44 * @return string|null 45 */ 46 function patch($data, $rangeType, $offset = null) { 47 48 if (is_resource($data)) { 49 $data = stream_get_contents($data); 50 } 51 52 switch($rangeType) { 53 54 case 1 : 55 $this->data.=$data; 56 break; 57 case 3 : 58 // Turn the offset into an offset-offset. 59 $offset = strlen($this->data) - $offset; 60 // No break is intentional 61 case 2 : 62 $this->data = 63 substr($this->data, 0, $offset) . 64 $data . 65 substr($this->data, $offset + strlen($data)); 66 break; 67 68 } 69 70 } 71 72 function get() { 73 74 return $this->data; 75 76 } 77 78 function getContentType() { 79 80 return 'text/plain'; 81 82 } 83 84 function getSize() { 85 86 return strlen($this->data); 87 88 } 89 90 function getETag() { 91 92 return '"' . $this->data . '"'; 93 94 } 95 96 function delete() { 97 98 throw new DAV\Exception\MethodNotAllowed(); 99 100 } 101 102 function setName($name) { 103 104 throw new DAV\Exception\MethodNotAllowed(); 105 106 } 107 108 function getName() { 109 110 return 'partial'; 111 112 } 113 114 function getLastModified() { 115 116 return null; 117 118 } 119 120 121} 122