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