<?php

namespace adrienpayet\D2P\data\location;

class Location
{
    protected PageDetails $page;
    protected ?FileDetails $file = null;
    protected ?array $parent = null;

    public function __construct(array $data)
    {
        $this->page = new PageDetails($data["page"]);
        
        if (isset($data['file'])) {
            $this->file = new FileDetails($data["file"]);
        }
        if (isset($data["parent"])) {
            $this->parent = [
                "author" => [
                    "name" => $data["parent"]["author"]["name"],
                    "email" => $data["parent"]["author"]["email"],
                ],
                "id" => $data["parent"]["id"]
            ];
        }
    }

    public function toArray() {
        $array = [
            "page" => $this->page->toArray(),
        ];

        if ($this->file) {
            $array["file"] = $this->file;
        }

        if ($this->parent) {
            $array["parent"] = $this->parent;   
        }

        return $array;
    }
}
