simialbi / yii2-schema-org

Schema.org yii2 representation and helpers for json-ld generation
MIT License
17 stars 4 forks source link

Сombining several objects into one. #9

Closed rumano closed 2 years ago

rumano commented 3 years ago

Can you combine multiple objects into one? I made small changes in the render() method.

The output code now looks like this.

    public static function render()
    {
        if (!empty(self::$_models))
        {
            $aModels = [];

            foreach (self::$_models as $model)
            {
                $aModels[] = $model->toJsonLDArray();
            }

            try
            {
                echo
                    Html::script
                    (
                        Json::encode($aModels),
                        [
                            'type' => 'application/ld+json'
                        ]
                    ) . "\n";
            }
            catch (InvalidArgumentException $e)
            {
                $logger = Yii::$app->log->logger;
                $logger->log($e->getMessage(), $logger::LEVEL_ERROR);
            }
        }
    }

Now when I call the methods:

            SchemaOrgJsonLDHelper::add($oBreadCrumbsList);
            SchemaOrgJsonLDHelper::add($oSingleFamilyResidence);

Output code:

[{"@context":"http://schema.org","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"https://www.example.com/nassau-county-ny.html","name":"Nassau County"}},{"@type":"ListItem","position":2,"item":{"@id":"https://www.example.com/nassau-city-ny.html","name":"Hempstead"}}],"@type":"BreadcrumbList"},{"@context":"http://schema.org","numberOfBedrooms":4,"floorSize":1296,"geo":{"longitude":"-73.6093200","latitude":"40.7165140","@type":"GeoCoordinates"},"address":{"postalCode":"11550","streetAddress":"Fairview Blvd","addressLocality":"Hempstead","addressRegion":"NY","@type":"PostalAddress"},"url":"https://www.example.com/fairview-blvd-hempstead-ny-11550-4344.html","@type":"SingleFamilyResidence"}]

I checked the validity of the code here: https://validator.schema.org/ No errors were found.

simialbi commented 3 years ago

Hi @rumano Thanks for your input. If you open a PR i could check and merge it.