ArticleMicrodata.php 3.12 KB
<?php

namespace frontend\microdata;

use yii\base\BaseObject;
use frontend\microdata\Microdata;


class ArticleMicrodata extends Microdata
{


    # массив, который составляет из себя реально существующие пары свойство/значение
    # !!! так же тут нету пар с пустым значением
    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 createJson()
    {

    }


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

        $test=[];
        foreach ($this->constructArray as $key=>$value)
        {
            if($key=='type')$key='@'.$key;
            $test[$key]=$value;
        }
        $resultJson1=json_encode($test);
        $resultJson1=str_replace('\/','/',$resultJson1);
        $resultJson.=$resultJson1;
        $resultJson.='</script>';
        return $resultJson;
    }


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

    }



    /**

    <script type="application/ld+json">
    {
    "@context": "http://schema.org",
    "@type": "Article",
    "author": "SEO-специалист",
    "interactionStatistic": [
        {
        "@type": "InteractionCounter",
        "interactionService":
           {
            "@type": "Website",
            "name": "Twitter",
            "url": "http://www.twitter.com"
            },
        "interactionType": "http://schema.org/ShareAction",
        "userInteractionCount": "20"
        },
        {
        "@type": "InteractionCounter",
        "interactionType": "http://schema.org/CommentAction",
        "userInteractionCount": "10"
        }
    ],
    "name": "JSON-LD"
    }
    </script>






    <script type="application/ld+json">
    {
    "@context": "http://schema.org",
    "@type": "NewsArticle",
    "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "https://google.com/article"
    },
    "headline": "Article headline",
    "image": [
    "https://example.com/photos/1x1/photo.jpg",
    "https://example.com/photos/4x3/photo.jpg",
    "https://example.com/photos/16x9/photo.jpg"
    ],
    "datePublished": "2015-02-05T08:00:00+08:00",
    "dateModified": "2015-02-05T09:20:00+08:00",
    "author": {
    "@type": "Person",
    "name": "John Doe"
    },
    "publisher": {
    "@type": "Organization",
    "name": "Google",
    "logo": {
    "@type": "ImageObject",
    "url": "https://google.com/logo.jpg"
    }
    },
    "description": "A most wonderful article"
    }
    </script>



     */


}