<?php
namespace app\components\amp;

use Yii;
use magyarandras\AMPConverter\TagConverterInterface;

class AMPAhref implements TagConverterInterface
{
    private $necessary_scripts = [];

    public function convert(\DOMDocument $doc)
    {
        $allowed_attributes = ['class', 'id', 'target', 'href'];
		$allowed_target = ['_blank', '_self', '_top', '_parent'];

		$ahrefs = $doc->getElementsByTagName('a');
		foreach($ahrefs as $element){
			for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
				if( array_search( $element->attributes->item($k)->nodeName, $allowed_attributes ) === false || ( $element->attributes->item($k)->nodeName == 'target' && array_search( $element->attributes->item($k)->nodeValue, $allowed_target) === false) ){
					$element->removeAttributeNode( $element->attributes->item($k));
				}
			}
		}
        return $doc;
    }

    public function getNecessaryScripts()
    {
        return $this->necessary_scripts;
    }
}
