<?php

namespace GriffinForms;

class Heirarchy
{
    protected $form;
    protected $formpage;
    protected $pagerow;
    protected $rowcolumn;
    protected $field;
    private static $instance;
    
    protected function setForm()
    {
        $this->form = array(
            'children' => 'formpages',
            'child' => 'formpage',
            'parent' => 'folder'
        );
    }    
    
    protected function setFormPage()
    {
        $this->formpage = array(
            'children' => 'pagerows',
            'child' => 'pagerow',
            'parent' => 'form'
        );
    }    
    
    protected function setPageRow()
    {
        $this->pagerow = array(
            'children' => 'rowcolumns',
            'child' => 'rowcolumn',
            'parent' => 'formpage'
        );
    }    
    
    protected function setRowColumn()
    {
        $this->rowcolumn = array(
            'children' => 'fields',
            'child' => 'field',
            'parent' => 'pagerow',
        );
    }    
    
    protected function setField()
    {
        $this->field = array(
            'children' => 'fieldresponses',
            'child' => 'fieldresponse',
            'parent' => 'rowcolumn',
        );
    }
    
    public function children($type)
    {
        if(isset($this->$type['children'])) {
            return $this->$type['children'];
        } else {
            return null;
        }
    }    
    
    public function child($type)
    {
        if(isset($this->$type['child'])) {
            return $this->$type['child'];
        } else {
            return null;
        }
    }    
    
    public function parents($type)
    {
        if(isset($this->$type['parent'])) {
            return $this->$type['parent'];
        } else {
            return null;
        }
    }
    
    public static function getInstance()
    {
        if (self::$instance == null)
        {
            self::$instance = new Heirarchy();
        }
        
        return self::$instance;
    }
    
    private function __construct()
    {
        foreach($this as $key => $value) {
            $method = 'set' . $key;
            $this->$method();
        }
    }
}