fix: quick fix to enforce support of x509 database connection on install (#6157)
[openemr.git] / src / Validators / BaseValidator.php
blobc8ce687cd63e1fd689c91158a410656f257ec978
1 <?php
3 namespace OpenEMR\Validators;
5 use OpenEMR\Common\Uuid\UuidRegistry;
6 use OpenEMR\Validators\ProcessingResult;
7 use Particle\Validator\Validator;
8 use Ramsey\Uuid\Exception\InvalidUuidStringException;
10 /**
11 * Base class for OpenEMR object validation.
12 * Validation processes are implemented using Particle (https://github.com/particle-php/Validator)
13 * @package OpenEMR
14 * @link http://www.open-emr.org
15 * @author Dixon Whitmire <dixonwh@gmail.com>
16 * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
17 * @copyright Copyright (c) 2020 Dixon Whitmire <dixonwh@gmail.com>
18 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
20 abstract class BaseValidator
22 // supported validation contexts for database operations
23 public const DATABASE_INSERT_CONTEXT = "db-insert";
24 public const DATABASE_UPDATE_CONTEXT = "db-update";
26 protected $validator;
28 protected $supportedContexts;
30 /**
31 * Configures the validator instance with validation requirements and rules.
32 * This default implementation sets the validator's supported context to include
33 * database inserts and updates.
35 protected function configureValidator()
37 array_push($this->supportedContexts, self::DATABASE_INSERT_CONTEXT, self::DATABASE_UPDATE_CONTEXT);
40 public function __construct()
42 $this->validator = new Validator();
43 $this->supportedContexts = [];
44 $this->configureValidator();
47 /**
48 * @return true if the requested context is supported by the validator instance.
50 private function isValidContext($context)
52 return in_array($context, $this->supportedContexts);
55 /**
56 * Performs a data validation using the configured rules and requirements.
58 * Validation results are conveyed by an array with the following keys:
59 * - isValid => true|false
60 * - messages => array(validationMessage, validationMessage, etc)
62 * @param $dataFields - The fields to validate.
63 * @param $context - The validation context to utilize. This is simply a "handle" for the rules.
64 * @return $validationResult array
66 public function validate($dataFields, $context)
68 if (!$this->isValidContext($context)) {
69 throw new \RuntimeException("unsupported context: " . $context);
72 $validationResult = $this->validator->validate($dataFields, $context);
74 $result = new ProcessingResult();
75 $result->setValidationMessages($validationResult->getMessages());
77 return $result;
80 /**
81 * Validates that a ID exists in the database.
83 * @param $field The identifier field in database
84 * @param $table The table in database
85 * @param $lookupId The identifier to validateId
86 * @param $isUuid true if the lookupId is UUID, otherwise false
87 * @return true if the lookupId is a valid existing id, otherwise Validation Message
89 public static function validateId($field, $table, $lookupId, $isUuid = false)
91 $validationResult = new ProcessingResult();
93 // Error Message
94 $validationMessages = [
95 $field => ["invalid or nonexisting value" => "value " . $lookupId],
97 $validationResult->setValidationMessages($validationMessages);
99 // Check if $id is not UUID or a Valid Integer
100 if ($isUuid) {
101 try {
102 $lookupId = UuidRegistry::uuidToBytes($lookupId);
103 } catch (InvalidUuidStringException $e) {
104 return $validationResult;
106 } elseif (!is_int(intval($lookupId))) {
107 return $validationResult;
110 $result = sqlQuery(
111 "SELECT $field FROM $table WHERE $field = ?",
112 array($lookupId)
114 if (!empty($result[$field])) {
115 return true;
116 } else {
117 return $validationResult;
122 * Validates that a Code from Valueset exists in the database.
124 * @param $code The code which needs to be verified
125 * @param $table The table in database
126 * @param $valueset Name of the particular Valueset
127 * @return boolean
129 public function validateCode($code, $table, $valueset)
131 $sql = "SELECT option_id FROM $table WHERE list_id = ? AND option_id = ?";
132 $result = sqlQuery(
133 $sql,
134 array($valueset, $code)
136 return $result['option_id'] ? true : false;