PHP code example of inesta / php-schemas

1. Go to this page and download the library: Download inesta/php-schemas 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/ */

    

inesta / php-schemas example snippets


use Inesta\Schemas\Builder\Factory\SchemaFactory;

// Create a simple schema
$article = SchemaFactory::create('Article', [
    'headline' => 'How to Use Schema.org in PHP',
    'author' => 'John Doe',
    'datePublished' => '2024-01-15',
    'description' => 'A comprehensive guide to implementing Schema.org in PHP applications.',
]);

// Render as JSON-LD
echo $article->toJsonLd();

use Inesta\Schemas\Builder\Builders\ArticleBuilder;
use Inesta\Schemas\Builder\Builders\PersonBuilder;

// Create a person
$author = PersonBuilder::create()
    ->name('John Doe')
    ->email('[email protected]')
    ->url('https://johndoe.com')
    ->build();

// Create an article with the author
$article = ArticleBuilder::create()
    ->headline('Advanced PHP Techniques')
    ->description('Learn advanced PHP programming techniques.')
    ->author($author)
    ->datePublished('2024-01-15')
    ->keywords(['php', 'programming', 'tutorial'])
    ->build();

use Inesta\Schemas\Renderer\JsonLd\JsonLdRenderer;

$renderer = new JsonLdRenderer();
$renderer
    ->setPrettyPrint(true)
    ->setIncludeScriptTag(true);

echo $renderer->render($article);

use Inesta\Schemas\Renderer\Microdata\MicrodataRenderer;

$renderer = new MicrodataRenderer();
$renderer
    ->setUseSemanticElements(true)
    ->setIncludeMetaElements(true);

echo $renderer->render($article);

use Inesta\Schemas\Renderer\Rdfa\RdfaRenderer;

$renderer = new RdfaRenderer();
$renderer
    ->setUseSemanticElements(true)
    ->setPrettyPrint(true);

echo $renderer->render($article);

use Inesta\Schemas\Validation\ValidationEngine;
use Inesta\Schemas\Validation\Rules\RequiredPropertiesRule;
use Inesta\Schemas\Validation\Rules\PropertyTypesRule;

$validator = new ValidationEngine();
$validator
    ->addRule(new RequiredPropertiesRule())
    ->addRule(new PropertyTypesRule());

$result = $validator->validate($article);

if (!$result->isValid()) {
    foreach ($result->getErrors() as $error) {
        echo "Error: {$error->getMessage()}\n";
    }
}

use Inesta\Schemas\Validation\Interfaces\ValidationRuleInterface;
use Inesta\Schemas\Validation\ValidationResult;

class CustomValidationRule implements ValidationRuleInterface
{
    public function validate(SchemaTypeInterface $schema): ValidationResult
    {
        // Your custom validation logic
        return new ValidationResult(true);
    }

    public function getPriority(): int
    {
        return 100;
    }
}

$validator->addRule(new CustomValidationRule());

// config/app.php
'providers' => [
    // ...
    Inesta\Schemas\Adapters\Laravel\SchemaServiceProvider::class,
],

'aliases' => [
    // ...
    'Schema' => Inesta\Schemas\Adapters\Laravel\Facades\Schema::class,
],

// Usage in Laravel
$article = Schema::article()
    ->headline('Laravel and Schema.org')
    ->description('Integrating Schema.org with Laravel applications.')
    ->build();

// config/bundles.php
return [
    // ...
    Inesta\Schemas\Adapters\Symfony\SchemaBundle::class => ['all' => true],
];
bash
composer 
html
<script type="application/ld+json">
{
    "@context": "https://schema.org",
    "@type": "Article",
    "headline": "Advanced PHP Techniques",
    "description": "Learn advanced PHP programming techniques.",
    "author": {
        "@type": "Person",
        "name": "John Doe",
        "email": "[email protected]",
        "url": "https://johndoe.com"
    },
    "datePublished": "2024-01-15",
    "keywords": ["php", "programming", "tutorial"]
}
</script>