1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
4
|
|
|
|
5
|
|
|
use Jclyons52\PHPQuery\Document; |
6
|
|
|
|
7
|
|
|
class PreviewBuilder |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PHPQuery document object that will be used to select elements |
12
|
|
|
* @var \Jclyons52\PHPQuery\Document |
13
|
|
|
*/ |
14
|
|
|
public $crawler; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Url object |
18
|
|
|
* @var Url |
19
|
|
|
*/ |
20
|
|
|
public $url; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* destructured array of url components from parse_url |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $urlComponents; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var HttpInterface |
30
|
|
|
*/ |
31
|
|
|
protected $http; |
32
|
|
|
|
33
|
|
|
|
34
|
81 |
|
public function __construct(HttpInterface $http) |
35
|
|
|
{ |
36
|
81 |
|
$this->http = $http; |
37
|
81 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Instantiate class with dependencies |
41
|
|
|
* @return static |
42
|
|
|
*/ |
43
|
3 |
|
public static function create() |
44
|
|
|
{ |
45
|
3 |
|
$http = new Http(); |
46
|
3 |
|
return new PreviewBuilder($http); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $url |
51
|
|
|
* @return Preview |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
45 |
|
public function fetch($url) |
55
|
|
|
{ |
56
|
45 |
|
$this->url = new Url($url); |
57
|
|
|
|
58
|
42 |
|
$body = $this->http->get($url); |
59
|
|
|
|
60
|
42 |
|
if ($body === false) { |
61
|
3 |
|
throw new \Exception('failed to load page'); |
62
|
|
|
} |
63
|
|
|
|
64
|
39 |
|
$document = new Document($body); |
65
|
|
|
|
66
|
39 |
|
$this->crawler = new Crawler($document); |
|
|
|
|
67
|
|
|
|
68
|
39 |
|
return $this->getPreview(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* returns an instance of Preview |
73
|
|
|
* @return Preview |
74
|
|
|
*/ |
75
|
39 |
|
public function getPreview() |
76
|
|
|
{ |
77
|
39 |
|
$title = $this->crawler->title(); |
|
|
|
|
78
|
|
|
|
79
|
39 |
|
$images = $this->images(); |
80
|
|
|
|
81
|
39 |
|
$description = $this->crawler->meta('description'); |
|
|
|
|
82
|
|
|
|
83
|
39 |
|
$meta = $this->crawler->meta(); |
|
|
|
|
84
|
|
|
|
85
|
39 |
|
$keywords = $this->crawler->metaKeywords(); |
|
|
|
|
86
|
|
|
|
87
|
39 |
|
if ($keywords !== []) { |
88
|
33 |
|
$meta['keywords'] = $keywords; |
89
|
22 |
|
} |
90
|
|
|
|
91
|
39 |
|
$media = new Media($this->http); |
92
|
|
|
|
93
|
39 |
|
return new Preview($media, [ |
94
|
39 |
|
'title' => $title, |
95
|
39 |
|
'images' => $images, |
96
|
39 |
|
'description' => $description, |
97
|
39 |
|
'url' => $this->url->original, |
98
|
39 |
|
'meta' => $meta, |
99
|
26 |
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
39 |
|
public function images() |
103
|
|
|
{ |
104
|
39 |
|
$urls = $this->crawler->images(); |
|
|
|
|
105
|
39 |
|
$result = []; |
106
|
39 |
|
foreach ($urls as $url) { |
107
|
36 |
|
$result[] = $this->url->formatRelativeToAbsolute($url); |
108
|
26 |
|
} |
109
|
39 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..