<?php
/**
* @author Antony Tkachenko <at@canaryknight.ru>
*/
namespace App\Utm;
class Utm implements \JsonSerializable
{
const PARAMS = [
'utm_source',
'utm_medium',
'utm_content',
'utm_term',
'utm_campaign',
'referrer'
];
const AMO_PROPS = [
'utm_source' => 792337,
'utm_medium' => 792339,
'utm_content' => 792345,
'utm_term' => 792343,
'utm_campaign' => 792341,
'referrer' => 792355,
];
/** @var array<string,string> */
private array $values = [];
private ?string $ymClientId = null;
public static function fromArray(array $values): Utm
{
$self = new self();
foreach ($values as $k => $v) {
if (!in_array($k, self::PARAMS)) {
continue;
}
$self->values[$k] = $v;
}
return $self;
}
public function get(string $name): ?string
{
return $this->values[$name] ?? null;
}
public function getAmoFields(): array
{
$values = [];
foreach (self::AMO_PROPS as $p => $id) {
$value = $this->get($p);
if (!$value) {
continue;
}
$values[] = [
'field_id' => $id,
'values' => [
['value' => $value]
]
];
}
if ($this->getYmClientId()) {
$values[] = [
'field_id' => 792371,
'values' => [
['value' => $this->getYmClientId()]
]
];
}
return $values;
}
public function jsonSerialize(): array
{
return $this->values;
}
public function getYmClientId(): ?string
{
return $this->ymClientId;
}
public function setYmClientId(?string $ymClientId): void
{
$this->ymClientId = $ymClientId;
}
}