PHP code example of becklyn / schema-org

1. Go to this page and download the library: Download becklyn/schema-org library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

becklyn / schema-org example snippets


use App\Entity\MyNewsArticle;
use Becklyn\SchemaOrg\Data\Article;
use Becklyn\SchemaOrg\Data\Organization;
use Becklyn\SchemaOrg\Data\SchemaOrgDataInterface;
use Becklyn\SchemaOrg\SchemaBuilder\SchemaBuilderInterface;

class MyNewsSchemaBuilder implements SchemaBuilderInterface
{
    /**
     * @inheritDoc
     */
    public function supports ($entity, ?string $usage = null, array $context = []) : bool
    {
        return $entity instanceof MyNewsArticle;
    }


    /**
     * @inheritDoc
     */
    public function buildSchema ($entity, ?string $usage = null, array $context = []) : ?SchemaOrgDataInterface
    {
        \assert($entity instanceof MyNewsArticle);

        // Don't generate metadata for unpublished news
        if (!$entity->isPublished())
        {
            return null;
        }

        return (new Article())
            ->withEditor($entity->getAuthor())
            ->withAbout($entity->getTeaserText())
            ->withArticleBody($entity->getTextContent())
            ->withPublisher(
                (new Organization())
                    ->withName("Awesome Publisher")
                    ->withEmail("[email protected]")
            )
            ->withLicense("Creative Commons Attribution-ShareAlike 3.0")
        ;
    }
}