Skip to content

Instantly share code, notes, and snippets.

@filljoyner
Created April 1, 2019 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filljoyner/c6d6d3ce3f982b01d9b28f207eec1a99 to your computer and use it in GitHub Desktop.
Save filljoyner/c6d6d3ce3f982b01d9b28f207eec1a99 to your computer and use it in GitHub Desktop.
A simple bag of info to prevent making the same calls over and over again
<?php
namespace App\Abilities\Components;
class DataBag
{
protected $data;
public function __construct($data=[])
{
$this->data = $data;
}
public function get($key, $default)
{
if(empty($this->data[$key])) {
$this->add($key, $default);
}
return $this->data[$key];
}
public function add($key, $data)
{
$this->data[$key] = $data;
if (is_callable($data)) {
$this->data[$key] = $data();
}
}
}
<?php
require('DataBag.php');
$bag = new DataBag;
$ip = '123.456.789.10';
$ip_data = $bag->get($ip, function() use ($ip) {
return IP::lookup($ip);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment