Valitron\Validator::__construct PHP Method

__construct() public method

Setup validation
public __construct ( array $data, array $fields = [], string $lang = null, string $langDir = null )
$data array
$fields array
$lang string
$langDir string
    public function __construct($data, $fields = array(), $lang = null, $langDir = null)
    {
        // Allows filtering of used input fields against optional second array of field names allowed
        // This is useful for limiting raw $_POST or $_GET data to only known fields
        $this->_fields = !empty($fields) ? array_intersect_key($data, array_flip($fields)) : $data;
        // set lang in the follow order: constructor param, static::$_lang, default to en
        $lang = $lang ?: static::lang();
        // set langDir in the follow order: constructor param, static::$_langDir, default to package lang dir
        $langDir = $langDir ?: static::langDir();
        // Load language file in directory
        $langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
        if (stream_resolve_include_path($langFile)) {
            $langMessages = (include $langFile);
            static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
        } else {
            throw new \InvalidArgumentException("Fail to load language file '" . $langFile . "'");
        }
    }

Usage Example

 /** Create a new server-side validator.
  *
  * @param RequestSchema $schema A RequestSchema object, containing the validation rules.
  * @param MessageTranslator $translator A MessageTranslator to be used to translate message ids found in the schema.
  */
 public function __construct($schema, $translator)
 {
     // Set schema
     $this->setSchema($schema);
     // Set translator
     $this->_translator = $translator;
     // TODO: use locale of translator to determine Valitron language?
     // Construct the parent with an empty data array.
     parent::__construct([]);
 }
All Usage Examples Of Valitron\Validator::__construct