1234567891011121314151617181920212223242526272829303132333435 |
- <?php
- use Symfony\Component\HttpFoundation\Response;
- class Model {
- protected $data;
- protected $path;
- public function __construct(string $path) {
- $this->path = $path;
- }
- public function load() {
- $raw = file_get_contents($this->path);
- $this->data = json_decode($raw, true);
- return $this->data;
- }
- public function save(array $doc) {
- if (empty($doc['id'])) {
- $id = (int) max(array_keys($this->data)) + 1;
- $doc['id'] = $id;
- $created = true;
- }
- else {
- $id = (int) $doc['id'];
- $created = false;
- }
- $this->data[$id] = $doc;
- $raw = json_encode($this->data);
- file_put_contents($this->path, $raw);
- return $created;
- }
- }
|