--- /dev/null
+<?php
+namespace wcf\system\upload;
+
+/**
+ * Provides a default implementation for validation strategies.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2012 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package com.woltlab.wcf
+ * @subpackage system.upload
+ * @category Community Framework
+ */
+class DefaultUploadFileValidationStrategy implements IUploadFileValidationStrategy {
+ /**
+ * allowed max size
+ * @var integer
+ */
+ protected $maxFilesize = 0;
+
+ /**
+ * allowed file extensions
+ * @var array<string>
+ */
+ protected $fileExtensions = array();
+
+ /**
+ * Creates a new DefaultUploadFileValidationStrategy object.
+ *
+ * @param integer $maxFilesize
+ * @param array<string> $fileExtensions
+ */
+ public function __construct($maxFilesize, array $fileExtensions) {
+ $this->maxFilesize = $maxFilesize;
+ $this->fileExtensions = $fileExtensions;
+ }
+
+ /**
+ * @see wcf\system\upload\IUploadFileValidationStrategy::validate()
+ */
+ public function validate(UploadFile $uploadFile) {
+ if ($uploadFile->getErrorCode() != 0) {
+ $uploadFile->setValidationErrorType('uploadFailed');
+ return false;
+ }
+
+ if ($uploadFile->getFilesize() > $this->maxFilesize) {
+ $uploadFile->setValidationErrorType('tooLarge');
+ return false;
+ }
+
+ if (!in_array($uploadFile->getFileExtension(), $this->fileExtensions)) {
+ $uploadFile->setValidationErrorType('invalidExtension');
+ return false;
+ }
+
+ return true;
+ }
+}
--- /dev/null
+<?php
+namespace wcf\system\upload;
+
+/**
+ * Interface for file upload validation strategies.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2012 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package com.woltlab.wcf
+ * @subpackage system.upload
+ * @category Community Framework
+ */
+interface IUploadFileValidationStrategy {
+ /**
+ * Validates the given file. Returns true on success, otherwise false.
+ *
+ * @param wcf\system\upload\UploadFile $uploadFile
+ * @return boolean
+ */
+ public function validate(UploadFile $uploadFile);
+}
}
/**
- * Validates the uploaded file. Returns true on success, otherwise false.
+ * Sets the validation error type.
*
- * @param integer $maxFilesize
- * @param array<string> $fileExtensions
- * @return boolean
- */
- public function validateFile($maxFilesize, array $fileExtensions) {
- if ($this->errorCode != 0) {
- $this->validationErrorType = 'uploadFailed';
- return false;
- }
-
- if ($this->getFilesize() > $maxFilesize) {
- $this->validationErrorType = 'tooLarge';
- return false;
- }
-
- if (!in_array($this->getFileExtension(), $fileExtensions)) {
- $this->validationErrorType = 'invalidExtension';
- return false;
- }
-
- return true;
+ * @param string $validationErrorType
+ */
+ public function setValidationErrorType($validationErrorType) {
+ $this->validationErrorType = $validationErrorType;
}
/**
* @param array<string> $fileExtensions
* @return boolean
*/
- public function validateFiles($maxFilesize, array $fileExtensions) {
+ public function validateFiles(IUploadFileValidationStrategy $validationStrategy) {
$result = true;
foreach ($this->files as $file) {
- if (!$file->validateFile($maxFilesize, $fileExtensions)) {
+ if (!$validationStrategy->validate($file)) {
$this->erroneousFiles[] = $file;
$result = false;
}