forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseValidator.php
138 lines (121 loc) · 4.48 KB
/
BaseValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace OpenEMR\Validators;
use OpenEMR\Common\Uuid\UuidRegistry;
use OpenEMR\Validators\ProcessingResult;
use Particle\Validator\Validator;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
/**
* Base class for OpenEMR object validation.
* Validation processes are implemented using Particle (https://github.com/particle-php/Validator)
* @package OpenEMR
* @link http://www.open-emr.org
* @author Dixon Whitmire <[email protected]>
* @copyright Copyright (c) 2020 Jerry Padgett <[email protected]>
* @copyright Copyright (c) 2020 Dixon Whitmire <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
abstract class BaseValidator
{
// supported validation contexts for database operations
public const DATABASE_INSERT_CONTEXT = "db-insert";
public const DATABASE_UPDATE_CONTEXT = "db-update";
protected $validator;
protected $supportedContexts;
/**
* Configures the validator instance with validation requirements and rules.
* This default implementation sets the validator's supported context to include
* database inserts and updates.
*/
protected function configureValidator()
{
array_push($this->supportedContexts, self::DATABASE_INSERT_CONTEXT, self::DATABASE_UPDATE_CONTEXT);
}
public function __construct()
{
$this->validator = new Validator();
$this->supportedContexts = [];
$this->configureValidator();
}
/**
* @return true if the requested context is supported by the validator instance.
*/
private function isValidContext($context)
{
return in_array($context, $this->supportedContexts);
}
/**
* Performs a data validation using the configured rules and requirements.
*
* Validation results are conveyed by an array with the following keys:
* - isValid => true|false
* - messages => array(validationMessage, validationMessage, etc)
*
* @param $dataFields - The fields to validate.
* @param $context - The validation context to utilize. This is simply a "handle" for the rules.
* @return $validationResult array
*/
public function validate($dataFields, $context)
{
if (!$this->isValidContext($context)) {
throw new \RuntimeException("unsupported context: " . $context);
}
$validationResult = $this->validator->validate($dataFields, $context);
$result = new ProcessingResult();
$result->setValidationMessages($validationResult->getMessages());
return $result;
}
/**
* Validates that a ID exists in the database.
*
* @param $field The identifier field in database
* @param $table The table in database
* @param $lookupId The identifier to validateId
* @param $isUuid true if the lookupId is UUID, otherwise false
* @return true if the lookupId is a valid existing id, otherwise Validation Message
*/
public static function validateId($field, $table, $lookupId, $isUuid = false)
{
$validationResult = new ProcessingResult();
// Error Message
$validationMessages = [
$field => ["invalid or nonexisting value" => "value " . $lookupId],
];
$validationResult->setValidationMessages($validationMessages);
// Check if $id is not UUID or a Valid Integer
if ($isUuid) {
try {
$lookupId = UuidRegistry::uuidToBytes($lookupId);
} catch (InvalidUuidStringException $e) {
return $validationResult;
}
} elseif (!is_int(intval($lookupId))) {
return $validationResult;
}
$result = sqlQuery(
"SELECT $field FROM $table WHERE $field = ?",
array($lookupId)
);
if (!empty($result[$field])) {
return true;
} else {
return $validationResult;
}
}
/**
* Validates that a Code from Valueset exists in the database.
*
* @param $code The code which needs to be verified
* @param $table The table in database
* @param $valueset Name of the particular Valueset
* @return boolean
*/
public function validateCode($code, $table, $valueset)
{
$sql = "SELECT option_id FROM $table WHERE list_id = ? AND option_id = ?";
$result = sqlQuery(
$sql,
array($valueset, $code)
);
return $result['option_id'] ? true : false;
}
}