/** * Get an environment variable or return the default if it is not defined. * * This avoid any post-processing, such as automatic casting. * * @param string $key * @param null|mixed|callable $default * * @return mixed|string */ public static function getRaw($key, $default = null) { $env = ArrayMap::of($_ENV); $server = ArrayMap::of($_SERVER); if ($env->member($key)) { return Maybe::fromJust($env->lookup($key)); } elseif ($server->member($key)) { return Maybe::fromJust($server->lookup($key)); } $value = getenv($key); if ($value === false) { return Std::thunk($default); } return $value; }
/** * Get the resulting value of an attempt to traverse a key path. * * Each key in the path is separated with a dot. * * For example, the following snippet should return `true`: * ```php * Arr::dotGet([ * 'hello' => [ * 'world' => true, * ], * ], 'hello.world'); * ``` * * Additionally, a default value may be provided, which will be returned if * the path does not yield to a value. * * @param array $array * @param string $key * @param null|mixed $default * * @return mixed */ public static function dotGet(array $array, $key, $default = null) { if (is_null($key)) { return $array; } if (isset($array[$key])) { return $array[$key]; } foreach (explode('.', $key) as $segment) { if (!is_array($array) || !array_key_exists($segment, $array)) { return Std::thunk($default); } $array = $array[$segment]; } return $array; }
/** * Generate the model. * * @throws LackOfCoffeeException * @return Model */ public function make() { // Make model instance $model = $this->getModelInstance(true); $filling = TransformPipeline::define()->inline(function ($input) { return array_merge($input, $this->overrides); })->inline(function ($input) { return Std::map(function ($value) { return Std::thunk($value); }, $input); })->run($this->getMapping()); Std::each(function ($value, $key) use(&$model) { $model->{$key} = $value; }, $filling); Std::each(function ($relation, $name) use(&$model) { if (!$this->isBelongsTo($name)) { return; } $model->{$name}()->associate($this->generateRelation($name)); }, $this->relations); $model->save(); Std::each(function ($relation, $name) use(&$model) { if ($this->isBelongsTo($name)) { return; } $related = $this->generateRelation($name); if (is_array($related)) { $model->{$name}()->saveMany($related); } else { $model->{$name}()->save($related); } }, $this->relations); return $model; }