Beispiel #1
0
 public static function config($path)
 {
     static $config;
     if ($config === NULL) {
         $configFile = __DIR__ . '/' . 'Config.php';
         if (!file_exists($configFile)) {
             throw new \Exception('Configuration file not found, please create app/Config.php to continue.');
         }
         $config = new DotData(require $configFile);
     }
     return $config->get($path);
 }
 /**
  * @param $association
  * @param ClassMetadataInfo $meta
  * @param $instance
  * @param Data $params
  * @throws \Doctrine\ORM\Mapping\MappingException
  */
 private function handleAssociation($association, ClassMetadataInfo $meta, $instance, Data $params)
 {
     $data = $params->get($association);
     if (is_array($data) && isset($data[0])) {
         foreach ($data as $index => $set) {
             $assoc = $this->getAssocicationHandler($association, $meta);
             $assoc->handle($association, $meta, $instance, new Data([$association => $set]));
         }
     } else {
         $assoc = $this->getAssocicationHandler($association, $meta);
         $assoc->handle($association, $meta, $instance, $params);
     }
 }
 public function handle($association, ClassMetadataInfo $meta, $instance, Data $params)
 {
     if ($params->get($association) === null) {
         return;
     }
     $this->association = $association;
     $this->meta = $meta;
     $this->instance = $instance;
     $this->params = $params;
     switch (true) {
         case $this->isSelfReferential():
             $this->selfReferential();
             break;
         case $this->isUniDirectional():
             $this->uniDirectional();
             break;
         case $this->isBiDirectional():
             $this->biDirectional();
             break;
     }
 }
Beispiel #4
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;
 }
 /**
  * Returns the value of option
  *
  * @param string $name Name of option
  * @param mixed $default Default value
  * @return array|mixed|null Value of option
  */
 public function getOption($name, $default = null)
 {
     $data = new Data((array) get_option($this->registry['option_name']));
     return $data->get($name, $default);
 }
 /**
  * Return a translation string from files using dot notation
  *
  * @param $id
  * @param string $lang
  * @param null $default
  * @return array|mixed|null
  */
 public function get_translation($id, $lang = 'en', $default = null)
 {
     PerchUtil::debug('Using translation: ' . $lang . '.' . $id, 'success');
     return $this->translations->get($lang . '.' . $id, $default ? $default : $id);
 }
Beispiel #7
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;
 }
Beispiel #8
0
 /**
  * Return a config value.
  *
  * @param string $key
  * @param string $default
  *
  * @return array|mixed|null
  */
 public function get($key, $default = '')
 {
     return $this->data->get($key, $default);
 }