/** * 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 the validaton rules within the validation context. * * @return array */ protected function getRulesInContext() { if (!$this->hasContext()) { return static::$rules; } $rulesInContext = Arr::get(static::$rules, self::DEFAULT_KEY, array()); if (!Arr::get(static::$rules, $this->context)) { throw new \Exception(sprintf("'%s' does not contain the validation context '%s'", get_called_class(), $this->context)); } $rulesInContext = array_merge($rulesInContext, static::$rules[$this->context]); return $rulesInContext; }
/** * Transforms the input array into the output array based on the rules * stated in the configuration file. * * @return array */ public function transform($root = '') { $output = []; $array = Arr::dot($this->feed_data); $rules = Arr::dot($this->rules); $all_rules = []; foreach ($rules as $key => $value) { if (strpos($key, '*')) { $new_keys = Arr::expandKeys($key, $this->expand_size); $new_values = Arr::expandKeys($value, $this->expand_size); $new_rules = array_combine($new_keys, $new_values); foreach ($new_rules as $new_key => $new_rule) { if (array_key_exists($new_rule, $array)) { $all_rules[$new_key] = $new_rule; } } } else { $all_rules[$key] = $value; } } foreach ($all_rules as $k => $v) { Arr::set($output, $k, Arr::get($array, $v)); } return $output; }
public function testForget() { $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, null); $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array); $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, []); $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array); $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk'); $this->assertEquals(['products' => []], $array); $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.desk.price'); $this->assertEquals(['products' => ['desk' => []]], $array); $array = ['products' => ['desk' => ['price' => 100]]]; Arr::forget($array, 'products.final.price'); $this->assertEquals(['products' => ['desk' => ['price' => 100]]], $array); $array = ['shop' => ['cart' => [150 => 0]]]; Arr::forget($array, 'shop.final.cart'); $this->assertEquals(['shop' => ['cart' => [150 => 0]]], $array); $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]]; Arr::forget($array, 'products.desk.price.taxes'); $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50]]]], $array); $array = ['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]]; Arr::forget($array, 'products.desk.final.taxes'); $this->assertEquals(['products' => ['desk' => ['price' => ['original' => 50, 'taxes' => 60]]]], $array); $array = ['products' => ['desk' => ['price' => 50], null => 'something']]; Arr::forget($array, ['products.amount.all', 'products.desk.price']); $this->assertEquals(['products' => ['desk' => [], null => 'something']], $array); // Only works on first level keys $array = ['*****@*****.**' => 'Joe', '*****@*****.**' => 'Jane']; Arr::forget($array, '*****@*****.**'); $this->assertEquals(['*****@*****.**' => 'Jane'], $array); // Does not work for nested keys $array = ['emails' => ['*****@*****.**' => ['name' => 'Joe'], 'jane@localhost' => ['name' => 'Jane']]]; Arr::forget($array, ['*****@*****.**', 'emails.jane@localhost']); $this->assertEquals(['emails' => ['*****@*****.**' => ['name' => 'Joe']]], $array); }
/** * Get an item from an array or object using "dot" notation. * * @param mixed $target * @param string|array $key * @param mixed $default * @return mixed */ function data_get($target, $key, $default = null) { if (is_null($key)) { return $target; } $key = is_array($key) ? $key : explode('.', $key); while (($segment = array_shift($key)) !== null) { if ($segment === '*') { if ($target instanceof Collection) { $target = $target->all(); } elseif (!is_array($target)) { return value($default); } $result = Arr::pluck($target, $key); return in_array('*', $key) ? Arr::collapse($result) : $result; } if (Arr::accessible($target) && Arr::exists($target, $segment)) { $target = $target[$segment]; } elseif (is_object($target) && isset($target->{$segment})) { $target = $target->{$segment}; } else { return value($default); } } return $target; }