function __construct($pth) { if (!is_example_path($pth) or !is_example_exists($pth)) { throw new Exception("Example {$pth} not exists"); } $this->pth = $pth; $this->file_id = last(explode('/', $pth)); $category_path = implode('/', but_last(explode('/', $pth))); $this->id = find_position($this->file_id, $category_path); $this->category = Category::get($category_path); }
}); /** * Этот путь подходит ли для примера? */ def('is_example_path', function ($url) { if (!$url) { return false; } $t = explode('/', $url); if (!preg_match('/^[0-9]+$/', last($t)) && !preg_match('/^[a-z0-9_.-]+\\.(md|html)$/', last($t))) { return false; } if (!last($t)) { return false; } if (!is_category_path(implode('/', but_last($t)))) { return false; } return true; }); /** * Существует ли эта категория в папке с данными? */ def('is_category_exists', function ($path) { if (!is_category_path($path)) { throw new Exception('Path is not valid: ' . $path); } $path = data_directory() . '/' . $path; if (file_exists($path) && is_dir($path)) { return true; }