for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace sokolnikov911\YandexTurboPages;
/**
* Interface CounterInterface
* @package sokolnikov911\YandexTurboPages
*/
class Counter implements CounterInterface
{
const TYPE_LIVE_INTERNET = 'LiveInternet';
const TYPE_GOOGLE_ANALYTICS = 'Google';
const TYPE_MEDIASCOPE = 'Mediascope';
const TYPE_MAIL_RU = 'MailRu';
const TYPE_RAMBLER = 'Rambler';
const TYPE_YANDEX = 'Yandex';
const TYPE_CUSTOM = 'custom';
private $type;
private $id;
private $url;
public function __construct($type, $id = null, $url = null)
$this->type = $type;
$this->id = $id;
$this->url = $url;
if ($type == self::TYPE_CUSTOM && $url === null) {
throw new \UnexpectedValueException('Please set url for custom counter');
}
if ($type != self::TYPE_CUSTOM && $id === null) {
throw new \UnexpectedValueException('Please set id for non custom counter');
public function appendTo(ChannelInterface $channel)
$channel->addCounter($this);
return $this;
public function asXML()
$idPart = $this->id ? ' id="' . $this->id . '" ' : '';
$urlPart = $this->url ? ' url="' . $this->url . '" ' : '';
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><yandex:analytics type="'
. $this->type . '"' . $idPart . $urlPart . ' xmlns:yandex="http://news.yandex.ru"></yandex:analytics>',
LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_ERR_FATAL);
return $xml;