Skip to content

Commit

Permalink
API allow chaining for Upload_Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Aug 5, 2021
1 parent 42ee290 commit 18cdc29
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Upload_Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,28 @@ public function getErrors()

/**
* Clear out all errors
*
* @return $this
*/
public function clearErrors()
{
$this->errors = [];

return $this;
}

/**
* Set information about temporary file produced by PHP.
*
* @param array $tmpFile
*
* @return $this
*/
public function setTmpFile($tmpFile)
{
$this->tmpFile = $tmpFile;

return $this;
}


Expand Down Expand Up @@ -153,6 +162,8 @@ public function getAllowedMaxFileSize($ext = null)
* </code>
*
* @param array|int|string $rules
*
* @return $this
*/
public function setAllowedMaxFileSize($rules)
{
Expand All @@ -177,6 +188,8 @@ public function setAllowedMaxFileSize($rules)
} elseif ((int)$rules > 0) {
$this->allowedMaxFileSize['*'] = (int)$rules;
}

return $this;
}

/**
Expand All @@ -195,6 +208,8 @@ public function getAllowedExtensions()
* See {@link setAllowedMaxFileSize()} to limit file size by extension.
*
* @param array $rules List of extensions
*
* @return $this
*/
public function setAllowedExtensions($rules)
{
Expand All @@ -208,6 +223,8 @@ public function setAllowedExtensions($rules)
}

$this->allowedExtensions = $rules;

return $this;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions tests/php/UploadValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace SilverStripe\Assets\Tests;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Assets\Upload_Validator;

/**
* @skipUpgrade
*/
class UploadValidatorTest extends SapphireTest
{
/**
* {@inheritDoc}
* @var bool
*/
protected $usesDatabase = false;

public function testUploadValidatorChaining()
{
$v = new Upload_Validator();

$chain = $v->clearErrors()->setAllowedMaxFileSize(100)->setAllowedExtensions([
'jpg'
]);

$this->assertInstanceOf(Upload_Validator::class, $chain);
}
}

0 comments on commit 18cdc29

Please sign in to comment.