UserInputParserTrait.php in Warmer 8
File
modules/warmer_cdn/src/Plugin/warmer/UserInputParserTrait.php
View source
<?php
namespace Drupal\warmer_cdn\Plugin\warmer;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
trait UserInputParserTrait {
private function extractTextarea(array $values, $key) {
if (!array_key_exists($key, $values)) {
return [];
}
if (!is_string($values[$key])) {
return $values[$key];
}
return array_filter(array_map(function ($val) {
return trim($val, "\r");
}, explode("\n", $values[$key])));
}
private function validateHeaders(array $form, FormStateInterface $form_state) {
$headers = $form_state
->getValue('headers');
$lines = array_filter(explode("\n", $headers));
$colons = array_reduce($lines, function ($carry, $line) {
return strpos($line, ':') === FALSE ? $carry : $carry + 1;
}, 0);
if ($colons && $colons !== count($lines)) {
$form_state
->setError($form['headers'], $this
->t('All headers must have a colon. Follow the format: <code>the-name: the-value</code>'));
}
}
private function resolveUri($user_input) {
try {
return Url::fromUri($user_input, [
'absolute' => TRUE,
])
->toString();
} catch (\InvalidArgumentException $e) {
}
try {
return Url::fromUserInput($user_input, [
'absolute' => TRUE,
])
->toString();
} catch (\InvalidArgumentException $e) {
}
return $user_input;
}
}