OrganizationMicrodata.php 1.62 KB
<?php
namespace frontend\microdata;

use yii\base\BaseObject;


class OrganizationMicrodata extends  Microdata
{


	public $brand=null;
	public $aggregateRating=null;
	public $offers=null;
	public $description=null;
	public $npm=null;


	public $type=null;
	public $url=null;
	public $logo=null;
	public $contactPoint=null;
	public $telephone=null;
	public $contactType=null;





	# массив, который составляет из себя реально существующие пары свойство/значение
	# !!! так же тут нету пар с пустым значением
	public $constructArray=[];

	public function __construct(array $config = [])
	{

		# обрезаю входящие свойства, работаем только с реально существующими
		foreach ($config as $key=>$value){
			if(!property_exists(self::class,$key))
			{
				unset($config[$key]);
			}
			elseif($value==null || $value===false || $value=='')
			{
				unset($config[$key]);
			}



		}

		$this->constructArray=$config;

		parent::__construct($config);
	}

	public function toJson(): string
	{
		$resultJson=" <script type=\"application/ld+json\">";

		$finalArray=[];
		foreach ($this->constructArray as $key=>$value)
		{
			if($key=='type' || $key=='context')$key='@'.$key;
			$finalArray[$key]=$value;
		}

		$resultJson1=json_encode($finalArray,JSON_UNESCAPED_UNICODE);
		$resultJson1=str_replace('\/','/',$resultJson1);
		$resultJson.=$resultJson1;
		$resultJson.='</script>';

		return $resultJson;
	}



	function add($key,$value)
	{
		if ($value!==null)
			$this->constructArray[$key]=$value;

	}









}