<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Typologies extends JsonResource
{
    /**
     * Typologies constructor.
     * @param $resource
     */
    protected $isChildren = FALSE;

    /**
     * Themes constructor.
     * @param $resource
     */
    public function __construct($resource, $isChildren = FALSE)
    {
        $this->resource = $resource;
        $this->isChildren = $isChildren;
    }

    public function toArray($request)
    {
        $out = [];
        if (!$this->isChildren) {
    
            $out = [
                'id'          => (int) $this->id,
                'name'        => $this->title,
                'language_id' => $this->language_id,
            ];
    
            $out['children'] = [];
    
            // Get child info
            // if there is any
            if ($this->children) {
                foreach ($this->children as $child) {
                    $out['children'][] = new Typologies($child, TRUE);
                }
            }
    
        } elseif ($this->isChildren) {
    
            $out = [
                'id'          => (int) $this->id,
                'name'        => $this->title,
                'language_id' => $this->language_id,
            ];
        }
        return $out;
    }
}
