<?php

/*
 * MicrodataParser
 * Copyright 2019 TopWebStudio. All Rights Reserved.
 * Distribution allowed only with licensing permission
 * contact@topwebstudio.com  
 */

namespace Topwebstudio\Parsers;

class MicrodataParser extends Parser {

    protected $out = array();
    protected $tree;
    protected $schemaContext = 'schema.org';

    public function __construct($content) {
        if (!$content || !$content['items']) {
            return false;
        }

        $this->content = $content;
        $tree = json_decode(file_get_contents(__DIR__ . '/../tree.jsonld.json'), true);
        $this->tree = $tree['children'];
    }

    public function getParsedData() {
        if ($this->content['items']) {
            foreach ($this->content['items'] as $item) {
                $this->out[] = $this->start($item);
            }

            $this->recursive_ksort($this->out);
        }


        return $this->out;
    }

    protected function loop($dataItem, &$out) {
        $this->setType($dataItem, $out);

        if (count($dataItem) == count($dataItem, COUNT_RECURSIVE)) {
            $out = $this->trim($dataItem);
        } else {
            foreach ($dataItem as $id => $propData) {

                $this->setType($propData, $out[$id]);

                $properties = isset($propData['properties']) && !empty($propData['properties']) ? $propData['properties'] : array();
                if ($properties) {
                    foreach ($properties as $_id => $propertyData) {
                        $this->setType($propertyData, $out[$id][$_id]);
                        if (count($propertyData) == 1 && is_string($propertyData[0])) {
                            $out[$id][$_id] = $this->trim($propertyData[0]);
                        } else {
                            $this->loop($propertyData, $out[$id][$_id]);
                        }
                    }
                }
            }
        }

        return $out;
    }

    protected function setType($dataItem, &$out = array()) {
        if (isset($dataItem['type']) && $dataItem['type'][0]) {
            $type = $dataItem['type'][0];

            $type = str_replace('http://' . $this->schemaContext . '/', '', $type);
            $type = str_replace('https://' . $this->schemaContext . '/', '', $type);

            $typeObject = $this->getTreeItemByName($this->tree, $type);
            if ($typeObject && isset($typeObject['name'])) {
                $out['@type'] = $typeObject['name'];
            }
        }
    }

    protected function start($dataItem, &$out = array()) {
        $this->setType($dataItem, $out);
        $out['@context'] = 'https://' . $this->schemaContext . '/';

        $properties = isset($dataItem['properties']) && !empty($dataItem['properties']) ? $dataItem['properties'] : array();
        if ($properties) {
            foreach ($properties as $id => $propertyData) {
                if (count($propertyData) == 1 && is_string($propertyData[0])) {
                    $out[$id] = $this->trim($propertyData[0]);
                } else {
                    $this->loop($propertyData, $out[$id]);
                }
            }
        } 
        // else {
         // $out[$id] = $this->trim($propertyData);
        // }
        return $out;
    }

    protected function getTreeItemByName($treeNode, $name) {
        foreach ($treeNode as $node) {
            if ($node && isset($node['name']) && $node['name'] === $name) {
                return $node;
            }

            if (isset($node['children']) && !empty($node['children'])) {
                $node = $this->getTreeItemByName($node['children'], $name);
                if ($node) {
                    return $node;
                }
            }
        }
    }

}
