1<?php
2
3
4namespace ComboStrap\Meta\Api;
5
6
7use ComboStrap\ExceptionRuntime;
8use ComboStrap\Meta\Store\MetadataDokuWikiStore;
9use ComboStrap\MetadataDokuWikiArrayStore;
10use ComboStrap\ResourceCombo;
11
12abstract class MetadataStoreAbs implements MetadataStore
13{
14
15    const CANONICAL = "store";
16    private $page;
17
18
19    public function isDokuWikiStore(): bool
20    {
21        return $this instanceof MetadataDokuWikiStore || $this instanceof MetadataDokuWikiArrayStore;
22    }
23
24    /**
25     * MetadataFormStore constructor.
26     * @param ResourceCombo $page
27     */
28    public function __construct(ResourceCombo $page)
29    {
30        $this->page = $page;
31    }
32
33    protected function checkResource(ResourceCombo $requestedResource)
34    {
35        if ($this->page->getPathObject()->toAbsoluteId() !== $requestedResource->getPathObject()->toAbsoluteId()) {
36            throw new ExceptionRuntime("The page ($requestedResource) is unknown. We got data for the page ($this->page)", $this->getCanonical());
37        }
38    }
39
40    /**
41     * @param MetadataStore|string $readStore
42     * @param $resource
43     * @return MetadataStore
44     */
45    public static function toMetadataStore($readStore, $resource): MetadataStore
46    {
47        if ($readStore instanceof MetadataStore) {
48            return $readStore;
49        }
50        if (!is_string($readStore)) {
51            throw new ExceptionRuntime("The class value is not a string", MetadataStoreAbs::CANONICAL);
52        }
53        if (!is_subclass_of($readStore, MetadataStore::class)) {
54            throw new ExceptionRuntime("The value ($readStore) is not a subclass of a store.");
55        }
56        if ($resource === null) {
57            throw new ExceptionRuntime("The resource is null. You can't implement a store without a resource.");
58        }
59        return $readStore::getOrCreateFromResource($resource);
60
61    }
62
63    public function getResource(): ResourceCombo
64    {
65        return $this->page;
66    }
67
68    public function getCanonical(): string
69    {
70        return self::CANONICAL;
71    }
72
73    public function __toString()
74    {
75        return get_class($this);
76    }
77
78
79}
80