class

Source code:

<?php
{strip}
@_DIALECT = div_php_dialect.json

/*{
    @name PHP class
    @description Generate a PHP Class
    @version 1.0
    @path php/class.tpl
    @update 5/9/2017
    @author Rafa Rodriguez rafageist
    @engine div/generator/divGenerator.php
    @vars:
          optional string scope Default scope (public, private, protected) for methods and properties
          optional string type Default type (static or not) for a method
          required string name The name of the class
          optional string extends The comma-separated list of extended classes
          optional list(scope|name|default) properties List of properties
}*/

{= scope: "public" =}
{= type: "" =}

?$namespace namespace {$namespace}; $namespace?
{/strip}
/**
 * Class {$name}
 *
 * @author {$author}
 * @version {$version}
 */
class {camel:name} ?$extends extends {$extends} $extends?
{
{strip}
    [$properties] property =>
    {?( "{$type}" == "enum" )?}
    [$values]
    {\t}const ENUM_{^^^property._key}_{^value} = "{_value}";
    [/$values]
    {/?}
    @empty@
    [/$properties]
    [$properties]
    {\t}{$scope} ${$_key};
    @empty@
    [/$properties]
{/strip}

    //custom-properties
    //custom-properties

    public function __construct()
    {
{strip}
        [$properties]
        ?$default
            {?( "{$type}" == "enum" )?}
                {\t}{\t}$this->{$_key} = self::ENUM_{^^^_key}_{^default};{\n}
            @else@
                {\t}{\t}$this->{$_key} = {$default};{\n}
            {/?}
        $default?
        [/$properties]
{/strip}
    }

    [$properties]
    /**
     * Property {$_key}
     */
    public function get{camel:_key}() {
        ?$before_get
        return {$before_set}($value);
        @else@
        return $this->{$_key};
        $before_get?
    }

    public function set{camel:_key}($value) {
        ?$before_set
        $value = {$before_set}($value);
        $before_set?
        $this->{$_key} = $value;
    }

    {?( "{$type:-2,2}" == "[]" )?}
    /*
    * @param {$type:0,-2} $value
    */
   ?$singular public function add{camel:singular}($value) @else@ public function add{camel:_key}($value) $singular?
    {
        $this->{$_key}[] = $value;
    }

   ?$singular public function del{camel:singular}ByValue($value) @else@ public function del{camel:_key}($value) $singular?
    {
        ?$searcher $key = {$searcher}($value); @else@ $key = array_search($value, $this->{$_key}); $searcher?
        $deleted = null;
        if ($key !== false)
        {
            $deleted = $this->{$_key}[$key];
            unset($this->{$_key}[$key]);
        }
        return $deleted;
    }
	
	?$singular public function del{camel:singular}ByKey($key) @else@ public function del{camel:_key}($value) $singular?
    {
        $deleted = null;
        if (isset($this->{$_key}[$key]))
        {
            $deleted = $this->{$_key}[$key];
            unset($this->{$_key}[$key]);
        }
        return $deleted;
    }

	
    ?$singular public function have{camel:singular}($value) @else@ public function have{camel:_key}($value) $singular?
    {
    ?$searcher $key = {$searcher}($value); @else@ $key = array_search($value, $this->{$_key}); $searcher?
        return $key !== false;
    }

    @else@

    {?( "{$type}" == "key-value" )?}

    public function get{camel:_key}Value($key, $default = null)
    {
        if (isset($this->{$_key}[{\$}key]))
            return $this->{$_key}[{\$}key];

        return $default;
    }

    public function set{camel:_key}Value($key, $value)
    {
        $this->{$_key}[{\$}key] = $value;
    }

    @else@

    public function cmp{camel:_key}($value) {
        $original = $this->{$_key};
        ?$before_set
        $value = {$before_set}($value);
        $before_set?

        if ($original == $value) return 0;
        if ($original < $value) return -1;
        if ($original <= $value) return -2;
        if ($original > $value) return 1;
        if ($original >= $value) return 2;
        if ($original === $value) return 3;
    }

    public function isEqual{camel:_key}($value) {
        return $this->cmp{camel:_key}($value) == 0;
    }
    {/?}
    {/?}
    @empty@
    [/$properties]

    //custom-methods
    //custom-methods
}