Skip to content

Instantly share code, notes, and snippets.

@jake-yeg
Created May 26, 2017 19:51
Show Gist options
  • Save jake-yeg/32f4ea7268552badb4cae4a04487c110 to your computer and use it in GitHub Desktop.
Save jake-yeg/32f4ea7268552badb4cae4a04487c110 to your computer and use it in GitHub Desktop.
<?php
class GetPut{
private $values = [];
public function getVal($key, $timestamp = null){
if($timestamp !== null && !empty($key)){
foreach($this->values[$key] as $value){
if($timestamp === $value['time']){
return $value['value'];
}else{
$this->err('timestamp doesn\'t match');
}
}
}else if($timestamp === null && !empty($key)){
$arrLen = count($this->values[$key]);
return $this->values[$key][$arrLen - 1]['value'];
}else{
$this->err('outside foreach');
}
}
public function putVal($key, $val){
if(empty($key) || empty($val)){
$this->err('value missing');
}
$timestamp = time();
if(isset($this->values[$key])){
array_push($this->values[$key], ['value' => $val, 'time' => $timestamp]);
}else{
$this->values[$key][] = ['value' => $val, 'time' => $timestamp];
}
echo 'Put value ' . $val . ' at ' . $timestamp . PHP_EOL;
return [$key, $val, $timestamp];
}
private function err($msg){
echo 'An error has occured ' . $msg . PHP_EOL;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment