Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
class BooleanRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' must be a boolean value.";
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
if ($value === true || $value === 1 || $value === '1') {
|
||||
return true; // true = Ist Boolean
|
||||
}
|
||||
if ($value === false || $value === 0 || $value === '0') {
|
||||
return true; // true = Ist Boolean
|
||||
}
|
||||
|
||||
// 'true' und 'false' als String ist nicht zulässig, da die Datenbank diese Werte nicht casten kann.
|
||||
|
||||
return false; // Kein Boolean
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\MissingRequiredParameterException;
|
||||
use Rakit\Validation\Rule;
|
||||
use Xentral\Components\Database\Database;
|
||||
|
||||
/**
|
||||
* Validierungsregel um zu prüfen ob ein Wert überhaupt in der Datenbank existiert.
|
||||
*
|
||||
* @example 'required|numeric|db_value:adresse,id' Bedeutet dass der zu validierende Wert
|
||||
* in der Tabelle 'adresse' in der Spalte 'id' existieren muss.
|
||||
*/
|
||||
class DbValueRule extends Rule
|
||||
{
|
||||
/** @var Database $db */
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
$this->fillable_params = ['table', 'column', 'except'];
|
||||
$this->message =
|
||||
"The attribute ':attribute' has to be present in database. " .
|
||||
"The value ':value' is not present in table ':table'.";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws MissingRequiredParameterException
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
// make sure required parameters exists
|
||||
$this->requireParameters(['table', 'column']);
|
||||
|
||||
// getting parameters
|
||||
$column = $this->parameter('column');
|
||||
$table = $this->parameter('table');
|
||||
$except = (int)$this->parameter('except'); // ID des Datensatzes der von der Prüfung ausgenommen werden soll
|
||||
|
||||
$select = $this->db->select()
|
||||
->cols(['COUNT(*) AS num'])
|
||||
->from($table)
|
||||
->where($this->db->escapeIdentifier($column) . ' = :value');
|
||||
$bindValues = ['value' => $value];
|
||||
|
||||
if ($except) {
|
||||
$select->where('id != :id');
|
||||
$bindValues['id'] = $except;
|
||||
}
|
||||
|
||||
$count = $this->db->fetchValue($select->getStatement(), $bindValues);
|
||||
|
||||
return (int)$count > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
class DecimalRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' must be a decimal or integer value.";
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
return is_numeric((string)$value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
class LengthRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' must have the length :length.";
|
||||
|
||||
/** @var array $fillable_params */
|
||||
protected $fillable_params = ['length'];
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Rakit\Validation\MissingRequiredParameterException
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
$this->requireParameters($this->fillable_params);
|
||||
|
||||
$length = (int)$this->parameter('length');
|
||||
|
||||
return mb_strlen((string)$value, 'UTF-8') === $length;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Regel prüft ob alle Zeichen Kleinbuchstaben sind.
|
||||
*
|
||||
* - Deutsche Umlaute sind nicht zulässig.
|
||||
* - Regel wurde für Fälle wie Sprach ISO-Code erstellt.
|
||||
*/
|
||||
class LowerRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' must be in lowercase letter.";
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
return ctype_lower($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
class NotPresentRule extends Rule
|
||||
{
|
||||
/** @var bool $implicit */
|
||||
protected $implicit = true;
|
||||
|
||||
/** @var string $message */
|
||||
protected $message = 'The attribute \':attribute\' is not allowed.';
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
return !$this->validation->hasValue($this->attribute->getKey());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\MissingRequiredParameterException;
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Regel prüft ob ein valides Zeitformat vorliegt.
|
||||
*/
|
||||
class TimeRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' is not valid time format. Format ':format' is required.";
|
||||
|
||||
/** @var array $fillable_params */
|
||||
protected $fillable_params = ['format'];
|
||||
|
||||
/** @var array $params */
|
||||
protected $params = [
|
||||
'format' => 'H:i:s',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @throws MissingRequiredParameterException
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
$this->requireParameters($this->fillable_params);
|
||||
|
||||
$format = $this->parameter('format');
|
||||
|
||||
return date_create_from_format('Y-m-d ' . $format, '2019-01-01 ' . $value) !== false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
use Xentral\Components\Database\Database;
|
||||
|
||||
class UniqueRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' has to be unique. The value ':value' is already in use.";
|
||||
|
||||
/** @var array $fillable_params */
|
||||
protected $fillable_params = ['table', 'column', 'except'];
|
||||
|
||||
/** @var Database $db */
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Rakit\Validation\MissingRequiredParameterException
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
// make sure required parameters exists
|
||||
$this->requireParameters(['table', 'column']);
|
||||
|
||||
// getting parameters
|
||||
$column = $this->parameter('column');
|
||||
$table = $this->parameter('table');
|
||||
$except = (int)$this->parameter('except'); // ID des Datensatzes der von der Prüfung ausgenommen werden soll
|
||||
|
||||
$select = $this->db->select()
|
||||
->cols(['COUNT(*) AS num'])
|
||||
->from($table)
|
||||
->where($this->db->escapeIdentifier($column) . ' = :value');
|
||||
$bindValues = ['value' => $value];
|
||||
|
||||
if ($except) {
|
||||
$select->where('id != :id');
|
||||
$bindValues['id'] = $except;
|
||||
}
|
||||
|
||||
$count = $this->db->fetchValue($select->getStatement(), $bindValues);
|
||||
|
||||
return intval($count) === 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator\Rule;
|
||||
|
||||
use Rakit\Validation\Rule;
|
||||
|
||||
/**
|
||||
* Regel prüft ob alle Zeichen Grossbuchstaben sind.
|
||||
*
|
||||
* - Deutsche Umlaute sind nicht zulässig.
|
||||
* - Regel wurde für Fälle wie Länder ISO-Code erstellt.
|
||||
*/
|
||||
class UpperRule extends Rule
|
||||
{
|
||||
/** @var string $message */
|
||||
protected $message = "The attribute ':attribute' must be in uppercase letter.";
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function check($value)
|
||||
{
|
||||
return ctype_upper($value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Api\Validator;
|
||||
|
||||
use Rakit\Validation\Validator as RakitValidator;
|
||||
|
||||
class Validator extends RakitValidator
|
||||
{
|
||||
/**
|
||||
* @see ApiContainer::createValidatorService
|
||||
*
|
||||
* @param array $messages
|
||||
*/
|
||||
public function __construct(array $messages = [])
|
||||
{
|
||||
$messages = [
|
||||
'db_value' => "The attribute ':attribute' has to be present in database. " .
|
||||
"The value ':value' is not present in table ':table'.",
|
||||
'between' => "The attribute :attribute must be between :min and :max.",
|
||||
'boolean' => "The attribute ':attribute' must be a boolean value.",
|
||||
'date' => "The attribute ':attribute' is not valid date format. Format ':format' is required.",
|
||||
'time' => "The attribute ':attribute' is not valid time format. Format ':format' is required.",
|
||||
'decimal' => "The attribute ':attribute' must be a decimal or integer value.",
|
||||
'integer' => "The attribute ':attribute' must be an integer value.",
|
||||
'length' => "The attribute ':attribute' must have the length :length.",
|
||||
'lower' => "The attribute ':attribute' must be in lowercase letter.",
|
||||
'in' => "The attribute ':attribute' is not allowing the value ':value'.",
|
||||
'max' => "The attribute ':attribute' has a maximum value ':max'.",
|
||||
'min' => "The attribute ':attribute' has a minimum value ':min'.",
|
||||
'numeric' => "The attribute ':attribute' must be numeric.",
|
||||
'not_present' => "The attribute ':attribute' is not allowed.",
|
||||
'present' => "The attribute ':attribute' must be present",
|
||||
'required' => "The attribute ':attribute' is required.",
|
||||
'unique' => "The attribute ':attribute' has to be unique. The value ':value' is already in use.",
|
||||
'upper' => "The attribute ':attribute' must be in uppercase letter.",
|
||||
];
|
||||
|
||||
parent::__construct($messages);
|
||||
|
||||
// Attribute nicht ucfirst-en
|
||||
$this->useHumanizedKeys = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user