Skip to content

Commit

Permalink
remove deps: laminas/laminas-validator
Browse files Browse the repository at this point in the history
  • Loading branch information
fetus-hina committed Nov 16, 2024
1 parent 3244739 commit 08d91f1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
32 changes: 22 additions & 10 deletions commands/BlogFeedController.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (C) 2015-2021 AIZAWA Hina
* @copyright Copyright (C) 2015-2024 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@fetus.jp>
*/
Expand All @@ -12,17 +12,19 @@

use Exception;
use Laminas\Feed\Reader\Reader as FeedReader;
use Laminas\Validator\Uri as UriValidator;
use TypeError;
use Yii;
use app\components\helpers\TypeHelper;
use app\models\BlogEntry;
use jp3cki\uuid\NS as UuidNS;
use jp3cki\uuid\Uuid;
use yii\console\Controller;

use function preg_match;
use function printf;
use function usort;

class BlogFeedController extends Controller
final class BlogFeedController extends Controller
{
public function actionCrawl()
{
Expand Down Expand Up @@ -56,14 +58,14 @@ private function processEntry($entry)
if (!$id) {
return;
}
$uuid = Uuid::v5(
(new UriValidator())->isValid($id)
? UuidNs::url()
: 'd0ec81fc-c8e6-11e5-a890-9ca3ba01e1f8',
$id,
)->__toString();
$uuid = TypeHelper::string(
Uuid::v5(
self::isValidUrl($id) ? UuidNs::url() : 'd0ec81fc-c8e6-11e5-a890-9ca3ba01e1f8',
$id,
),
);
$link = $entry->getLink();
if (!(new UriValidator())->isValid($link)) {
if (!self::isValidUrl($link)) {
return;
}

Expand All @@ -85,4 +87,14 @@ private function processEntry($entry)
echo "Registered new blog entry\n";
printf(" #%d, %s %s\n", $model->id, $model->url, $model->title);
}

private static function isValidUrl(mixed $url): bool
{
try {
$url = TypeHelper::url($url);
return (bool)preg_match('#\Ahttps?://#i', $url);
} catch (TypeError) {
return false;
}
}
}
17 changes: 16 additions & 1 deletion components/helpers/TypeHelper.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @copyright Copyright (C) 2015-2023 AIZAWA Hina
* @copyright Copyright (C) 2015-2024 AIZAWA Hina
* @license https://github.com/fetus-hina/stat.ink/blob/master/LICENSE MIT
* @author AIZAWA Hina <hina@fetus.jp>
*/
Expand All @@ -21,8 +21,10 @@
use function is_scalar;
use function is_string;

use const FILTER_FLAG_PATH_REQUIRED;
use const FILTER_VALIDATE_FLOAT;
use const FILTER_VALIDATE_INT;
use const FILTER_VALIDATE_URL;

final class TypeHelper
{
Expand Down Expand Up @@ -82,6 +84,19 @@ public static function floatOrNull(mixed $value): ?float
return is_float($value) ? $value : null;
}

public static function url(mixed $value): string
{
$value = filter_var(
self::string($value);
FILTER_VALIDATE_URL,
FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED,
);
return match (true) {
is_string($value) => $value,
default => throw new TypeError('The value is not a valid URL'),
};
}

public static function array(mixed $value): array
{
return match (true) {
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"jp3cki/yii2-zxcvbn": "^1.0",
"laminas/laminas-feed": "^2.23.0",
"laminas/laminas-http": "^2.20",
"laminas/laminas-validator": "^2.64.1",
"lipis/flag-icons": "^7.2.3",
"lusitanian/oauth": "^0.8.11",
"markrogoyski/math-php": "^2.10.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 08d91f1

Please sign in to comment.