oguz-yilmaz/parser

There is no license information available for the latest version (dev-master) of this package.

Lets you create lists of url redirects from csv file for bot Apache and Nginx and custom.

dev-master 2022-04-05 22:01 UTC

This package is auto-updated.

Last update: 2024-05-16 20:37:31 UTC


README

This library lets you create lists of url redirects from csv file.

Installation

Using composer

Use the package manager composer to install the package.

composer require oguz-yilmaz/parser

Usage

require 'vendor/autoload.php';

$file = new Parser\File('urls.csv');
$parser = new Parser\Parser($file, new Parser\ApacheStrategy());

$parser->setRedirectColumns([0,1])
       ->setMainUrl('https://www.example.com')
       ->parse();

echo $parser;

Example Output

Example OUTPUT

Methods

setRedirectColumns()

This is the columns in the csv files.It accepts array with length 2. First element of the array will represent the column of the urls that will be redirected to the urls in the column represented by the second element in the array.

setMainUrl()

Sets the main url of the site so that you can get and use in execute() method of the strategy you are using.
Here is an example strategy that uses main url:

class ApacheStrategy implements StrategyInterface 
{
    public function execute(string $pathFrom, string $pathTo, string $mainUrl = ''): string
    {
        return "Redirect 301 $pathFrom $mainUrl$pathTo";
    }
}

Definin your own custom Strategy class

You should implement the Parser\StrategyInterface interface:

class CustomStrategy implements Parser\StrategyInterface
{
    public function execute(string $pathFrom, string $pathTo, string $mainUrl = ''): string
    {
        return 'Your redirect strategy for each of url';
    }

}

$file = new Parser\File('urls.csv');
$parser = new Parser\Parser($file, new CustomStrategy());

$parser->setRedirectColumns([1,3])
       ->setMainUrl('https://www.example.com')
       ->parse();

echo $parser;