forked from caneara/tipsea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeta.php
117 lines (105 loc) · 3.08 KB
/
Meta.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Responses;
use App\Models\Tip;
use App\Models\User;
use App\Types\Model;
use App\Storage\Image;
use Illuminate\Support\Str;
class Meta
{
/**
* Generate the Open Graph and Twitter Card meta tags for the given model.
*
*/
public static function create(Model $model) : array
{
return [
'alt' => static::getAltImageText($model),
'card' => static::getCard($model),
'description' => static::getDescription($model),
'image' => static::getImage($model),
'title' => static::getTitle($model),
'type' => static::getType($model),
'url' => static::getUrl($model),
];
}
/**
* Retrieve the alternate image text to use for the meta tag.
*
*/
protected static function getAltImageText(Model $model) : string
{
return match (get_class($model)) {
Tip::class => Str::of($model->teaser)->replace('"', "'")->limit(400)->toString(),
User::class => '',
};
}
/**
* Retrieve the card type to use for the meta tag.
*
*/
protected static function getCard(Model $model) : string
{
return match (get_class($model)) {
Tip::class => 'summary_large_image',
User::class => 'summary',
};
}
/**
* Retrieve the description to use for the meta tag.
*
*/
protected static function getDescription(Model $model) : string
{
$value = match (get_class($model)) {
Tip::class => $model->summary,
User::class => "{$model->name} is on TipSea. Write, share and discover code tips.",
};
return Str::limit($value, 197);
}
/**
* Retrieve the image to use for the meta tag.
*
*/
protected static function getImage(Model $model) : string
{
return match (get_class($model)) {
Tip::class => $model->card ? Image::url($model->card) : asset('img/tip.png'),
User::class => $model->avatar ? Image::url($model->avatar) : asset('img/user.png'),
};
}
/**
* Retrieve the title to use for the meta tag.
*
*/
protected static function getTitle(Model $model) : string
{
$value = match (get_class($model)) {
Tip::class => $model->title,
User::class => $model->name,
};
return Str::limit($value, 67);
}
/**
* Retrieve the type to use for the meta tag.
*
*/
protected static function getType(Model $model) : string
{
return match (get_class($model)) {
Tip::class => 'article',
User::class => 'profile',
};
}
/**
* Retrieve the url to use for the meta tag.
*
*/
protected static function getUrl(Model $model) : string
{
return match (get_class($model)) {
Tip::class => route('tips.show', ['tip' => $model, 'slug' => $model->slug]),
User::class => route('profile', ['user' => $model->handle]),
};
}
}