Skip to content

Commit

Permalink
mvc: Phalcon framework dependency (#6389)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AdSchellevis committed Feb 13, 2024
1 parent 203a034 commit c2ea9aa
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 18 deletions.
89 changes: 89 additions & 0 deletions src/opnsense/mvc/app/models/OPNsense/Base/Messages/Message.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
31 changes: 13 additions & 18 deletions src/opnsense/mvc/app/models/OPNsense/Base/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}

Expand All @@ -51,7 +47,7 @@ public function __construct($validators = [])
*/
public function appendMessage($message)
{
$this->messages->appendMessage($message);
$this->messages[] = $message;
}

/**
Expand All @@ -64,14 +60,10 @@ public function appendMessage($message)
*/
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;
}

Expand All @@ -82,17 +74,20 @@ public function add($key, $validator)
*/
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;
Expand Down

0 comments on commit c2ea9aa

Please sign in to comment.