protected function handleDotCase(&$array, $key, $value) { if (strpos($key, '.') !== false) { unset($array[$key]); Arr::set($array, $key, $value); } }
/** * Set a given configuration value. * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { $keys = is_array($key) ? $key : [$key => $value]; foreach ($keys as $key => $value) { Arr::set($this->items, $key, $value); } }
public function testPasswordResetUsingConfirmationCode() { if (!$this->serviceExists('mymail')) { $emailService = \DreamFactory\Core\Models\Service::create(["name" => "mymail", "label" => "Test mail service", "description" => "Test mail service", "is_active" => true, "type" => "local_email", "mutable" => true, "deletable" => true, "config" => ["driver" => "sendmail", "command" => "/usr/sbin/sendmail -bs"]]); $userConfig = \DreamFactory\Core\User\Models\UserConfig::find(4); $userConfig->password_email_service_id = $emailService->id; $userConfig->save(); } if (!\DreamFactory\Core\Models\EmailTemplate::whereName('mytemplate')->exists()) { $template = \DreamFactory\Core\Models\EmailTemplate::create(['name' => 'mytemplate', 'description' => 'test', 'to' => $this->user2['email'], 'subject' => 'rest password test', 'body_text' => 'link {link}']); $userConfig = \DreamFactory\Core\User\Models\UserConfig::find(4); $userConfig->password_email_template_id = $template->id; $userConfig->save(); } Arr::set($this->user2, 'email', '*****@*****.**'); $user = $this->createUser(2); Config::set('mail.pretend', true); $rs = $this->makeRequest(Verbs::POST, static::RESOURCE, ['reset' => 'true'], ['email' => $user['email']]); $content = $rs->getContent(); $this->assertTrue($content['success']); /** @var User $userModel */ $userModel = User::find($user['id']); $code = $userModel->confirm_code; $rs = $this->makeRequest(Verbs::POST, static::RESOURCE, ['login' => 'true'], ['email' => $user['email'], 'code' => $code, 'new_password' => '778877']); $content = $rs->getContent(); $this->assertTrue($content['success']); $this->assertTrue(Session::isAuthenticated()); $userModel = User::find($user['id']); $this->assertEquals('y', $userModel->confirm_code); $this->service = ServiceHandler::getService($this->serviceId); $rs = $this->makeRequest(Verbs::POST, 'session', [], ['email' => $user['email'], 'password' => '778877']); $content = $rs->getContent(); $this->assertTrue(!empty($content['session_id'])); }
/** * Replace a single item in a given Request’s input using dot-notation * @param Request $request to modify * @param string $key in dot-notation * @param mixed $value to set */ protected function setRequestInput(Request $request, $key, $value) { if (strpos($key, '.')) { // The data to set is deeper than 1 level down // meaning the final value of the input's first level key is expected to be an array list($first_level_key, $key_rest) = explode('.', $key, 2); // Pull out the input's existing first level value to modify it as an array $first_level_value = $request->input($first_level_key); //Request::input() pulls from all input data using dot-notation (ArrayAccess on Request would also pull out files which is undesirable here). if (!is_array($first_level_value)) { $first_level_value = []; } Arr::set($first_level_value, $key_rest, $value); } else { // The data to set is in the first level $first_level_key = $key; $first_level_value = $value; } $request->merge([$first_level_key => $first_level_value]); // The only current alternatives for modifying Request input data are merge() and replace(), the latter replacing the whole input data. /* * It could look tempting to skip all of the above code and just * Arr::set() on the Request object utilizing it's ArrayAccess... * But it doesn't work for the second- or higher dot-levels because of the * non-reference return value of ArrayAccess::offsetGet() */ }
/** * Update setting * * @param $key * @param $value * @return mixed */ public function set($key, $value) { $this->prepare(); if (!$this->has($key) || $this->get($key) != $value) { $this->isDirty = true; } Arr::set($this->data, $key, $value); }
function traitAjax_response() { $data = ['vendor' => static::getVendor(), 'package' => static::getEntity()]; $ret = array_merge($data, $this->traitAjaxParams); $message = Arr::get($ret, 'message'); $message = htmlspecialchars($message); Arr::set($ret, 'message', $message); return $ret; }
/** * Set a given configuration value. * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { if (is_array($key)) { foreach ($key as $innerKey => $innerValue) { Arr::set($this->items, $innerKey, $innerValue); } } else { Arr::set($this->items, $key, $value); } }
/** * Get a subset of the items from the input data. * * @param array|mixed $keys * @return array */ public function only($keys) { $keys = is_array($keys) ? $keys : func_get_args(); $results = []; $input = self::all(); foreach ($keys as $key) { Arr::set($results, $key, Arr::get($input, $key)); } return $results; }
public function testPATCHPassword() { $user = $this->createUser(1); Arr::set($user, 'password', '1234'); $payload = json_encode($user, JSON_UNESCAPED_SLASHES); $rs = $this->makeRequest(Verbs::PATCH, static::RESOURCE . '/' . $user['id'], [], $payload); $content = $rs->getContent(); $this->assertFalse(Session::authenticate(['email' => $user['email'], 'password' => '1234'])); $this->assertTrue($this->adminCheck([$content])); }
/** * Set a value on the server. * * @param string|array $key * @param mixed|null $value */ public function set($key, $value = null) { // Set the value on the contents $contents = (array) $this->getContents(); if (is_array($key)) { $contents = $key; } else { Arr::set($contents, $key, $value); } $this->saveContents($contents); }
function getClean() { $ret = $this->data; foreach ($this->filters as $field => $filters) { foreach ($filters as $filter) { $filtered = call_user_func($filter, Arr::get($ret, $field)); Arr::set($ret, $field, $filtered); } } return $ret; }
public function map($data) { foreach ($data as &$item) { $value = Arr::get($item, $this->name); if ($this->hasValueWrapper()) { $value = call_user_func($this->valueWrapper, $value); Arr::set($item, $this->name, $value); } } return $data; }
/** * Create a new search index. * * @param string|null $index * @param array|null $settings * @param array|null $mappings * @return array */ public function createIndex($index = null, array $settings = null, array $mappings = null) { $indexName = $index ?: $this->getDefaultIndex(); $params = ['index' => $indexName]; if (!empty($settings)) { Arr::set($params, 'body.settings', $settings); } if (!empty($mappings)) { Arr::set($params, 'body.mappings', $mappings); } return $this->client->indices()->create($params); }
static function register($code, $name, $aliases = []) { self::$roles[$code] = $name; ksort(self::$roles); $aliases = (array) $aliases; foreach ($aliases as $alias) { self::$aliases[$alias] = $code; } $code = str_replace('.', '.items.', $code); Arr::set(self::$tree, $code . '.name', $name); Arr::set(self::$tree, $code . '.code', $code); }
function reqOnlyIfExists($keys) { $keys = is_array($keys) ? $keys : func_get_args(); $results = []; $input = request()->all(); foreach ($keys as $key) { if (request()->exists($key)) { Arr::set($results, $key, Arr::get($input, $key)); } } return $results; }
/** * Create/Update a configuration value * * @param string $key * @param mixed $value */ public function set($key, $value = null) { $value = $this->modifiers->convert($key, $value, Modifier::DIRECTION_FROM); // verify the array if (!$this->isValidValue($value)) { $msg = 'Value for "' . $key . '" is invalid. '; $msg .= 'Must be scalar or an array of scalar values'; throw new InvalidArgumentException($msg); } $this->arrHelper->set($this->data, $key, $value); $this->storage && $this->storage->save($key, $value); }
/** * Set a given option value. * * @param array|string $key * @param mixed $value * @return void */ public function set($key, $value = null) { if (is_array($key)) { // If given key is an array foreach ($key as $innerKey => $innerValue) { Arr::set($this->items, $innerKey, $innerValue); $this->itemsModified[] = $innerKey; } } else { Arr::set($this->items, $key, $value); $this->itemsModified[] = $key; } }
/** * Fetch the settings from the database * * @return array */ public function load() { $data = []; $settings = $this->database->select('SELECT * FROM ' . $this->table); foreach ($settings as $setting) { $id = $setting->id; $value = $setting->value; if (strlen($value) && ($value[0] == '[' || $value[0] == '{')) { $value = json_decode($value, 1, 512); } Arr::set($data, $id, $value); } return $data; }
/** * $access_name = 'admin.plugins.module.config_module'; * $path = 'admin._items.plugins._items.module._items.config'; * $default_title = 'Config Module'; * * @param type $access_name */ function autoCreateParent($access_name) { $path = str_replace('.', '._items.', $access_name); $arr = explode('.', $access_name); $tmp = array_pop($arr); $default_title = ucwords(str_replace('_', ' ', $tmp)); if (!Arr::get($this->items, $path . '.item')) { Arr::set($this->items, $path . '.item.text', $default_title); Arr::set($this->items, $path . '.item.url', '#'); } if (!Arr::get($this->items, $path . '.access_name')) { Arr::set($this->items, $path . '.access_name', $access_name); } }
/** * 保存到数据库中 * * @param $key * @param $value * @return mixed */ protected function setModel($key, $value) { Arr::set($this->items, $key, $value); $key = explode('.', $key)[0]; $value = $this->items[$key]; $model = $this->model->where('key', $key)->where('aid', $this->aid)->first(); if (!$model) { $model = clone $model; } $model->aid = $this->aid; $model->key = $key; $model->value = $value; return $model->save(); }
public function setConfig(Application $app, $data) { $db = $app['config']->get('database'); if (!$db) { return false; } $driver = $db['default']; $post_data = ['connections' => [$driver => $data]]; $config = array_merge(Arr::dot($db), Arr::dot($post_data)); $a = []; foreach ($config as $k => $v) { Arr::set($a, $k, $v); } $app['config']->set('database', $a); return $driver; }
/** * Register all active extension to dispatcher. * * @return void */ protected function registerActiveExtensions() { $available = $this->memory->get('extensions.available', []); $active = $this->memory->get('extensions.active', []); // Loop all active extension and merge the configuration with // available config. Extension registration is handled by dispatcher // process due to complexity of extension boot process. foreach ($active as $name => $options) { if (isset($available[$name])) { $config = array_merge((array) Arr::get($available, "{$name}.config"), (array) Arr::get($options, 'config')); Arr::set($options, 'config', $config); $this->extensions[$name] = $options; $this->dispatcher->register($name, $options); } } }
public function setConfig(Application $app, DatabaseRequest $request) { $driver = $request->get('driver'); $connections = $this->connections(); $config = array_merge($connections[$driver], $request->get($driver)); if (isset($config['default'])) { unset($config['default']); } $config = array_merge(Arr::dot($app['config']->get('database')), Arr::dot(['default' => $driver, 'connections' => [$driver => $config]])); $a = []; foreach ($config as $k => $v) { Arr::set($a, $k, $v); } $app['config']->set('database', $a); return $driver; }
/** * Get the current user's roles of the application. * * If the user is a guest, empty array should be returned. * * @return array */ public function roles() { $user = $this->user(); $userId = 0; // This is a simple check to detect if the user is actually logged-in, // otherwise it's just as the same as setting userId as 0. is_null($user) || ($userId = $user->getAuthIdentifier()); $roles = Arr::get($this->userRoles, "{$userId}", []); // This operation might be called more than once in a request, by // cached the event result we can avoid duplicate events being fired. if (empty($roles)) { $roles = $this->getUserRolesFromEventDispatcher($user, $roles); } Arr::set($this->userRoles, "{$userId}", $roles); return $roles; }
function branch($path = '') { $priorities = (array) Arr::get($this->childs, $path); if (count($priorities)) { krsort($priorities); foreach ($priorities as $priority => $elements) { foreach ($elements as $element) { $element_path = str_replace('.', '._items_.', $element); $e = Arr::get($this->items, $element); $a = Arr::get($this->tree, $element_path, []); Arr::set($this->tree, $element_path, array_merge($a, $e)); $this->branch($element); } } } return $this->tree; }
/** * Give an item to the traveler * * @param mixed $value Value to assoicate to key * @param string $key Dot notation key, default: null * @return void */ public function give($value, $key = null) { if ($key === null) { if ($value === null) { return; } if (!is_array($value)) { $class = new ReflectionClass($value); $key = snake_case($class->getShortName()); } else { foreach ($value as $key => $item) { Arr::set($this->items, $key, $item); } return; } } $this->items = Arr::set($this->items, strtolower($key), $value); }
/** * Get the hydrated models without eager loading. * * @param array $columns * * @return \Illuminate\Database\Eloquent\Model[] */ public function getModels($columns = ['*']) { $results = $this->query->get($columns); $connection = $this->model->getConnectionName(); // Check for joined relations if (!empty($this->joined)) { foreach ($results as $key => $result) { $relation_values = []; foreach ($result as $column => $value) { Arr::set($relation_values, $column, $value); } foreach ($this->joined as $relationName) { $relation = $this->getRelation($relationName); $relation_values[$relationName] = $relation->getRelated()->newFromBuilder(Arr::pull($relation_values, $relationName), $connection); } $results[$key] = $relation_values; } } return $this->model->hydrate($results, $connection)->all(); }
function setRelations($relations) { if (false === $relations) { return $this; } $ret = []; foreach ($relations as $relation) { Arr::set($ret, $relation, []); } $relations = $ret; $accessor = Accessor::factory($this->model); foreach ($relations as $relation => $subrelation) { $method = \Str::camel('hasmany_' . $relation); if (method_exists($accessor, $method)) { $data = call_user_func([$accessor, $method]); $this->addRelation($relation, $data, $subrelation); } } return $this; }
/** * Release a pushed job back onto the queue. * * @param int $delay * @return void */ protected function recreateJob($delay) { $payload = json_decode($this->job->body, true); Arr::set($payload, 'attempts', Arr::get($payload, 'attempts', 1) + 1); $this->iron->recreate(json_encode($payload), $this->getQueue(), $delay); }
/** * Set an array item to a given value using "dot" notation. * * If no key is given to the method, the entire array will be replaced. * * @param array $array * @param string $key * @param mixed $value * @return array */ function array_set(&$array, $key, $value) { return Arr::set($array, $key, $value); }