{{ '<?php' }}
namespace XeroPHP\Models\{{ model.Namespace }};

use XeroPHP\Remote;
{% if model.SupportsPDF %}
use XeroPHP\Traits\PDFTrait;
{% endif %}
{% if model.SupportsAttachments %}
use XeroPHP\Traits\AttachmentTrait;
{% endif %}
{% for class in model.UsedClasses %}
use XeroPHP\Models\{{ class }};
{% endfor %}

class {{ model.ClassName }} extends Remote\Model
{

{% if model.SupportsPDF %}
    use PDFTrait;
{% endif %}
{% if model.SupportsAttachments %}
    use AttachmentTrait;

{% endif %}
{% for property in model.Properties %}
    /**
     * {{ property.Description|wordwrap(100, "\n     * ")|raw }}
     *
     * @property {{ property.PHPType }}{% if property.isArray %}[]{% endif %} {{ property.Name }}
{% if property.isDeprecated %}     * @deprecated
{% endif %}
     */

{% endfor %}

{% for enum in model.Enums %}
{% for enum_value in enum.Values %}
    const {{ enum_value.constant_name }} = '{{ enum_value.value }}';{% if enum.description %} // {{ enum_value.description }}{% endif %}

{% endfor %}

{% endfor %}

    /**
     * Get the resource uri of the class (Contacts) etc
     *
     * @return string
     */
    public static function getResourceURI()
    {
        return '{{ model.ResourceURI }}';
    }


    /**
     * Get the root node name.  Just the unqualified classname
     *
     * @return string
     */
    public static function getRootNodeName()
    {
        return '{{ model.ClassName }}';
    }


    /**
     * Get the guid property
     *
     * @return string
     */
    public static function getGUIDProperty()
    {
        return '{{ model.GUIDProperty.Name }}';
    }


    /**
     * Get the stem of the API (core.xro) etc
     *
     * @return string|null
     */
    public static function getAPIStem()
    {
        return {% if model.API.StemConstant %}Remote\URL::{{ model.API.StemConstant }}{% else %}null{% endif %};
    }


    /**
     * Get the supported methods
     */
    public static function getSupportedMethods()
    {
        return [
{% for method in model.Methods %}
            Remote\Request::METHOD_{{ method }}{% if not loop.last %},{% endif %}

{% endfor %}
        ];
    }

    /**
     *
     * Get the properties of the object.  Indexed by constants
     *  [0] - Mandatory
     *  [1] - Type
     *  [2] - PHP type
     *  [3] - Is an Array
     *  [4] - Saves directly
     *
     * @return array
     */
    public static function getProperties()
    {
        return [
{% for property in model.Properties %}
            '{{ property.name }}' => [{% if property.isMandatory %}true{% else %}false{% endif %}, self::{{ property.getTypeConstant }}, {% if property.isHintable %}'{{ property.getPHPType(true)|addslashes }}'{% else %}null{% endif %}, {% if property.isArray %}true{% else %}false{% endif %}, {% if property.save_directly %}true{% else %}false{% endif %}]{% if not loop.last %},{% endif %}

{% endfor %}
        ];
    }

    public static function isPageable()
    {
        return {% if model.isPageable %}true{% else %}false{% endif %};
    }

{#  getters and setters #}
{% for property in model.properties %}
    /**
     * @return {{ property.PHPType }}{% if property.isArray %}[]|Remote\Collection
     * Always returns a collection, switch is for type hinting{% endif %}

{% if property.isDeprecated %}     * @deprecated
{% endif %}
     */
    public function get{{ property.Name }}()
    {
        return $this->_data['{{ property.Name }}'];
    }

{% if property.isReadOnly == false %}
    /**
     * @param {{ property.PHPType }} $value
     * @return {{ model.ClassName }}
{% if property.isDeprecated %}     * @deprecated
{% endif %}
     */
    public function {% if property.isArray %}add{% else %}set{% endif %}{{ property.NameSingular }}({% if property.isHintable %}{{ property.PHPType }} {% endif %}$value)
    {
        $this->propertyUpdated('{{ property.Name }}', $value);
        {% if property.isArray %}if (!isset($this->_data['{{ property.Name }}'])) {
            $this->_data['{{ property.Name }}'] = new Remote\Collection();
        }
        $this->_data['{{ property.Name }}'][] = $value;
        {% else %}$this->_data['{{ property.Name }}'] = $value;
        {% endif %}return $this;
    }
{% endif %}

{% endfor %}

}
