aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Wiki/HelperTrait.php
blob: 74c518bf0a195732808e34901caadf724bb2832b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace JsonApi\Routes\Wiki;

use JsonApi\Errors\BadRequestException;
use JsonApi\Errors\RecordNotFoundException;

trait HelperTrait
{
    protected static function findWikiPage($wikiPageId)
    {
        if (is_numeric($wikiPageId)) {
            $page = \WikiPage::find($wikiPageId);
            if (!$page) {
                throw new RecordNotFoundException();
            } else {
                return $page;
            }
        }

        if (!preg_match('/^([^_]+)_(.+)$/', $wikiPageId, $matches)) {
            throw new BadRequestException();
        }

        if (!$wikiPage = \WikiPage::findOneBySQL('`range_id` = ? AND `name` = ?', [$matches[1], $matches[2]])) {
            throw new RecordNotFoundException();
        }

        return $wikiPage;
    }
}