Пример #1
0
 /**
  * Validate defined rules in app
  * @return bool
  * @throws SyntaxException
  */
 public final function validate()
 {
     // validate csrf token if required
     if ($this->_tokenRequired && !$this->_tokenOk) {
         App::$Session->getFlashBag()->add('warning', __('Hack attention: security token is wrong!'));
         return false;
     }
     // get all rules as array from method rules()
     $rules = $this->rules();
     // get default values of attributes
     $defaultAttr = $this->getAllProperties();
     // start validation: on this step class attribute values will be changed to input data if it valid
     $success = $this->runValidate($rules);
     // get not-passed validation fields as array
     $badAttributes = $this->getBadAttribute();
     // prevent warnings
     if (Obj::isArray($badAttributes) && count($badAttributes) > 0) {
         foreach ($badAttributes as $attr) {
             if (Str::contains('.', $attr)) {
                 // sounds like dot-separated array attr
                 $attrName = strstr($attr, '.', true);
                 // get attr name
                 $attrArray = trim(strstr($attr, '.'), '.');
                 // get dot-based array path
                 $defaultValue = new DotData($defaultAttr);
                 // load default attr
                 $dotData = new DotData($this->{$attrName});
                 // load local attr variable
                 $dotData->set($attrArray, $defaultValue->get($attr));
                 // set to local prop. variable default value
                 $this->{$attrName} = $dotData->export();
                 // export to model
             } else {
                 $this->{$attr} = $defaultAttr[$attr];
                 // just set ;)
             }
             // add message about wrong attribute to session holder, later display it
             $attrLabel = $attr;
             if ($this->getLabel($attr) !== null) {
                 $attrLabel = $this->getLabel($attr);
             }
             App::$Session->getFlashBag()->add('warning', __('Field "%field%" is incorrect', ['field' => $attrLabel]));
         }
     }
     return $success;
 }
Пример #2
0
 /**
  * Try to recursive validate field by defined rules and set result to model properties if validation is successful passed
  * @param string|array $field_name
  * @param string $filter_name
  * @param mixed $filter_argv
  * @return bool
  * @throws SyntaxException
  */
 public function validateRecursive($field_name, $filter_name, $filter_argv = null)
 {
     // check if we got it from form defined request method
     if (App::$Request->getMethod() !== $this->_sendMethod) {
         return false;
     }
     // get field value from user input data
     $field_value = $this->getFieldValue($field_name);
     $check = false;
     // maybe no filter required?
     if ($filter_name === 'used') {
         $check = true;
     } elseif (Str::contains('::', $filter_name)) {
         // sounds like a callback class::method::method
         // string to array via delimiter ::
         $callbackArray = explode('::', $filter_name);
         // first item is a class name
         $class = array_shift($callbackArray);
         // last item its a function
         $method = array_pop($callbackArray);
         // left any items? maybe post-static callbacks?
         if (count($callbackArray) > 0) {
             foreach ($callbackArray as $obj) {
                 if (Str::startsWith('$', $obj) && property_exists($class, ltrim($obj, '$'))) {
                     // sounds like a variable
                     $obj = ltrim($obj, '$');
                     // trim variable symbol '$'
                     $class = $class::${$obj};
                     // make magic :)
                 } elseif (method_exists($class, $obj)) {
                     // maybe its a function?
                     $class = $class::$obj;
                     // call function
                 } else {
                     throw new SyntaxException('Filter callback execution failed: ' . $filter_name);
                 }
             }
         }
         // check is endpoint method exist
         if (method_exists($class, $method)) {
             $check = @$class::$method($field_value, $filter_argv);
         } else {
             throw new SyntaxException('Filter callback execution failed: ' . $filter_name);
         }
     } elseif (method_exists('Ffcms\\Core\\Filter\\Native', $filter_name)) {
         // only full namespace\class path based :(
         if ($filter_argv != null) {
             $check = Native::$filter_name($field_value, $filter_argv);
         } else {
             $check = Native::$filter_name($field_value);
         }
     } else {
         throw new SyntaxException('Filter "' . $filter_name . '" is not exist');
     }
     if ($check !== true) {
         // switch only on fail check.
         $this->_badAttr[] = $field_name;
     } else {
         $field_set_name = $field_name;
         // prevent array-type setting
         if (Str::contains('.', $field_set_name)) {
             $field_set_name = strstr($field_set_name, '.', true);
         }
         if (property_exists($this, $field_set_name)) {
             if ($field_name !== $field_set_name) {
                 // array-based property
                 $dot_path = trim(strstr($field_name, '.'), '.');
                 // prevent throws any exceptions for null and false objects
                 if (!Obj::isArray($this->{$field_set_name})) {
                     $this->{$field_set_name} = [];
                 }
                 // use dot-data provider to compile output array
                 $dotData = new DotData($this->{$field_set_name});
                 $dotData->set($dot_path, $field_value);
                 // todo: check me!!! Here can be bug of fail parsing dots and passing path-value
                 // export data from dot-data lib to model property
                 $this->{$field_set_name} = $dotData->export();
             } else {
                 // just single property
                 $this->{$field_name} = $field_value;
                 // refresh model property's from post data
             }
         }
     }
     return $check;
 }
Пример #3
0
 /**
  * Get data as array.
  *
  * @return array
  */
 public function getAllAsArray()
 {
     return $this->data->export();
 }