Changeset View
Changeset View
Standalone View
Standalone View
src/app/Rules/FileName.php
<?php | <?php | ||||
namespace App\Rules; | namespace App\Rules; | ||||
use Illuminate\Contracts\Validation\Rule; | use Illuminate\Contracts\Validation\Rule; | ||||
use Illuminate\Support\Facades\Validator; | |||||
use Illuminate\Support\Str; | |||||
class FileName implements Rule | class FileName implements Rule | ||||
{ | { | ||||
private $message; | private $message; | ||||
private $owner; | |||||
/** | |||||
* Class constructor. | |||||
* | |||||
* @param \App\User $owner The file owner | |||||
*/ | |||||
public function __construct($owner) | |||||
{ | |||||
$this->owner = $owner; | |||||
} | |||||
/** | /** | ||||
* Determine if the validation rule passes. | * Determine if the validation rule passes. | ||||
* | * | ||||
* @param string $attribute Attribute name | * @param string $attribute Attribute name | ||||
* @param mixed $name The value to validate | * @param mixed $name The value to validate | ||||
* | * | ||||
* @return bool | * @return bool | ||||
Show All 20 Lines | public function passes($attribute, $name): bool | ||||
// Leading/trailing spaces, or all spaces | // Leading/trailing spaces, or all spaces | ||||
if (preg_match('|^\s+$|', $name) || preg_match('|^\s+|', $name) || preg_match('|\s+$|', $name)) { | if (preg_match('|^\s+$|', $name) || preg_match('|^\s+|', $name) || preg_match('|\s+$|', $name)) { | ||||
$this->message = \trans('validation.file-name-invalid'); | $this->message = \trans('validation.file-name-invalid'); | ||||
return false; | return false; | ||||
} | } | ||||
// FIXME: Should we require a dot? | // FIXME: Should we require a dot? | ||||
// Check if the name is unique | |||||
$exists = $this->owner->fsItems() | |||||
->join('fs_properties', 'fs_items.id', '=', 'fs_properties.item_id') | |||||
->where('key', 'name') | |||||
->where('value', $name) | |||||
->exists(); | |||||
if ($exists) { | |||||
$this->message = \trans('validation.file-name-exists'); | |||||
return false; | |||||
} | |||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Get the validation error message. | * Get the validation error message. | ||||
* | * | ||||
* @return string | * @return string | ||||
*/ | */ | ||||
public function message(): ?string | public function message(): ?string | ||||
{ | { | ||||
return $this->message; | return $this->message; | ||||
} | } | ||||
} | } |