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 authored and GuySartorelli committed Dec 21, 2023
1 parent 6c7a971 commit 595c5a5
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 @@ -75,19 +75,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 @@ -154,6 +163,8 @@ public function getAllowedMaxFileSize($ext = null)
* </code>
*
* @param array|int|string $rules
*
* @return $this
*/
public function setAllowedMaxFileSize($rules)
{
Expand All @@ -178,6 +189,8 @@ public function setAllowedMaxFileSize($rules)
} elseif ((int)$rules > 0) {
$this->allowedMaxFileSize['*'] = (int)$rules;
}

return $this;
}

/**
Expand All @@ -196,6 +209,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 @@ -209,6 +224,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 595c5a5

Please sign in to comment.