Skip to content
Snippets Groups Projects
Select Git revision
  • dda5806af9a8f9d143d50893e541e10c81efbd9f
  • master default protected
  • Legacy_Php7
3 results

projectlist.class.php

Blame
  • validateparam.class.php 6.89 KiB
    <?php
    class validateparam
    {
    
        /**
         * Summary of _aValidationDefs
         * @var array
         */
        protected array $_aValidationDefs = [];
    
        protected bool $_flag_debug = false;
    
    
        /**
         * Write debug line if _flag_debug is set to true
         * @param string $s  message to show
         * @return void
         */
        protected function _wd(string $s)
        {
            if ($this->_flag_debug) {
                echo "DEBUG: $s<br>\n";
            }
        }
    
        /**
         * Include the default validation rules defined in validateparam.settings.php
         */
        public function __construct(){
            // IMPORTANT:
            // This style is not so nice like <variable> = include <file>;
            // keep the include line unchanged - it is detected by installer 
            // CLI client project (amcli)
            include 'validateparam.settings.php';
        }
    
        /**
         * shared validaten checks for floor and integer
         * 
         * @param array $aOpt  Validation rules
         * @param mixed $value value to verify
         * @return string with found error
         */
        protected function _checkCount(array $aOpt, mixed $value)
        {
            $sError = "";
    
            if ($aOpt['min'] ?? false) {
                if ($value < $aOpt['min']) {
                    $sError .= "Value is too small; minimum is $aOpt[min]. ";
                }
            }
            if ($aOpt['max'] ?? false) {
                if ($value > $aOpt['max']) {
                    $sError .= "Value is too big; maximum is $aOpt[max]. ";
                }
            }
            if (isset($aOpt['oneof']) && is_array($aOpt['oneof'])) {
                if (array_search($value, $aOpt['oneof']) == false) {
                    $sError .= "Value is invalid. Value doesn't match one of these values " . print_r($aOpt['oneof']);
                }
            }
    
            return $sError;
        }
    
        /**
         * Validate a single value. It returns a string with an errormessage.
         * No error means ok.
         *