Lectures/includes/stories.php

26 lines
776 B
PHP

<?php
class Stories {
private static $storiesDir = __DIR__ . '/../stories/';
public static function getAll() {
$stories = [];
foreach (glob(self::$storiesDir . '*.json') as $file) {
$story = json_decode(file_get_contents($file), true);
$stories[] = $story;
}
return $stories;
}
public static function get($id) {
$file = self::$storiesDir . $id . '.json';
if (!file_exists($file)) {
return null;
}
return json_decode(file_get_contents($file), true);
}
public static function save($story) {
$file = self::$storiesDir . $story['id'] . '.json';
return file_put_contents($file, json_encode($story, JSON_PRETTY_PRINT));
}
}