Skip to content

Instantly share code, notes, and snippets.

@sonOfRa
Created October 9, 2017 14:44
Show Gist options
  • Select an option

  • Save sonOfRa/7aa978d94e273190920d9ea6d248ae52 to your computer and use it in GitHub Desktop.

Select an option

Save sonOfRa/7aa978d94e273190920d9ea6d248ae52 to your computer and use it in GitHub Desktop.
<?php
namespace App\Rules;
use App\EntryType;
use Illuminate\Contracts\Validation\Rule;
/**
* Class BibTeXKey
* @package App\Rules
*
* Rule to verify whether a given BibTeX citation key is valid.
* Currently a key is only invalid if:
* - The given entry type has no bibtex key and the citationkey is non-empty
* - The given entry type has a bibtex key, and the citationkey is empty, and neither author or editor are supplied to
* automatically generate a key
*/
class BibTeXKey implements Rule
{
private $authors;
private $editors;
private $type;
private $message = "";
/**
* Create a new rule instance.
* @param int $type
* @param array|null $authors
* @param array|null $editors
*/
public function __construct(int $type, ?array $authors, ?array $editors)
{
$this->type = EntryType::find($type);
$this->authors = $authors;
$this->editors = $editors;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return false;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->message;
}
}
$this->validate($request, [
'title' => 'required|string|max:191',
'subtitle' => 'nullable|string|max:191',
'type' => 'required|integer|exists:entry_types,id',
'category' => 'required|integer|exists:categories,id',
'categorysubid' => 'nullable|string',
'categoryid' => ['required', 'integer',
new UniqueCategoryId($request->get('category'), $request->get('categorysubid'), $entry->id)],
'pubyear' => 'required_with:pubmonth|nullable|integer',
'pubmonth' => 'required_with:pubday|nullable|integer|between:1,12',
'pubday' => ['nullable', 'integer', 'between:1,31',
new ValidDay($request->get('pubyear'), $request->get('pubmonth'))],
'isbn' => 'nullable|string',
'citationkey' => ['nullable', new BibTeXKey($request->get('type'), $request->get('authors'), $request->get('editors')),
'string', 'unique:entries,citation_key,' . $entry->id],
'abstract' => 'nullable|string',
'publisher' => 'nullable|integer|exists:publishers,id',
'creators' => 'nullable|array',
'creators.*' => 'integer|exists:creators,id',
'editors' => 'nullable|array',
'editors.*' => 'integer|exists:creators,id',
'collections' => 'nullable|array',
'collections.*' => 'integer|exists:collections,id',
'keywords' => 'nullable|array',
'keywords.*' => 'integer|exists:keywords,id',
'edition' => 'nullable|string',
'volume' => 'nullable|string',
'page_start' => 'nullable|integer',
'page_end' => 'nullable|integer',
'issue' => 'nullable|integer'
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment