mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-20 03:16:12 +00:00
mvc: Phalcon framework dependency (https://github.com/opnsense/core/issues/6389)
Add simple Message class and remove "Messages" dependancy in Validation.php, which should be backwards compatible with all existing validations. By moving \Phalcon\Filter\Validation() into validate() we're making the validation paths more explicit, if an objects implements ValidatorInterface, it uses phalcon, otherwise it's a simple BaseValidator passing messages back to $this->appendMessage(). The original phalcon Message class has additional fields we don't use, we only use fieldname for tracking purposes and the message itself.
This commit is contained in:
parent
203a034e93
commit
c2ea9aa303
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2024 Deciso B.V.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
||||
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OPNsense\Base\Messages;
|
||||
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $message;
|
||||
|
||||
/**
|
||||
* OPNsense\Base\Messages\Message constructor
|
||||
*/
|
||||
public function __construct($message, $field = "")
|
||||
{
|
||||
$this->message = $message;
|
||||
$this->field = $field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic __toString method returns verbose message
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getField()
|
||||
{
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets field name related to message
|
||||
*/
|
||||
public function setField($field)
|
||||
{
|
||||
$this->field = $field;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets verbose message
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@ -28,20 +28,16 @@
|
||||
|
||||
namespace OPNsense\Base;
|
||||
|
||||
use Phalcon\Messages\Messages;
|
||||
|
||||
class Validation
|
||||
{
|
||||
private $validators = [];
|
||||
private $messages = null;
|
||||
private $phalcon_validation = null;
|
||||
private $data = [];
|
||||
|
||||
public function __construct($validators = [])
|
||||
{
|
||||
$this->validators = $validators;
|
||||
$this->phalcon_validation = new \Phalcon\Filter\Validation();
|
||||
$this->messages = new Messages();
|
||||
$this->messages = [];
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
@ -51,7 +47,7 @@ class Validation
|
||||
*/
|
||||
public function appendMessage($message)
|
||||
{
|
||||
$this->messages->appendMessage($message);
|
||||
$this->messages[] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,14 +60,10 @@ class Validation
|
||||
*/
|
||||
public function add($key, $validator)
|
||||
{
|
||||
if (is_a($validator, "OPNsense\\Base\\BaseValidator")) {
|
||||
if (empty($this->validators[$key])) {
|
||||
$this->validators[$key] = [];
|
||||
}
|
||||
$this->validators[$key][] = $validator;
|
||||
} else {
|
||||
$this->phalcon_validation->add($key, $validator);
|
||||
if (empty($this->validators[$key])) {
|
||||
$this->validators[$key] = [];
|
||||
}
|
||||
$this->validators[$key][] = $validator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@ -82,17 +74,20 @@ class Validation
|
||||
*/
|
||||
public function validate($data)
|
||||
{
|
||||
$validatorData = $this->validators;
|
||||
$this->data = $data;
|
||||
|
||||
foreach ($validatorData as $field => $validators) {
|
||||
$phalcon_validation = new \Phalcon\Filter\Validation();
|
||||
foreach ($this->validators as $field => $validators) {
|
||||
foreach ($validators as $validator) {
|
||||
$validator->validate($this, $field);
|
||||
if (is_a($validator, 'Phalcon\Filter\Validation\ValidatorInterface')) {
|
||||
$phalcon_validation->add($field, $validator);
|
||||
} else {
|
||||
$validator->validate($this, $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// XXX: temporary dual validation
|
||||
$phalconMsgs = $this->phalcon_validation->validate($data);
|
||||
$phalconMsgs = $phalcon_validation->validate($data);
|
||||
if (!empty($phalconMsgs)) {
|
||||
foreach ($phalconMsgs as $phalconMsg) {
|
||||
$this->messages[] = $phalconMsg;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user