Skip to content

Commit

Permalink
Improve array config validation
Browse files Browse the repository at this point in the history
  • Loading branch information
j0k3r committed Jan 27, 2022
1 parent 84a3cec commit 0c7cd93
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 10 deletions.
12 changes: 4 additions & 8 deletions src/Extractor/ContentExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,16 +1044,12 @@ private function getReadability(string $html, string $url, string $parser, bool
{
$readability = new Readability($html, $url, $parser, $enableTidy);

if (isset($this->config->getReadability()['pre_filters']) && \is_array($this->config->getReadability()['pre_filters'])) {
foreach ($this->config->getReadability()['pre_filters'] as $filter => $replacer) {
$readability->addPreFilter($filter, $replacer);
}
foreach ($this->config->getReadability()['pre_filters'] as $filter => $replacer) {
$readability->addPreFilter($filter, $replacer);
}

if (isset($this->config->getReadability()['post_filters']) && \is_array($this->config->getReadability()['post_filters'])) {
foreach ($this->config->getReadability()['post_filters'] as $filter => $replacer) {
$readability->addPostFilter($filter, $replacer);
}
foreach ($this->config->getReadability()['post_filters'] as $filter => $replacer) {
$readability->addPostFilter($filter, $replacer);
}

return $readability;
Expand Down
16 changes: 16 additions & 0 deletions src/Extractor/ContentExtractorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Graby\Extractor;

use Graby\OptionsResolver\ArrayStringOptionsTrait;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Configuration for ContentExtractor as a Value Object.
*/
class ContentExtractorConfig
{
use ArrayStringOptionsTrait;

private string $default_parser;
/** @var array<string> */
private array $allowed_parsers = ['libxml', 'html5lib'];
Expand Down Expand Up @@ -63,6 +67,18 @@ public function __construct(array $config)
$readabilityResolver->setAllowedTypes('post_filters', 'array');
});

$resolver->setNormalizer('readability', function (Options $options, $value) {
$this->validateArray($value, 'readability[pre_filters]', 'pre_filters');
$this->validateArray($value, 'readability[post_filters]', 'post_filters');

return $value;
});
$resolver->setNormalizer('fingerprints', function (Options $options, $value) {
$this->validateArray($value, 'fingerprints');

return $value;
});

$config = $resolver->resolve($config);

foreach ($config as $key => $value) {
Expand Down
2 changes: 1 addition & 1 deletion src/Extractor/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private function cleanupUrl(string $url): string
{
// rewrite part of urls to something more readable
foreach ($this->config->getRewriteUrl() as $find => $action) {
if (false !== strpos($url, (string) $find) && \is_array($action)) {
if (false !== strpos($url, (string) $find)) {
$url = strtr($url, $action);
}
}
Expand Down
22 changes: 22 additions & 0 deletions src/Extractor/HttpClientConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace Graby\Extractor;

use Graby\OptionsResolver\ArrayStringOptionsTrait;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Configuration for HttpClient as a Value Object.
*/
class HttpClientConfig
{
use ArrayStringOptionsTrait;

private string $ua_browser;
private string $default_referer;
/** @var array<array<string, string>> */
Expand Down Expand Up @@ -69,6 +74,23 @@ public function __construct(array $config)
$resolver->setAllowedTypes('ajax_triggers', 'string[]');
$resolver->setAllowedTypes('max_redirect', 'int');

$resolver->setNormalizer('user_agents', function (Options $options, $value) {
$this->validateArray($value, 'user_agents');

return $value;
});
$resolver->setNormalizer('rewrite_url', function (Options $options, $value) {
foreach ($value as $url => $action) {
if (!\is_string($url)) {
throw new InvalidOptionsException(sprintf('The option "rewrite_url" with key "%s" is expected to be of type "string", but is of type "%s".', $url, get_debug_type($url)));
}

$this->validateArray($action, 'rewrite_url[' . $url . ']');
}

return $value;
});

$config = $resolver->resolve($config);

foreach ($config as $key => $value) {
Expand Down
34 changes: 34 additions & 0 deletions src/OptionsResolver/ArrayStringOptionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Graby\OptionsResolver;

use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* To be used inside a `OptionsResolver->setNormalizer` to enforce that a given value is an array on string, string.
*/
trait ArrayStringOptionsTrait
{
public function validateArray(array $array, string $option, string $key = null): void
{
if (null === $key) {
foreach ($array as $arrayKey => $arrayValue) {
if (!\is_string($arrayKey)) {
throw new InvalidOptionsException(sprintf('The option "%s" with key "%s" is expected to be of type "string", but is of type "%s".', $option, $arrayKey, get_debug_type($arrayKey)));
}
if (!\is_string($arrayValue)) {
throw new InvalidOptionsException(sprintf('The option "%s" with value "%s" is expected to be of type "string", but is of type "%s".', $option, $arrayValue, get_debug_type($arrayValue)));
}
}
} elseif (!empty($array[$key])) {
foreach ($array[$key] as $arrayKey => $arrayValue) {
if (!\is_string($arrayKey)) {
throw new InvalidOptionsException(sprintf('The option "%s" with key "%s" is expected to be of type "string", but is of type "%s".', $option, $arrayKey, get_debug_type($arrayKey)));
}
if (!\is_string($arrayValue)) {
throw new InvalidOptionsException(sprintf('The option "%s" with value "%s" is expected to be of type "string", but is of type "%s".', $option, $arrayValue, get_debug_type($arrayValue)));
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/SiteConfig/ConfigBuilderConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(array $config)

$resolver->setRequired('site_config');

$resolver->setAllowedTypes('site_config', 'array');
$resolver->setAllowedTypes('site_config', 'string[]');
$resolver->setAllowedTypes('hostname_regex', 'string');

$resolver->setNormalizer('site_config', function (Options $options, $value) {
Expand Down

0 comments on commit 0c7cd93

Please sign in to comment.