Separated laravel app to its own folder (#540)

This commit is contained in:
Julien Nahum
2024-08-26 18:24:56 +02:00
committed by GitHub
parent 39b8df5eed
commit 5bd1dda504
546 changed files with 124 additions and 143 deletions

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Rules;
use App\Service\Forms\FormLogicConditionChecker;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class CustomFieldValidationRule implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*
* @var bool
*/
public $implicit = true;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct(public array $validation, public array $formData)
{
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$logicConditions = $this->validation['error_conditions']['conditions'] ?? null;
if (empty($logicConditions) || empty($logicConditions['children'] ?? [])) {
return true;
}
return FormLogicConditionChecker::conditionsMet(
$logicConditions,
$this->formData
);
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return isset($this->validation['error_message']) ? $this->validation['error_message'] : 'Invalid input';
}
}

View File

@@ -0,0 +1,881 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;
class FormPropertyLogicRule implements DataAwareRule, ValidationRule
{
public const ACTIONS_VALUES = [
'show-block',
'hide-block',
'make-it-optional',
'require-answer',
'enable-block',
'disable-block',
];
public const CONDITION_MAPPING = [
'text' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'matrix' => [
'comparators' => [
'equals' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'does_not_equal' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'contains' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
'does_not_contain' => [
'expected_type' => 'object',
'format' => [
'type' => 'object',
],
],
],
],
'url' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'email' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'phone_number' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'contains' => [
'expected_type' => 'string',
],
'does_not_contain' => [
'expected_type' => 'string',
],
'starts_with' => [
'expected_type' => 'string',
],
'ends_with' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'number' => [
'comparators' => [
'equals' => [
'expected_type' => 'number',
],
'does_not_equal' => [
'expected_type' => 'number',
],
'greater_than' => [
'expected_type' => 'number',
],
'less_than' => [
'expected_type' => 'number',
],
'greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'less_than_or_equal_to' => [
'expected_type' => 'number',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'rating' => [
'comparators' => [
'equals' => [
'expected_type' => 'number',
],
'does_not_equal' => [
'expected_type' => 'number',
],
'greater_than' => [
'expected_type' => 'number',
],
'less_than' => [
'expected_type' => 'number',
],
'greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'less_than_or_equal_to' => [
'expected_type' => 'number',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'scale' => [
'comparators' => [
'equals' => [
'expected_type' => 'number',
],
'does_not_equal' => [
'expected_type' => 'number',
],
'greater_than' => [
'expected_type' => 'number',
],
'less_than' => [
'expected_type' => 'number',
],
'greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'less_than_or_equal_to' => [
'expected_type' => 'number',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'slider' => [
'comparators' => [
'equals' => [
'expected_type' => 'number',
],
'does_not_equal' => [
'expected_type' => 'number',
],
'greater_than' => [
'expected_type' => 'number',
],
'less_than' => [
'expected_type' => 'number',
],
'greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'less_than_or_equal_to' => [
'expected_type' => 'number',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'content_length_equals' => [
'expected_type' => 'number',
],
'content_length_does_not_equal' => [
'expected_type' => 'number',
],
'content_length_greater_than' => [
'expected_type' => 'number',
],
'content_length_greater_than_or_equal_to' => [
'expected_type' => 'number',
],
'content_length_less_than' => [
'expected_type' => 'number',
],
'content_length_less_than_or_equal_to' => [
'expected_type' => 'number',
],
],
],
'checkbox' => [
'comparators' => [
'equals' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'does_not_equal' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
],
],
'select' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
],
'does_not_equal' => [
'expected_type' => 'string',
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
],
],
'multi_select' => [
'comparators' => [
'contains' => [
'expected_type' => ['object', 'string'],
'format' => [
'type' => 'uuid',
],
],
'does_not_contain' => [
'expected_type' => ['object', 'string'],
'format' => [
'type' => 'uuid',
],
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
],
],
'date' => [
'comparators' => [
'equals' => [
'expected_type' => 'string',
'format' => [
'type' => 'date',
],
],
'before' => [
'expected_type' => 'string',
'format' => [
'type' => 'date',
],
],
'after' => [
'expected_type' => 'string',
'format' => [
'type' => 'date',
],
],
'on_or_before' => [
'expected_type' => 'string',
'format' => [
'type' => 'date',
],
],
'on_or_after' => [
'expected_type' => 'string',
'format' => [
'type' => 'date',
],
],
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'past_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
'past_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
'past_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
'next_week' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
'next_month' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
'next_year' => [
'expected_type' => 'object',
'format' => [
'type' => 'empty',
'values' => '{}',
],
],
],
],
'files' => [
'comparators' => [
'is_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
'is_not_empty' => [
'expected_type' => 'boolean',
'format' => [
'type' => 'enum',
'values' => [true],
],
],
],
],
];
private $isConditionCorrect = true;
private $isActionCorrect = true;
private $conditionErrors = [];
private $field = [];
private $data = [];
private function checkBaseCondition($condition)
{
if (!isset($condition['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
return;
}
if (!isset($condition['value']['property_meta'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
return;
}
if (!isset($condition['value']['property_meta']['type'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
return;
}
if (!isset($condition['value']['operator'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
return;
}
if (!isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
$typeField = $condition['value']['property_meta']['type'];
$operator = $condition['value']['operator'];
$value = $condition['value']['value'];
if (!isset(self::CONDITION_MAPPING[$typeField])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
return;
}
if (!isset(self::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
return;
}
$type = self::CONDITION_MAPPING[$typeField]['comparators'][$operator]['expected_type'];
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
}
}
if (!$foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
}
private function valueHasCorrectType($type, $value)
{
if (
($type === 'string' && gettype($value) !== 'string') ||
($type === 'boolean' && !is_bool($value)) ||
($type === 'number' && !is_numeric($value)) ||
($type === 'object' && !is_array($value))
) {
return false;
}
return true;
}
private function checkConditions($conditions)
{
if (array_key_exists('operatorIdentifier', $conditions)) {
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
$this->conditionErrors[] = 'missing operator';
$this->isConditionCorrect = false;
return;
}
if (isset($conditions['operatorIdentifier']['children'])) {
$this->conditionErrors[] = 'extra condition';
$this->isConditionCorrect = false;
return;
}
if (!is_array($conditions['children'])) {
$this->conditionErrors[] = 'wrong sub-condition type';
$this->isConditionCorrect = false;
return;
}
foreach ($conditions['children'] as &$child) {
$this->checkConditions($child);
}
} elseif (isset($conditions['identifier'])) {
$this->checkBaseCondition($conditions);
}
}
private function checkActions($actions)
{
if (is_array($actions) && count($actions) > 0) {
foreach ($actions as $val) {
if (
!in_array($val, static::ACTIONS_VALUES) ||
(in_array($this->field['type'], ['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image']) && !in_array($val, ['hide-block', 'show-block'])) ||
(isset($this->field['hidden']) && $this->field['hidden'] && !in_array($val, ['show-block', 'require-answer'])) ||
(isset($this->field['required']) && $this->field['required'] && !in_array($val, ['make-it-optional', 'hide-block', 'disable-block'])) ||
(isset($this->field['disabled']) && $this->field['disabled'] && !in_array($val, ['enable-block', 'require-answer', 'make-it-optional']))
) {
$this->isActionCorrect = false;
break;
}
}
} else {
$this->isActionCorrect = false;
}
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->setProperty($attribute);
if (isset($value['conditions'])) {
$this->checkConditions($value['conditions']);
$this->checkActions($value['actions'] ?? null);
}
return $this->isConditionCorrect && $this->isActionCorrect;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*/
public function message()
{
$message = null;
if (!$this->isConditionCorrect) {
$message = 'The logic conditions for ' . $this->field['name'] . ' are not complete.';
} elseif (!$this->isActionCorrect) {
$message = 'The logic actions for ' . $this->field['name'] . ' are not valid.';
}
if (count($this->conditionErrors) > 0) {
return $message . ' Error detail(s): ' . implode(', ', $this->conditionErrors);
}
return $message;
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
$this->isConditionCorrect = true;
$this->isActionCorrect = true;
$this->conditionErrors = [];
return $this;
}
private function setProperty(string $attributeKey)
{
$attributeKey = Str::of($attributeKey)->replace('.logic', '')->toString();
$this->field = \Arr::get($this->data, $attributeKey);
}
}

View File

@@ -0,0 +1,194 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
class IntegrationLogicRule implements DataAwareRule, ValidationRule
{
private $isConditionCorrect = true;
private $conditionErrors = [];
private $field = [];
private $data = [];
private function checkBaseCondition($condition)
{
if (!isset($condition['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition body';
return;
}
if (!isset($condition['value']['property_meta'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property';
return;
}
if (!isset($condition['value']['property_meta']['type'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition property type';
return;
}
if (!isset($condition['value']['operator'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition operator';
return;
}
if (!isset($condition['value']['value'])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'missing condition value';
return;
}
$typeField = $condition['value']['property_meta']['type'];
$operator = $condition['value']['operator'];
$value = $condition['value']['value'];
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition type';
return;
}
if (!isset(FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator])) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'configuration not found for condition operator';
return;
}
$type = FormPropertyLogicRule::CONDITION_MAPPING[$typeField]['comparators'][$operator]['expected_type'];
if (is_array($type)) {
$foundCorrectType = false;
foreach ($type as $subtype) {
if ($this->valueHasCorrectType($subtype, $value)) {
$foundCorrectType = true;
}
}
if (!$foundCorrectType) {
$this->isConditionCorrect = false;
}
} else {
if (!$this->valueHasCorrectType($type, $value)) {
$this->isConditionCorrect = false;
$this->conditionErrors[] = 'wrong type of condition value';
}
}
}
private function valueHasCorrectType($type, $value)
{
if (
($type === 'string' && gettype($value) !== 'string') ||
($type === 'boolean' && !is_bool($value)) ||
($type === 'number' && !is_numeric($value)) ||
($type === 'object' && !is_array($value))
) {
return false;
}
return true;
}
private function checkConditions($conditions)
{
if (array_key_exists('operatorIdentifier', $conditions)) {
if (($conditions['operatorIdentifier'] !== 'and') && ($conditions['operatorIdentifier'] !== 'or')) {
$this->conditionErrors[] = 'missing operator';
$this->isConditionCorrect = false;
return;
}
if (isset($conditions['operatorIdentifier']['children'])) {
$this->conditionErrors[] = 'extra condition';
$this->isConditionCorrect = false;
return;
}
if (!is_array($conditions['children'])) {
$this->conditionErrors[] = 'wrong sub-condition type';
$this->isConditionCorrect = false;
return;
}
foreach ($conditions['children'] as &$child) {
$this->checkConditions($child);
}
} elseif (isset($conditions['identifier'])) {
$this->checkBaseCondition($conditions);
}
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (isset($value)) {
$this->checkConditions($value);
}
return $this->isConditionCorrect;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*/
public function message()
{
$message = null;
if (!$this->isConditionCorrect) {
$message = 'The logic conditions are not complete.';
}
if (count($this->conditionErrors) > 0) {
return $message . ' Error detail(s): ' . implode(', ', $this->conditionErrors);
}
return $message;
}
/**
* Set the data under validation.
*
* @param array $data
* @return $this
*/
public function setData($data)
{
$this->data = $data;
$this->isConditionCorrect = true;
$this->conditionErrors = [];
return $this;
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class MatrixValidationRule implements ValidationRule
{
protected $field;
protected $isRequired;
public function __construct(array $field, bool $isRequired)
{
$this->field = $field;
$this->isRequired = $isRequired;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->isRequired && empty($value)) {
return; // If not required and empty, validation passes
}
if (!is_array($value)) {
$fail('The Matrix field must be an array.');
return;
}
$rows = $this->field['rows'];
$columns = $this->field['columns'];
foreach ($rows as $row) {
if (!array_key_exists($row, $value)) {
if ($this->isRequired) {
$fail("Missing value for row '{$row}'.");
}
continue;
}
$cellValue = $value[$row];
if ($cellValue === null) {
if ($this->isRequired) {
$fail("Value for row '{$row}' is required.");
}
continue;
}
if (!in_array($cellValue, $columns)) {
$fail("Invalid value '{$cellValue}' for row '{$row}'.");
}
}
// Check for extra rows that shouldn't be there
$extraRows = array_diff(array_keys($value), $rows);
foreach ($extraRows as $extraRow) {
$fail("Unexpected row '{$extraRow}' in the matrix.");
}
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class OneEmailPerLine implements ValidationRule
{
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if ($value === null || empty(trim($value))) {
return true;
}
foreach (preg_split("/\r\n|\n|\r/", $value) as $email) {
$email = trim($email);
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
return false;
}
}
return true;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'You need one valid email per line.';
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Rules;
use App\Http\Controllers\Forms\PublicFormController;
use App\Models\Forms\Form;
use App\Service\Storage\StorageFileNameParser;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class StorageFile implements ValidationRule
{
public string $error = 'Invalid file.';
public function __construct(public int $maxSize, public array $fileTypes = [], public ?Form $form = null)
{
}
/**
* File can have 2 formats:
* - file-name_{uuid}.{ext}
* - {uuid}
*
* @param string $attribute
* @param mixed $value
*/
public function passes($attribute, $value): bool
{
// If full path then no need to validate
if (filter_var($value, FILTER_VALIDATE_URL) !== false) {
return true;
}
// This is use when updating a record, and file uploads aren't changed.
if ($this->form) {
$newPath = Str::of(PublicFormController::FILE_UPLOAD_PATH)->replace('?', $this->form->id);
if (Storage::exists($newPath.'/'.$value)) {
return true;
}
}
$fileNameParser = StorageFileNameParser::parse($value);
if (! $uuid = $fileNameParser->uuid) {
return false;
}
$filePath = PublicFormController::TMP_FILE_UPLOAD_PATH.$uuid;
if (! Storage::exists($filePath)) {
return false;
}
if (Storage::size($filePath) > $this->maxSize) {
$this->error = 'File is too large.';
return false;
}
if (count($this->fileTypes) > 0) {
$this->error = 'Incorrect file type. Allowed only: '.implode(',', $this->fileTypes);
return collect($this->fileTypes)->map(function ($type) {
return strtolower($type);
})->contains(strtolower($fileNameParser->extension));
}
return true;
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
public function message(): string
{
return $this->error;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Support\Facades\Http;
class ValidHCaptcha implements ImplicitRule
{
public const H_CAPTCHA_VERIFY_URL = 'https://hcaptcha.com/siteverify';
private $error = 'Invalid CAPTCHA. Please prove you\'re not a bot.';
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if (empty($value)) {
$this->error = 'Please complete the captcha.';
return false;
}
return Http::asForm()->post(self::H_CAPTCHA_VERIFY_URL, [
'secret' => config('services.h_captcha.secret_key'),
'response' => $value,
])->json('success');
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if(!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return $this->error;
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class ValidPhoneInputRule implements ValidationRule
{
public ?int $reason = 0;
public function passes($attribute, $value)
{
if (! is_string($value) || ! $value) {
return false;
}
try {
if (ctype_alpha(substr($value, 0, 2))) { // First 2 will be country code
$value = substr($value, 2);
}
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$phone = $phoneUtil->parse($value);
$this->reason = $phoneUtil->isPossibleNumberWithReason($phone);
return $this->reason === \libphonenumber\ValidationResult::IS_POSSIBLE;
} catch (\Exception $e) {
return false;
}
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
public function message()
{
return match ($this->reason) {
\libphonenumber\ValidationResult::IS_POSSIBLE => 'The :attribute is not valid for an unknown reason.',
\libphonenumber\ValidationResult::INVALID_COUNTRY_CODE => 'The :attribute does not have a valid country code.',
\libphonenumber\ValidationResult::TOO_SHORT => 'The :attribute is too short.',
\libphonenumber\ValidationResult::TOO_LONG => 'The :attribute is too long.',
\libphonenumber\ValidationResult::IS_POSSIBLE_LOCAL_ONLY => 'The :attribute is not a valid phone number (local number).',
\libphonenumber\ValidationResult::INVALID_LENGTH => 'The :attribute does not have a valid length.',
default => 'The :attribute is not a valid phone number.',
};
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class ValidUrl implements ValidationRule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
// Define the regular expression to match the desired URL patterns
$pattern = '/^(https?:\/\/)?(www\.)?[\w.-]+\.\w+(:\d+)?(\/[^\s]*)?$/';
return preg_match($pattern, $value);
}
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$this->passes($attribute, $value)) {
$fail($this->message());
}
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute format is invalid.';
}
}