Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API allow chaining for Upload_Validator #460

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}