Created
April 1, 2019 21:50
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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