1 | <?php |
||
11 | abstract class Protocol |
||
12 | { |
||
13 | /** @var Model|\MadWeb\Seoable\Contracts\Seoable */ |
||
14 | protected $model; |
||
15 | |||
16 | /** @var array */ |
||
17 | protected $modelSeoData; |
||
18 | |||
19 | /** @var \Artesaos\SEOTools\SEOMeta */ |
||
20 | protected $metaService = null; |
||
21 | |||
22 | /** @var \Artesaos\SEOTools\OpenGraph */ |
||
23 | protected $openGraphService = null; |
||
24 | |||
25 | /** @var \Artesaos\SEOTools\TwitterCards */ |
||
26 | protected $twitterCardService = null; |
||
27 | |||
28 | /** @var \Artesaos\SEOTools\SEOTools */ |
||
29 | protected $seoTools = null; |
||
30 | |||
31 | protected $isRaw = false; |
||
32 | |||
33 | protected $isStoredFieldsIgnores = false; |
||
34 | |||
35 | /** @param Model|\MadWeb\Seoable\Contracts\Seoable $model */ |
||
36 | 21 | public function __construct(Seoable $model) |
|
37 | { |
||
38 | 21 | $this->model = $model; |
|
39 | 21 | $this->modelSeoData = (array) $this->model->getSeoData(); |
|
40 | |||
41 | 21 | $this->seoTools = resolve('seotools'); |
|
42 | 21 | $this->metaService = resolve('seotools.metatags'); |
|
43 | 21 | $this->openGraphService = resolve('seotools.opengraph'); |
|
44 | 21 | $this->twitterCardService = resolve('seotools.twitter'); //TODO: resolve method ability |
|
45 | 21 | } |
|
46 | |||
47 | /** |
||
48 | * @param array|string $value |
||
49 | * @param \MadWeb\Seoable\Fields\Field|string $type |
||
50 | * @return mixed |
||
51 | */ |
||
52 | 18 | protected function parseValue($value, $type) |
|
53 | { |
||
54 | 18 | $raw_field = $this->isStoredFieldsIgnores ? |
|
55 | 3 | null : |
|
56 | 18 | $this->getRawFields()[Str::snake(class_basename($type))] ?? null; |
|
57 | |||
58 | 18 | if (! $raw_field and ! $this->isRaw) { |
|
59 | 12 | $type = $type instanceof Field ? $type : new $type($value, $this->model); |
|
60 | } |
||
61 | |||
62 | 18 | return $raw_field ?? ($this->isRaw ? $value : $type->getValue()); |
|
63 | } |
||
64 | |||
65 | 6 | public function __call($name, $arguments) |
|
66 | { |
||
67 | 6 | if (Str::endsWith($name, 'Raw')) { |
|
68 | 3 | $this->isRaw = true; |
|
69 | 3 | $this->{mb_strstr($name, 'Raw', true)}(...$arguments); |
|
70 | 3 | $this->isRaw = false; |
|
71 | |||
72 | 3 | return $this; |
|
73 | } |
||
74 | |||
75 | 3 | throw new BadMethodCallException; |
|
76 | } |
||
77 | |||
78 | 3 | public function twitter(): TwitterCard |
|
79 | { |
||
80 | 3 | return new TwitterCard($this->model); |
|
81 | } |
||
82 | |||
83 | 3 | public function opengraph(): OpenGraph |
|
84 | { |
||
85 | 3 | return new OpenGraph($this->model); |
|
86 | } |
||
87 | |||
88 | public function meta(): Meta |
||
89 | { |
||
90 | return new Meta($this->model); |
||
91 | } |
||
92 | |||
93 | 3 | public function ignoreStored() |
|
94 | { |
||
95 | 3 | $this->isStoredFieldsIgnores = true; |
|
96 | |||
97 | 3 | return $this; |
|
98 | } |
||
99 | |||
100 | abstract protected function getRawFields(): array; |
||
101 | } |
||
102 |