/** * @param string|array $keys chain keys * @param mixed $default * @param Sanitize $sanitize * @return mixed|null */ public function get($keys, $default = null, Sanitize $sanitize = null) { if (!($result = ArrayHelper::getValue(Serialize::unserializeRecursive($_COOKIE), $keys))) { return $default; } return $this->sanitize($result, $sanitize); }
/** * Unserialize. * * @param string $value serialized array * @param array $params params: * * - key * - separator * * @return string */ public static function unserialize($value, array $params) { if (empty($value)) { return null; } if (!empty($params['key'])) { return ArrayHelper::getValue(Serialize::unserialize($value, false), explode(Helper::getValue($params['separator'], '.'), $params['key'])); } return Serialize::unserialize($value, false); }
public function sanitize($input) { $object = null; $attributes = $this->attributes; if (is_object($input)) { $object = $input; $input = (array) $input; } if ($attributes instanceof Sanitize) { $attributes = $this->each($attributes, $input); } $result = []; foreach ($attributes as $attribute => $sanitize) { if (!$sanitize instanceof Sanitize) { throw new SanitizeException("`{$attribute}` is not `" . Sanitize::className() . "`"); } if ($attribute === $this->remainder) { $result = array_merge($result, $this->remainder($sanitize, $attributes, $input)); continue; } if (strrpos($attribute, '.') !== false && ArrayHelper::getValue($input, explode('.', $attribute))) { $input = $this->chain($sanitize, $input, $attribute); continue; } if (!isset($input[$attribute])) { continue; } if (is_array($input[$attribute]) || is_object($input[$attribute])) { if (!$this->recursive) { continue; } $this->attributes = is_array($this->attributes) && isset($this->attributes[$attribute]) ? $this->attributes[$attribute] : $this->attributes; $result[$attribute] = $this->sanitize($input[$attribute]); continue; } $result[$attribute] = $sanitize->sanitize($input[$attribute]); } $result = is_int(key($result)) ? $result + $input : array_merge($input, $result); if (isset($object)) { foreach ($result as $property => $value) { $object->{$property} = $value; } return $object; } return $result; }
/** * Returns a constant. * @param string|array $name name of constant * @param int $sanitize * @return mixed */ public function getConst($name, $sanitize = null) { return $this->sanitize(ArrayHelper::getValue(static::$constants, $name), $sanitize); }
/** * @inheritdoc */ public function get($keys, $default = null) { $array = $_SESSION; return ArrayHelper::getValue($array, $keys, $default); }
protected function translateInternal($keys, array $placeholders = []) { if (!isset(static::$data[$this->locale][$this->category])) { static::$data[$this->locale][$this->category] = []; } $result = ArrayHelper::getValue(static::$data[$this->locale][$this->category], $keys); if (!isset($result)) { if ($this->throwException) { $keys = is_array($keys) ? implode('][', $keys) : $keys; throw new i18nException(i18nException::UNKNOWN_I18N, ['name' => "{$this->category}[{$keys}]"]); } return null; } return StringHelper::replace($result, $placeholders, $this->removeBraces); }
/** * Draws a text string on an existing image. * * @param string $filename the image file path or path alias. * @param string $text the text to write to the image * @param string $fontFile the file path or path alias * @param array $start the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates. * @param array $fontOptions the font options. The following options may be specified: * * - color: The font color. Defaults to "fff". * - size: The font size. Defaults to 12. * - angle: The angle to use to write the text. Defaults to 0. * * @return ImageInterface * @throws \Exception if `$fontOptions` is invalid */ public static function text($filename, $text, $fontFile, array $start = [0, 0], array $fontOptions = []) { if (!isset($start[0], $start[1])) { throw new ImagineException('$start must be an array of two elements.'); } $fontSize = ArrayHelper::getValue($fontOptions, ['size'], 12); $fontColor = ArrayHelper::getValue($fontOptions, ['color'], 'fff'); $fontAngle = ArrayHelper::getValue($fontOptions, ['angle'], 0); $img = static::getImagine()->open(Alias::getAlias($filename)); $font = static::getImagine()->font(Alias::getAlias($fontFile), $fontSize, (new RGB())->color($fontColor)); $img->draw()->text($text, $font, new Point($start[0], $start[1]), $fontAngle); return $img; }
use rock\security\Security; use rock\template\Template; return array_merge(['route' => ['class' => \rock\route\Route::className()], 'access' => ['class' => \rock\access\Access::className()], 'behavior' => ['class' => \rock\components\Behavior::className()], 'db' => ['class' => \rock\db\Connection::className(), 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', 'dsn' => 'mysql:host=localhost;dbname=rockdemo;charset=utf8', 'tablePrefix' => 'spt_', 'aliasSeparator' => '__'], 'BatchQueryResult' => ['class' => BatchQueryResult::className()], 'template' => ['class' => Template::className(), 'locale' => [\rock\LocaleProperties::className(), 'locale'], 'autoEscape' => Template::ESCAPE | Template::TO_TYPE, 'handlerLink' => function ($link, Template $template, array $params = []) { if (!($link = Alias::getAlias("@{$link}", [], false))) { return '#'; } return $template->autoEscape(\rock\template\filters\BaseFilter::modifyUrl($link, $params)); }, 'extensions' => ['cfg' => function (array $keys) { return \rock\helpers\ArrayHelper::getValue(Rock::$config, $keys); }, 'user' => function (array $keys) { if (current($keys) === 'isGuest') { return Rock::$app->user->isGuest(); } elseif (in_array(current($keys), ['isLogged', 'isAuthenticated'], true)) { return !Rock::$app->user->isGuest(); } return \rock\helpers\ArrayHelper::getValue(Rock::$app->user->getAll(), $keys); }, 'call' => function (array $call, array $params = [], Template $template) { if (!isset($call[1])) { $call[1] = null; } list($class, $method) = $call; if ($class === 'context') { $object = $template->context; $function = [$object, $method]; } elseif (function_exists($class) && !$class instanceof \Closure) { return call_user_func_array($class, $params); } else { $object = \rock\di\Container::load($class); if (!method_exists($object, $method)) { throw new \rock\base\BaseException(\rock\base\BaseException::UNKNOWN_METHOD, ['method' => "{$class}::{$method}"]); }
protected function rawPostInternal($name = null, $default = null) { $params = $this->getBodyParams(); if (!isset($name)) { return $params; } return ArrayHelper::getValue($params, $name, $default); }
/** * Renders the option tags. * * That can be used by {@see \rock\template\Html::dropDownList()} and {@see \rock\template\Html::listBox()}. * * @param string|array $selection the selected value(s). This can be either a string for single selection * or an array for multiple selections. * @param array $items the option data items. The array keys are option values, and the array values * are the corresponding option labels. The array can also be nested (i.e. some array values are arrays too). * For each sub-array, an option group will be generated whose label is the key associated with the sub-array. * If you have a list of data models, you may convert them into the format described above using {@see \rock\helpers\ArrayHelper::map()}. * * Note, the values and labels will be automatically HTML-encoded by this method, and the blank spaces in * the labels will also be HTML-encoded. * @param array $tagOptions the $options parameter that * is passed to the {@see \rock\template\Html::dropDownList()} or {@see \rock\template\Html::listBox()} call. * This method will take out these elements, if any: "prompt", "options" and "groups". See more details * in {@see \rock\template\Html::dropDownList()} for the explanation of these elements. * * @return string the generated list options */ public static function renderSelectOptions($selection, $items, &$tagOptions = []) { $lines = []; $encodeSpaces = static::remove($tagOptions, 'encodeSpaces', false); if (isset($tagOptions['prompt'])) { $prompt = $encodeSpaces ? str_replace(' ', ' ', static::encode($tagOptions['prompt'])) : static::encode($tagOptions['prompt']); $lines[] = static::tag('option', $prompt, ['value' => '']); } $options = isset($tagOptions['options']) ? $tagOptions['options'] : []; $groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : []; unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']); $options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces); foreach ($items as $key => $value) { if (is_array($value)) { $groupAttrs = isset($groups[$key]) ? $groups[$key] : []; $groupAttrs['label'] = $key; $attrs = ['options' => $options, 'groups' => $groups, 'encodeSpaces' => $encodeSpaces]; $content = static::renderSelectOptions($selection, $value, $attrs); $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); } else { $attrs = isset($options[$key]) ? $options[$key] : []; $attrs['value'] = (string) $key; $attrs['selected'] = $selection !== null && (!is_array($selection) && !strcmp($key, $selection) || is_array($selection) && in_array($key, $selection)); $lines[] = static::tag('option', $encodeSpaces ? str_replace(' ', ' ', static::encode($value)) : static::encode($value), $attrs); } } return implode("\n", $lines); }
/** * Converts an object or an array of objects into an array. * * @param object|array $object the object to be converted into an array * @param array $properties a mapping from object class names to the properties that need to put into the resulting arrays. * The properties specified for each class is an array of the following format: * * ```php * [ * 'apps\models\Post' => [ * 'id', * 'title', * // the key name in array result => property name * 'createTime' => 'created_at', * // the key name in array result => anonymous function * 'length' => function ($post) { * return strlen($post->content); * }, * ], * ] * ``` * * The result of `Model::convert($post, $properties)` could be like the following: * * ```php * [ * 'id' => 123, * 'title' => 'test', * 'createTime' => '2013-01-01 12:00AM', * 'length' => 301, * ] * ``` * * @param boolean $recursive whether to recursively converts properties which are objects into arrays. * @return array the array representation of the object */ public static function convert($object, $properties = [], $recursive = true) { if (is_array($object)) { if ($recursive) { foreach ($object as $key => $value) { if (is_array($value) || is_object($value)) { $object[$key] = static::convert($value, $properties, true); } } } return $object; } elseif (is_object($object)) { if (!empty($properties)) { $className = get_class($object); if (!empty($properties[$className])) { $result = []; foreach ($properties[$className] as $key => $name) { if (is_int($key)) { $result[$name] = $object->{$name}; } else { $result[$key] = ArrayHelper::getValue($object, $name); } } return $recursive ? static::convert($result) : $result; } } if ($object instanceof Arrayable) { $result = $object->toArray(); } else { $result = []; foreach ($object as $key => $value) { $result[$key] = $value; } } return $recursive ? static::convert($result) : $result; } else { return [$object]; } }