-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert configuration to Value Object
Mostly to enforce type of each parameter of a config. Also, enforce type for the input configuration using `setAllowedTypes`.
- Loading branch information
Showing
3 changed files
with
147 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?php | ||
|
||
namespace Graby\Extractor; | ||
|
||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Configuration for HttpClient as a Value Object. | ||
*/ | ||
class HttpClientConfig | ||
{ | ||
private string $ua_browser; | ||
private string $default_referer; | ||
/** @var array<array<string, string>> */ | ||
private array $rewrite_url; | ||
/** @var array<string> */ | ||
private array $header_only_types; | ||
/** @var array<string> */ | ||
private array $header_only_clues; | ||
private array $user_agents; | ||
/** @var array<string> */ | ||
private array $ajax_triggers; | ||
private int $max_redirect; | ||
|
||
public function __construct(array $config) | ||
{ | ||
$resolver = new OptionsResolver(); | ||
$resolver->setDefaults([ | ||
'ua_browser' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.92 Safari/535.2', | ||
'default_referer' => 'http://www.google.co.uk/url?sa=t&source=web&cd=1', | ||
'rewrite_url' => [ | ||
'docs.google.com' => ['/Doc?' => '/View?'], | ||
'tnr.com' => ['tnr.com/article/' => 'tnr.com/print/article/'], | ||
'.m.wikipedia.org' => ['.m.wikipedia.org' => '.wikipedia.org'], | ||
'm.vanityfair.com' => ['m.vanityfair.com' => 'www.vanityfair.com'], | ||
], | ||
// Prevent certain file/mime types | ||
// HTTP responses which match these content types will | ||
// be returned without body. | ||
'header_only_types' => [ | ||
'image', | ||
'audio', | ||
'video', | ||
], | ||
// URLs ending with one of these extensions will | ||
// prompt client to send a HEAD request first | ||
// to see if returned content type matches $headerOnlyTypes. | ||
'header_only_clues' => ['mp3', 'zip', 'exe', 'gif', 'gzip', 'gz', 'jpeg', 'jpg', 'mpg', 'mpeg', 'png', 'ppt', 'mov'], | ||
// User Agent strings - mapping domain names | ||
'user_agents' => [], | ||
// AJAX triggers to search for. | ||
// for AJAX sites, e.g. Blogger with its dynamic views templates. | ||
'ajax_triggers' => [ | ||
"<meta name='fragment' content='!'", | ||
'<meta name="fragment" content="!"', | ||
"<meta content='!' name='fragment'", | ||
'<meta content="!" name="fragment"', | ||
], | ||
// number of redirection allowed until we assume request won't be complete | ||
'max_redirect' => 10, | ||
]); | ||
|
||
$resolver->setAllowedTypes('ua_browser', 'string'); | ||
$resolver->setAllowedTypes('default_referer', 'string'); | ||
$resolver->setAllowedTypes('rewrite_url', 'array'); | ||
$resolver->setAllowedTypes('header_only_types', 'string[]'); | ||
$resolver->setAllowedTypes('header_only_clues', 'string[]'); | ||
$resolver->setAllowedTypes('user_agents', 'array'); | ||
$resolver->setAllowedTypes('ajax_triggers', 'string[]'); | ||
$resolver->setAllowedTypes('max_redirect', 'int'); | ||
|
||
$config = $resolver->resolve($config); | ||
|
||
foreach ($config as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
|
||
public function getUaBrowser(): string | ||
{ | ||
return $this->ua_browser; | ||
} | ||
|
||
public function getDefaultReferer(): string | ||
{ | ||
return $this->default_referer; | ||
} | ||
|
||
/** | ||
* @return array<array<string, string>> | ||
*/ | ||
public function getRewriteUrl(): array | ||
{ | ||
return $this->rewrite_url; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function getHeaderOnlyTypes(): array | ||
{ | ||
return $this->header_only_types; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function getHeaderOnlyClues(): array | ||
{ | ||
return $this->header_only_clues; | ||
} | ||
|
||
public function getUserAgents(): array | ||
{ | ||
return $this->user_agents; | ||
} | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
public function getAjaxTriggers(): array | ||
{ | ||
return $this->ajax_triggers; | ||
} | ||
|
||
public function getMaxRedirect(): int | ||
{ | ||
return $this->max_redirect; | ||
} | ||
} |