|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Jclyons52\PHPQuery\Document; |
|
7
|
|
|
use League\Plates\Engine; |
|
8
|
|
|
|
|
9
|
|
|
class Preview |
|
10
|
|
|
{ |
|
11
|
|
|
protected $result; |
|
12
|
|
|
|
|
13
|
|
|
protected $client; |
|
14
|
|
|
|
|
15
|
|
|
public $document; |
|
16
|
|
|
|
|
17
|
|
|
public $url; |
|
18
|
|
|
|
|
19
|
|
|
protected $urlComponents; |
|
20
|
|
|
|
|
21
|
27 |
|
public function __construct(Client $client) |
|
22
|
|
|
{ |
|
23
|
27 |
|
$this->client = $client; |
|
24
|
27 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $url |
|
28
|
|
|
* @return Document |
|
29
|
|
|
*/ |
|
30
|
27 |
|
public function fetch($url) |
|
31
|
|
|
{ |
|
32
|
27 |
|
$this->url = $url; |
|
33
|
|
|
|
|
34
|
27 |
|
$this->urlComponents = parse_url($url); |
|
35
|
|
|
|
|
36
|
27 |
|
$this->result = $this->client->request('GET', $url); |
|
37
|
|
|
|
|
38
|
24 |
|
$body = $this->result->getBody()->getContents(); |
|
39
|
|
|
|
|
40
|
24 |
|
$this->document = new Document($body); |
|
41
|
|
|
|
|
42
|
24 |
|
return $this->document; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function title() |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->document->querySelector('title')->text(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return mixed |
|
55
|
|
|
*/ |
|
56
|
6 |
|
public function metaDescription() |
|
57
|
3 |
|
{ |
|
58
|
6 |
|
return $this->document->querySelector('meta[name="description"]')->attr('content'); |
|
59
|
3 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return mixed |
|
63
|
|
|
*/ |
|
64
|
3 |
|
public function metaKeywords() |
|
65
|
|
|
{ |
|
66
|
3 |
|
$keywordString = $this->document->querySelector('meta[name="keywords"]')->attr('content'); |
|
67
|
|
|
|
|
68
|
3 |
|
$keywords = explode(',', $keywordString); |
|
69
|
|
|
|
|
70
|
3 |
|
return array_map(function ($word) { |
|
71
|
3 |
|
return trim($word); |
|
72
|
|
|
|
|
73
|
3 |
|
}, $keywords); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
6 |
|
public function metaTitle() |
|
80
|
|
|
{ |
|
81
|
6 |
|
return $this->document->querySelector('meta[name="title"]')->attr('content'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function meta() |
|
88
|
|
|
{ |
|
89
|
3 |
|
$metaTags = $this->document->querySelectorAll('meta'); |
|
90
|
3 |
|
$values = []; |
|
91
|
3 |
|
foreach ($metaTags as $meta) { |
|
92
|
3 |
|
$values[$meta->attr('name')] = $meta->attr('content'); |
|
93
|
3 |
|
} |
|
94
|
3 |
|
return $values; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function images() |
|
101
|
|
|
{ |
|
102
|
6 |
|
$images = $this->document->querySelectorAll('img'); |
|
103
|
6 |
|
$urls = $images->attr('src'); |
|
104
|
6 |
|
$result = []; |
|
105
|
6 |
|
foreach ($urls as $url) { |
|
106
|
6 |
|
$result[] = $this->formatUrl($url); |
|
107
|
6 |
|
} |
|
108
|
6 |
|
return $result; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
public function render($type = 'card') |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
3 |
|
$title = $this->metaTitle(); |
|
114
|
|
|
|
|
115
|
3 |
|
$image = $this->images()[0]; |
|
116
|
|
|
|
|
117
|
3 |
|
$description = $this->metaDescription(); |
|
118
|
|
|
|
|
119
|
3 |
|
$templates = new Engine(__DIR__ . '/Templates'); |
|
120
|
|
|
|
|
121
|
3 |
|
return $templates->render('card', [ 'title' => $title, 'image' => $image, 'body' => $description, 'url' => $this->url ]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $url |
|
126
|
|
|
* @return array |
|
127
|
|
|
*/ |
|
128
|
6 |
|
private function formatUrl($url) |
|
129
|
|
|
{ |
|
130
|
6 |
|
$path = array_key_exists('path', $this->urlComponents) ? $this->urlComponents['path'] : ''; |
|
131
|
|
|
|
|
132
|
6 |
|
if (filter_var($url, FILTER_VALIDATE_URL) !== false) { |
|
133
|
6 |
|
return $url; |
|
134
|
|
|
} |
|
135
|
6 |
|
if (substr($url, 0, 1) === '/') { |
|
136
|
6 |
|
return 'http://' . $this->urlComponents['host'] . $url; |
|
137
|
|
|
} |
|
138
|
6 |
|
return 'http://' . $this->urlComponents['host'] .$path . '/' . $url; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.