t1gor/Robots.txt-Parser-Class

View on GitHub
source/Parser/DirectiveProcessors/SitemapProcessor.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php declare(strict_types=1);

namespace t1gor\RobotsTxtParser\Parser\DirectiveProcessors;

use t1gor\RobotsTxtParser\Directive;

class SitemapProcessor extends AbstractDirectiveProcessor implements DirectiveProcessorInterface {

    public function getDirectiveName(): string {
        return Directive::SITEMAP;
    }

    public function process(string $line, array & $root, string & $currentUserAgent = '*', string $prevLine = '') {
        $parts = explode(':', $line);
        array_shift($parts);
        $trimmed = array_map('trim', $parts);
        $entry   = implode(':', $trimmed);

        if (!isset($root[$currentUserAgent][Directive::SITEMAP])) {
            $root[$currentUserAgent][Directive::SITEMAP] = [];
        }

        if (!in_array($entry, $root[$currentUserAgent][Directive::SITEMAP])) {
            $root[$currentUserAgent][Directive::SITEMAP][] = $entry;
        } else {
            $this->log(strtr('{directive} with value {faulty} skipped as already exists for {useragent}', [
                '{directive}' => Directive::SITEMAP,
                '{faulty}'    => $entry,
                '{useragent}' => $currentUserAgent,
            ]));
        }
    }
}