GuzzleHttpPsr7UploadedFile

The W3 Total Cache GuzzleHttp Psr7 UploadedFile class.

Defined (1)

The class is defined in the following location(s).

/lib/Azure/GuzzleHttp/Psr7/UploadedFile.php  
  1. class UploadedFile implements UploadedFileInterface 
  2. /** 
  3. * @var int[] 
  4. */ 
  5. private static $errors = [ 
  6. UPLOAD_ERR_OK,  
  7. UPLOAD_ERR_INI_SIZE,  
  8. UPLOAD_ERR_FORM_SIZE,  
  9. UPLOAD_ERR_PARTIAL,  
  10. UPLOAD_ERR_NO_FILE,  
  11. UPLOAD_ERR_NO_TMP_DIR,  
  12. UPLOAD_ERR_CANT_WRITE,  
  13. UPLOAD_ERR_EXTENSION,  
  14. ]; 
  15.  
  16. /** 
  17. * @var string 
  18. */ 
  19. private $clientFilename; 
  20.  
  21. /** 
  22. * @var string 
  23. */ 
  24. private $clientMediaType; 
  25.  
  26. /** 
  27. * @var int 
  28. */ 
  29. private $error; 
  30.  
  31. /** 
  32. * @var null|string 
  33. */ 
  34. private $file; 
  35.  
  36. /** 
  37. * @var bool 
  38. */ 
  39. private $moved = false; 
  40.  
  41. /** 
  42. * @var int 
  43. */ 
  44. private $size; 
  45.  
  46. /** 
  47. * @var StreamInterface|null 
  48. */ 
  49. private $stream; 
  50.  
  51. /** 
  52. * @param StreamInterface|string|resource $streamOrFile 
  53. * @param int $size 
  54. * @param int $errorStatus 
  55. * @param string|null $clientFilename 
  56. * @param string|null $clientMediaType 
  57. */ 
  58. public function __construct( 
  59. $streamOrFile,  
  60. $size,  
  61. $errorStatus,  
  62. $clientFilename = null,  
  63. $clientMediaType = null 
  64. ) { 
  65. $this->setError($errorStatus); 
  66. $this->setSize($size); 
  67. $this->setClientFilename($clientFilename); 
  68. $this->setClientMediaType($clientMediaType); 
  69.  
  70. if ($this->isOk()) { 
  71. $this->setStreamOrFile($streamOrFile); 
  72.  
  73. /** 
  74. * Depending on the value set file or stream variable 
  75. * @param mixed $streamOrFile 
  76. * @throws InvalidArgumentException 
  77. */ 
  78. private function setStreamOrFile($streamOrFile) 
  79. if (is_string($streamOrFile)) { 
  80. $this->file = $streamOrFile; 
  81. } elseif (is_resource($streamOrFile)) { 
  82. $this->stream = new Stream($streamOrFile); 
  83. } elseif ($streamOrFile instanceof StreamInterface) { 
  84. $this->stream = $streamOrFile; 
  85. } else { 
  86. throw new InvalidArgumentException( 
  87. 'Invalid stream or file provided for UploadedFile' 
  88. ); 
  89.  
  90. /** 
  91. * @param int $error 
  92. * @throws InvalidArgumentException 
  93. */ 
  94. private function setError($error) 
  95. if (false === is_int($error)) { 
  96. throw new InvalidArgumentException( 
  97. 'Upload file error status must be an integer' 
  98. ); 
  99.  
  100. if (false === in_array($error, UploadedFile::$errors)) { 
  101. throw new InvalidArgumentException( 
  102. 'Invalid error status for UploadedFile' 
  103. ); 
  104.  
  105. $this->error = $error; 
  106.  
  107. /** 
  108. * @param int $size 
  109. * @throws InvalidArgumentException 
  110. */ 
  111. private function setSize($size) 
  112. if (false === is_int($size)) { 
  113. throw new InvalidArgumentException( 
  114. 'Upload file size must be an integer' 
  115. ); 
  116.  
  117. $this->size = $size; 
  118.  
  119. /** 
  120. * @param mixed $param 
  121. * @return boolean 
  122. */ 
  123. private function isStringOrNull($param) 
  124. return in_array(gettype($param), ['string', 'NULL']); 
  125.  
  126. /** 
  127. * @param mixed $param 
  128. * @return boolean 
  129. */ 
  130. private function isStringNotEmpty($param) 
  131. return is_string($param) && false === empty($param); 
  132.  
  133. /** 
  134. * @param string|null $clientFilename 
  135. * @throws InvalidArgumentException 
  136. */ 
  137. private function setClientFilename($clientFilename) 
  138. if (false === $this->isStringOrNull($clientFilename)) { 
  139. throw new InvalidArgumentException( 
  140. 'Upload file client filename must be a string or null' 
  141. ); 
  142.  
  143. $this->clientFilename = $clientFilename; 
  144.  
  145. /** 
  146. * @param string|null $clientMediaType 
  147. * @throws InvalidArgumentException 
  148. */ 
  149. private function setClientMediaType($clientMediaType) 
  150. if (false === $this->isStringOrNull($clientMediaType)) { 
  151. throw new InvalidArgumentException( 
  152. 'Upload file client media type must be a string or null' 
  153. ); 
  154.  
  155. $this->clientMediaType = $clientMediaType; 
  156.  
  157. /** 
  158. * Return true if there is no upload error 
  159. * @return boolean 
  160. */ 
  161. private function isOk() 
  162. return $this->error === UPLOAD_ERR_OK; 
  163.  
  164. /** 
  165. * @return boolean 
  166. */ 
  167. public function isMoved() 
  168. return $this->moved; 
  169.  
  170. /** 
  171. * @throws RuntimeException if is moved or not ok 
  172. */ 
  173. private function validateActive() 
  174. if (false === $this->isOk()) { 
  175. throw new RuntimeException('Cannot retrieve stream due to upload error'); 
  176.  
  177. if ($this->isMoved()) { 
  178. throw new RuntimeException('Cannot retrieve stream after it has already been moved'); 
  179.  
  180. /** 
  181. * {@inheritdoc} 
  182. * @throws RuntimeException if the upload was not successful. 
  183. */ 
  184. public function getStream() 
  185. $this->validateActive(); 
  186.  
  187. if ($this->stream instanceof StreamInterface) { 
  188. return $this->stream; 
  189.  
  190. return new LazyOpenStream($this->file, 'r+'); 
  191.  
  192. /** 
  193. * {@inheritdoc} 
  194. * @see http://php.net/is_uploaded_file 
  195. * @see http://php.net/move_uploaded_file 
  196. * @param string $targetPath Path to which to move the uploaded file. 
  197. * @throws RuntimeException if the upload was not successful. 
  198. * @throws InvalidArgumentException if the $path specified is invalid. 
  199. * @throws RuntimeException on any error during the move operation, or on 
  200. * the second or subsequent call to the method. 
  201. */ 
  202. public function moveTo($targetPath) 
  203. $this->validateActive(); 
  204.  
  205. if (false === $this->isStringNotEmpty($targetPath)) { 
  206. throw new InvalidArgumentException( 
  207. 'Invalid path provided for move operation; must be a non-empty string' 
  208. ); 
  209.  
  210. if ($this->file) { 
  211. $this->moved = php_sapi_name() == 'cli' 
  212. ? rename($this->file, $targetPath) 
  213. : move_uploaded_file($this->file, $targetPath); 
  214. } else { 
  215. copy_to_stream( 
  216. $this->getStream(),  
  217. new LazyOpenStream($targetPath, 'w') 
  218. ); 
  219.  
  220. $this->moved = true; 
  221.  
  222. if (false === $this->moved) { 
  223. throw new RuntimeException( 
  224. sprintf('Uploaded file could not be moved to %s', $targetPath) 
  225. ); 
  226.  
  227. /** 
  228. * {@inheritdoc} 
  229. * @return int|null The file size in bytes or null if unknown. 
  230. */ 
  231. public function getSize() 
  232. return $this->size; 
  233.  
  234. /** 
  235. * {@inheritdoc} 
  236. * @see http://php.net/manual/en/features.file-upload.errors.php 
  237. * @return int One of PHP's UPLOAD_ERR_XXX constants. 
  238. */ 
  239. public function getError() 
  240. return $this->error; 
  241.  
  242. /** 
  243. * {@inheritdoc} 
  244. * @return string|null The filename sent by the client or null if none 
  245. * was provided. 
  246. */ 
  247. public function getClientFilename() 
  248. return $this->clientFilename; 
  249.  
  250. /** 
  251. * {@inheritdoc} 
  252. */ 
  253. public function getClientMediaType() 
  254. return $this->clientMediaType;