1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\Seoable\Protocols; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use MadWeb\Seoable\Fields\Field; |
7
|
|
|
use MadWeb\Seoable\Contracts\Seoable; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
abstract class Protocol |
11
|
|
|
{ |
12
|
|
|
/** @var Model|\MadWeb\Seoable\Contracts\Seoable */ |
13
|
|
|
protected $model; |
14
|
|
|
|
15
|
|
|
/** @var array */ |
16
|
|
|
protected $modelSeoData; |
17
|
|
|
|
18
|
|
|
/** @var \Artesaos\SEOTools\SEOMeta */ |
19
|
|
|
protected $metaService = null; |
20
|
|
|
|
21
|
|
|
/** @var \Artesaos\SEOTools\OpenGraph */ |
22
|
|
|
protected $openGraphService = null; |
23
|
|
|
|
24
|
|
|
/** @var \Artesaos\SEOTools\TwitterCards */ |
25
|
|
|
protected $twitterCardService = null; |
26
|
|
|
|
27
|
|
|
/** @var \Artesaos\SEOTools\SEOTools */ |
28
|
|
|
protected $seoTools = null; |
29
|
|
|
|
30
|
|
|
protected $isRaw = false; |
31
|
|
|
|
32
|
|
|
protected $isStoredFieldsIgnores = false; |
33
|
|
|
|
34
|
|
|
/** @param Model|\MadWeb\Seoable\Contracts\Seoable $model */ |
35
|
|
|
public function __construct(Seoable $model) |
36
|
|
|
{ |
37
|
|
|
$this->model = $model; |
38
|
|
|
$this->modelSeoData = (array) $this->model->getSeoData(); |
39
|
|
|
|
40
|
|
|
$this->seoTools = resolve('seotools'); |
41
|
|
|
$this->metaService = resolve('seotools.metatags'); |
42
|
|
|
$this->openGraphService = resolve('seotools.opengraph'); |
43
|
|
|
$this->twitterCardService = resolve('seotools.twitter'); //TODO: resolve method ability |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array|string $value |
48
|
|
|
* @param \MadWeb\Seoable\Fields\Field|string $type |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
protected function parseValue($value, $type) |
52
|
|
|
{ |
53
|
|
|
$raw_field = $this->isStoredFieldsIgnores ? |
54
|
|
|
null : |
55
|
|
|
$this->getRawFields()[snake_case(class_basename($type))] ?? null; |
56
|
|
|
|
57
|
|
|
if (! $raw_field and ! $this->isRaw) { |
|
|
|
|
58
|
|
|
$type = $type instanceof Field ? $type : new $type($value, $this->model); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $raw_field ?? ($this->isRaw ? $value : $type->getValue()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __call($name, $arguments) |
65
|
|
|
{ |
66
|
|
|
if (ends_with($name, 'Raw')) { |
67
|
|
|
$this->isRaw = true; |
68
|
|
|
$this->{mb_strstr($name, 'Raw', true)}(...$arguments); |
69
|
|
|
$this->isRaw = false; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw new BadMethodCallException; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function twitter(): TwitterCard |
78
|
|
|
{ |
79
|
|
|
return new TwitterCard($this->model); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function opengraph(): OpenGraph |
83
|
|
|
{ |
84
|
|
|
return new OpenGraph($this->model); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function meta(): Meta |
88
|
|
|
{ |
89
|
|
|
return new Meta($this->model); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function ignoreStored() |
93
|
|
|
{ |
94
|
|
|
$this->isStoredFieldsIgnores = true; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
abstract protected function getRawFields(): array; |
100
|
|
|
} |
101
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.