Your IP : 216.73.216.240


Current Path : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/
Upload File :
Current File : /home/juvelize/saulnois/tmp/install_695d2cf0b4957/src/View/Sd/SdRecipe.php

<?php
/*
* @package		Tech Fry Library
* @license		https://www.gnu.org/licenses/gpl-3.0.en.html
*/

namespace TechFry\Library\View\Sd;

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;

// https://developers.google.com/search/docs/appearance/structured-data/recipe
class SdRecipe
{
    public static function get_output($data = array())
    {
        $uri = Uri::getInstance();
        
        $name = $data['name'] ?? $data['title'];
        
        $json = array(
            "@context" => "https://schema.org",
            "@type" => "Recipe",
            "name" => $name,
            "description" => $data['description'],
            "datePublished" => $data['publish_up'],
            "dateModified" => $data['modified'],
            "recipeCategory" => $data['recipeCategory'],
            "recipeCuisine" => $data['recipeCuisine'],
            "totalTime" => "PT" . $data['totalTime'] . "M",
            "keywords" => $data['keywords'],
            "recipeYield" => $data['recipeYield'],
        );

        // Ingredients ($data['ingredients'] can be 1D or 2D Array)
        $ingredients = array();
        if (isset($data['ingredients']))
        {
            foreach ($data['ingredients'] as $ingredient)
            {
                if (\is_array($ingredient))
                {
                    foreach ($ingredient as $i)
                    {
                        $ingredients[] = $i;
                    }
                }
                else
                {
                    $ingredients[] = $ingredient;
                }
            }
            $json["recipeIngredient"] = $ingredients;
        }

        // Instructions ($data['instructions'] is 2D Array)
        $instructions = array();
        $n = 1;
        if (isset($data['instructions']))
        {
            foreach ($data['instructions'] as $instruction)
            {
                $instruction = array_values($instruction);
                
                $instructions[] = array(
                    "@type" => "HowToStep", 
                    "text" => $instruction[0],
                    "name" => $instruction[1] ?? '',
                    "url" => $uri . '#step' . $n,
                    "image" => isset($instruction[2]) && $instruction[2] ? $uri->base() . $instruction[2] : '',
                );
                $n++;
            }
            $json["recipeInstructions"] = $instructions;
        }

        // Author
        $json["author"] = array(
            "@type" => "Person",
            "name" => $data['author'],
        );

        // Rating
        if (isset($data['rating_count']) && $data['rating_count'])
        {
            $json["aggregateRating"] = array(
                "ratingCount" => $data['rating_count'] ?? $data["ratingCount"],
                "ratingValue" => $data['rating_value'] ?? $data["ratingValue"],
            );
        }

        // Image
        if (isset($data['image']) && $data['image'])
        {
            $json['image'] = Sd::add_image($data['image']);
        }

        // Video
        if (isset($data['video_url']) && $data['video_url'])
        {
            $json["video"] = array(
                "@type" => "VideoObject",
                "name" => $name,
                "description" => 'Video about ' . $name,
                "thumbnailUrl" => $uri->base() . $data['image'],
                "uploadDate" => $data['publish_up'],
                "embedUrl" => $data['video_url'],
            );
        }

        // Calories
        if (isset($data['calories']) && $data['calories'])
        {
            $json["nutrition"] = array(
                "@type" => "NutritionInformation",
                "calories" => $data['calories'] . ' Calories',
            );
        }

        Sd::display_sd($json);
    }
}