belyashdima-test-tasks / sitemap-lib-test
This test package include tool for saving sitemap in 3 formats: xml, json and csv
Package info
github.com/lolkaspy/SitemapLibTest
pkg:composer/belyashdima-test-tasks/sitemap-lib-test
v1.0.0
2024-09-07 19:14 UTC
Requires
- php: >=8.2
- ext-xmlwriter: *
README
Ссылка на тестовое задание: https://docs.google.com/document/d/115MIjgY5AvGc1bmOYcTBOVYK8SZBS1LrSfvo1agsdWI/edit
Запуск программы
Подключение библиотеки
-
Создаём пустой проект, добавляем файл composer.json.
-
Запускаем следующую команду:
composer run install -
Создаём в проекте файл, к примеру с таким названием:
StartPoint.php -
Перед подключением библиотеки необходимо её достать с портала Packagist:
composer require belyashdima-test-tasks/sitemap-lib-test -
Обновляем composer:
composer update -
В скрипте после тега
<?phpдобавляем следующие строки:
require_once __DIR__ . '/vendor/autoload.php'; use classes\BaseFormatter;
Пример входных данных
$urls = [ [ "loc" => "https://site.ru/", "lastmod" =>"2020-12-14", "priority" =>1, "changefreq"=>"hourly" ], [ "loc" => "https://site.ru/news", "lastmod"=>"2020-12-10", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/about", "lastmod"=>"2020-12-07", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products", "lastmod"=>"2020-12-12", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/products/ps5", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/xbox", "lastmod"=>"2020-12-12", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/wii", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ] ];
Примеры инициализаций объектов по различным форматам файла:
// Путь для примера, можно указать любой другой $path = getenv('HOME').'/uploads/sitemap2.json'; $jsonFile = new BaseFormatter($urls,'json',$path); $path2 = getenv('HOME').'/uploads/sitemap2.сsv'; $csvFile = new BaseFormatter($urls,'csv',$path2); $path3 = getenv('HOME').'/uploads/sitemap2.xml'; $xmlFile = new BaseFormatter($urls,'xml',$path3);
Итоговый код скрипта StartPoint.php
<?php require_once __DIR__ . '/vendor/autoload.php'; use classes\BaseFormatter; $urls = [ [ "loc" => "https://site.ru/", "lastmod" =>"2020-12-14", "priority" =>1, "changefreq"=>"hourly" ], [ "loc" => "https://site.ru/news", "lastmod"=>"2020-12-10", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/about", "lastmod"=>"2020-12-07", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products", "lastmod"=>"2020-12-12", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/products/ps5", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/xbox", "lastmod"=>"2020-12-12", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/wii", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ] ]; // Путь для примера, можно указать любой другой $path = getenv('HOME').'/uploads/sitemap3.json'; $jsonFile = new BaseFormatter($urls,'json',$path); $path2 = getenv('HOME').'/uploads/sitemap3.сsv'; $csvFile = new BaseFormatter($urls,'csv',$path2); $path3 = getenv('HOME').'/uploads/sitemap3.xml'; $xmlFile = new BaseFormatter($urls,'xml',$path3); ?>
Примеры сгенерированных файлов
CSV
JSON
XML
Пример исключения
$path4 = getenv('HOME').'/uploads/sitemap3.txt'; $txtFile = new BaseFormatter($urls,'txt',$path3);
/bin/php /home/belyashdima/PhpstormProjects/untitled2/StartPoint.php
PHP Fatal error: Uncaught Exception: Неподдерживаемый формат файла in /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php:44
Stack trace:
#0 /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php(28): classes\BaseFormatter->setFormatter()
#1 /home/belyashdima/PhpstormProjects/untitled2/StartPoint.php(61): classes\BaseFormatter->__construct()
#2 {main}
thrown in /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php on line 44
Process finished with exit code 255