Import source

This commit is contained in:
Steffen Lange 2019-07-15 22:27:48 +02:00
parent 50b489784d
commit 32b8c4cb56
34 changed files with 890 additions and 0 deletions

69
Database.php Normal file
View file

@ -0,0 +1,69 @@
<?php
class Database {
private $time = 0;
private $file = '';
private $data = array();
function __construct(string $file) {
$this->time = time();
$this->file = $file;
}
function sort() : bool {
return usort($this->data, array($this, 'cmp'));
}
function load() : bool {
if ($json = file_get_contents($this->file)) {
if ($arr = json_decode($json, true)) {
foreach ($arr as $obj) {
array_push($this->data, new PatchObject($obj));
}
return true;
}
}
return false;
}
function save() : bool {
$arr = array();
foreach ($this->data as $obj) {
array_push($arr, $obj->toArray());
}
if ($json = json_encode($arr, JSON_PRETTY_PRINT)) {
if (file_put_contents($this->file, $json)) {
return true;
}
}
return false;
}
function time() : int {
return $this->time;
}
function count() : int {
return count($this->data);
}
function get(int $n) : PatchObject {
return $this->data[$n];
}
function find(string $id) : PatchObject {
foreach ($this->data as $obj) {
if (strcmp($obj->GetId(), $id) == 0) {
return $obj;
}
}
return new PatchObject();
}
function addOrUpdate(PatchObject $patch) {
$patch->SetTimestamp($this->time);
foreach ($this->data as &$obj) {
if (strcmp($obj->GetId(), $patch->GetId()) == 0) {
$obj = $patch;
return;
}
}
array_push($this->data, $patch);
}
private function cmp(PatchObject $a, PatchObject $b) : int {
return strcasecmp($a->getProduct(), $b->getProduct());
}
}
?>