/** * load a json string or json file and decode it. will throw error if * * @error 16507 * @see json_decode * @param string $json expects json string or json file to decode * @param bool $assoc expects value whether to force objects into associative arrays * @param int $depth expects max recursion depth * @param int $options bitmask options * @return mixed * @throws Xapp_Util_Json_Exception */ public static function load($json, $assoc = false, $depth = 512, $options = 0) { if (is_string($json)) { if (xapp_is('file', $json)) { $json = trim((string) file_get_contents($json)); } if (self::isJson($json)) { return self::decode($json, $assoc, $depth, $options); } } throw new Xapp_Util_Json_Exception(_("input argument 1 is not a decodable json value"), 1650701); }
/** * Validates an array of options using options metainformation * * @param $optionsValues array : options * @param $optionsMeta array : options meta: dictionary, rules, defaults and pre-processors * @param $errors array : returns errors found during validation */ public static function validate($optionsValues, $optionsMeta, &$errors) { // apply defaults if (array_key_exists(self::OPTIONS_DEFAULT, $optionsMeta)) { foreach ($optionsMeta[self::OPTIONS_DEFAULT] as $key => $default) { if (!isset($optionsValues[$key])) { $optionsValues[$key] = $default; } } } // apply pre-processors if (array_key_exists(self::OPTIONS_PREPROCESSORS, $optionsMeta)) { foreach ($optionsMeta[self::OPTIONS_PREPROCESSORS] as $key => $methods) { if (isset($optionsValues[$key])) { foreach ($methods as $method) { $optionsValues[$key] = call_user_func($method, $optionsValues[$key]); } } } } // check types if (array_key_exists(self::OPTIONS_DICT, $optionsMeta)) { foreach ($optionsMeta[self::OPTIONS_DICT] as $key => $type) { if (isset($optionsValues[$key])) { if (!xapp_is($type, $optionsValues[$key])) { $errors[] = XAPP_TEXT_FORMATTED("INCORRECT_OPTION_TYPE", array($optionsValues[$key], $type)); } } } } // check rules if (array_key_exists(self::OPTIONS_RULE, $optionsMeta)) { foreach ($optionsMeta[self::OPTIONS_RULE] as $key => $requested) { if (!isset($optionsValues[$key])) { $errors[] = XAPP_TEXT_FORMATTED("REQUIRED_OPTION_NOT_PROVIDED", array($key)); } } } }
/** * xapp option shortcut function for xapp_has_option this will use xapp_has_option with third parameter strict = true * so option value has to be a valid value other then null, false, empty array. see xapp_has_option * * @see xapp_has_option * @param null|string $key expects the options key name to check * @param null|object|string|array $mixed expects value according to explanation above * @return bool */ function xapp_is_option($key = null, &$mixed = null) { if ($key !== null) { if (is_array($mixed)) { return xapp_is('value', $mixed[$key]); } else { if (xapped()) { return Xapp::hasOption($key, $mixed, true); } else { if (is_object($mixed) && xapp_can_options($mixed)) { return array_key_exists($key, $mixed->options) && xapp_is('value', $mixed->options[$key]) ? true : false; } else { if (is_string($mixed) && xapp_can_options($mixed)) { return array_key_exists($key, $mixed::$options) && xapp_is('value', $mixed::$options[$key]) ? true : false; } } } } } return false; }