Skip to content

Instantly share code, notes, and snippets.

@saeedvir
Last active July 20, 2020 09:01
Show Gist options
  • Save saeedvir/330e894045baa8e6a37ba34b8ad7c407 to your computer and use it in GitHub Desktop.
Save saeedvir/330e894045baa8e6a37ba34b8ad7c407 to your computer and use it in GitHub Desktop.
Generate data in key/value structure / For ex: for admin panel dashboard
<?php
/**
*
* @author Saeedvir [
* 'email' => 'saeed.es91@gmail.com',
* 'telegram' => 'https://t.me/PhpWebDeveloper'
* ]
* @usage :
$dataGenerator = new DataStructureGenerator('data', true,true);
$dataGenerator->setData('users', 8);
$dataGenerator->setData('payments', 8000);
# laravel ex:
$dataGenerator->setDataFromModel(new \App\User, function ($model) {
return $model->select('id')->count();
}, 'users', 8000);
$dataGenerator->setKeyFunctions(
[
'payments' => function ($value) {
return number_format($value);
},
]
);
print_r($dataGenerator->getJsonData(true);
print_r($dataGenerator->getJsonData(false, ['users']));
*/
class DataStructureGenerator
{
protected $data_response = [];
protected $pkn;
protected $enable_hash = false;
protected $enable_timestamps = false;
protected $key_functions = [];
public function __construct($pkn = 'data', $enable_hash = false, $enable_timestamps = false)
{
if (is_string($pkn)) {
$this->pkn = 'data';
}
$this->enable_hash = $enable_hash;
$this->enable_timestamps = $enable_timestamps;
$this->data_response[$this->pkn] = [];
}
public function setData($key, $value)
{
$this->data_response[$this->pkn][$key] = $value;
}
public function setDataFromModel($model, $func = null, $key, $value)
{
if (!$func) {
return null;
}
$this->data_response[$this->pkn][$key] = $func($model);
}
public function getData($key)
{
if (isset($this->data_response[$this->pkn][$key])) {
return $this->data_response[$this->pkn][$key];
}
return null;
}
public function removeData($key)
{
if (isset($this->data_response[$this->pkn][$key])) {
unset($this->data_response[$this->pkn][$key]);
return true;
}
return false;
}
public function emptyData()
{
$this->data_response[$this->pkn] = [];
}
public function setKeyFunctions($key_functions = [])
{
if (is_array($key_functions)) {
$this->key_functions = $key_functions;
} else {
$this->key_functions = [];
}
}
public function getJsonData($return_json = true, $keys = [])
{
if ($this->enable_hash) {
$this->data_response['_hash'] = $this->generateArrayHash('serialize');
}
if ($this->enable_timestamps) {
$this->data_response['_timestaps'] = time();
}
$this->data_response[$this->pkn] = $this->getDataWithKeys($keys);
$this->walkKeyFunctions();
if ($return_json) {
return json_encode($this->data_response);
} else {
return $this->data_response;
}
}
protected function getDataWithKeys($keys = [])
{
if (is_array($keys) && count($keys) > 0) {
return array_intersect_key($this->data_response[$this->pkn], array_flip(array_values($keys)));
} else {
return $this->data_response[$this->pkn];
}
}
protected function generateArrayHash($mode = 'serialize')
{
if ($mode === 'json_encode') {
//use json_encode
return md5(json_encode($this->data_response[$this->pkn]));
} else {
//use serialize
return md5(serialize($this->data_response[$this->pkn]));
}
}
protected function walkKeyFunctions()
{
if (count($this->key_functions) > 0) {
foreach ($this->data_response[$this->pkn] as $key => $value) {
if (isset($this->key_functions[$key])) {
if (is_callable($this->key_functions[$key])) {
$this->data_response[$this->pkn][$key] = $this->key_functions[$key]($value);
} else {
$this->data_response[$this->pkn][$key] = $this->key_functions[$key];
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment