Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ added appendRedirects and removeRedirects site-methods
Signed-off-by: Bruno Meilick <b@bnomei.com>
- Loading branch information
Showing
14 changed files
with
982 additions
and
1,513 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bnomei; | ||
|
||
use Kirby\Toolkit\V; | ||
|
||
final class Redirect | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $fromuri; | ||
/** | ||
* @var string | ||
*/ | ||
private $touri; | ||
/** | ||
* @var int | ||
*/ | ||
private $code; | ||
|
||
public function __construct(string $fromuri, string $touri, $code = 301) | ||
{ | ||
$this->fromuri = $fromuri; | ||
$this->touri = $this->url($touri); | ||
$this->code = static::normalizeCode($code); | ||
} | ||
|
||
public function matches(string $url): bool | ||
{ | ||
return $this->from() === $url; | ||
} | ||
|
||
public function from(): string | ||
{ | ||
return $this->fromuri; | ||
} | ||
|
||
public function to(): string | ||
{ | ||
return $this->touri; | ||
} | ||
|
||
public function code(): int | ||
{ | ||
return $this->code; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
return [ | ||
'fromuri' => $this->from(), | ||
'touri' => $this->to(), | ||
'code' => '_' . $this->code(), | ||
]; | ||
} | ||
|
||
public function __debugInfo() | ||
{ | ||
return $this->toArray(); | ||
} | ||
|
||
public static function url($url): string | ||
{ | ||
$id = '/' . trim($url, '/'); | ||
$page = page($id); | ||
if ($page) { | ||
return $page->url(); | ||
} | ||
|
||
if (V::url($url)) { | ||
return $url; | ||
} | ||
|
||
return url($url); | ||
} | ||
|
||
public static function normalizeCode($code): int | ||
{ | ||
if (is_string($code)) { | ||
$code = intval(str_replace('_', '', $code)); | ||
} | ||
if (! $code) { | ||
$code = 301; | ||
} | ||
|
||
return $code; | ||
} | ||
} |
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
Oops, something went wrong.