public static function set($key, $value = null) { static::$items = arraySet(static::$vars, $key, $value); }
/** * Get a subset of the items from the input data. * * @param array $keys * @return array */ public function only($keys) { $keys = Arrays::is($keys) ? $keys : func_get_args(); $results = []; $input = $this->all(); foreach ($keys as $key) { arraySet($results, $key, arrayGet($input, $key)); } return $results; }
function arraySet(&$array, $key, $value, $separator = '.') { if (strpos($key, $separator) !== false) { $keys = explode($separator, $key, 2); if (strlen(Arrays::first($keys)) && strlen($keys[1])) { if (!Arrays::exists(Arrays::first($keys), $array)) { if (Arrays::first($keys) === '0' && !empty($array)) { $array = array(Arrays::first($keys) => $array); } else { $array[Arrays::first($keys)] = array(); } } elseif (!Arrays::is($array[Arrays::first($keys)])) { throw new Exception("Cannot create sub-key for '{$keys[0]}' as key already exists."); } $array[Arrays::first($keys)] = arraySet($array[Arrays::first($keys)], $keys[1], $value); } else { throw new Exception("Invalid key '{$key}'"); } } else { $array[$key] = $value; } return $array; }
public function __set($key, $value) { $this->values = arraySet($this->values, $this->format($key), $value); return $this; }