Models output now matches php_cs_fixer output

This commit is contained in:
Greyscale 2019-12-19 14:46:29 +01:00
parent 0fde8d9abe
commit cfe3f6d972
2 changed files with 84 additions and 100 deletions

View file

@ -1,36 +1,33 @@
<?php
namespace {{ namespace }}\Models\Base;
use \⌬\⌬ as App;
use \{{ namespace }}\{{ namespace }};
use \Gone\AppCore\Exceptions;
use \⌬\Controllers\Abstracts\Model as AbstractModel;
use {{ namespace }}\Models;
use {{ namespace }}\TableGateways;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ⌬\Controllers\Abstracts\Model as AbstractModel;
use ⌬\Database\Interfaces\ModelInterface as ModelInterface;
use \{{ namespace }}\Services;
use \{{ namespace }}\Models;
use \{{ namespace }}\TableGateways;
use \{{ namespace }}\Models\{{ class_name }}Model;
use ⌬\⌬ as App;
{% include '_overwrite_warning.twig' %}
{% set existingMethods = [] %}
abstract class Base{{ class_name }}Model
extends AbstractModel
implements ModelInterface
abstract class Base{{ class_name }}Model extends AbstractModel implements ModelInterface
{
// Declare what fields are available on this object
{% for column in columns %}
const FIELD_{{ column.getFieldSanitised|upper }} = '{{ column.getField }}';
public const FIELD_{{ column.getFieldSanitised|upper }} = '{{ column.getField }}';
{% endfor %}
{% for column in columns %}
const TYPE_{{ column.getFieldSanitised|upper }} = '{{ column.getDbType }}';
public const TYPE_{{ column.getFieldSanitised|upper }} = '{{ column.getDbType }}';
{% endfor %}
// Constant arrays defined by ENUMs
{% for column in columns %}
{% if column.getDbType == 'enum' and column.getPhpType == 'string' %}
const OPTIONS_{{ column.getFieldSanitised|upper }} = ["{{ column.getPermittedValues | join('", "') | raw }}"];
public const OPTIONS_{{ column.getFieldSanitised|upper }} = ["{{ column.getPermittedValues | join('", "') | raw }}"];
{% endif %}
{% endfor %}
@ -38,19 +35,19 @@ abstract class Base{{ class_name }}Model
{% for column in columns %}
{% if column.getDbType == 'enum' and column.getPhpType == 'string' %}
{% for permittedValue in column.getPermittedValues %}
const {{ column.getFieldSanitised|upper }}_{{ permittedValue|upper|replace({'-':'_'}) }} = '{{ permittedValue }}';
public const {{ column.getFieldSanitised|upper }}_{{ permittedValue|upper|replace({'-':'_'}) }} = '{{ permittedValue }}';
{% endfor %}
{% endif %}
{% endfor %}
{% if primary_keys %}
protected $_primary_keys = ['{{ primary_keys|join('\', \'')|raw }}'];
{% endif %}
{% endif %}
{% if autoincrement_keys %}
protected $_autoincrement_keys = ['{{ autoincrement_keys|join('\', \'')|raw }}'];
{% endif %}
{% endif %}
{% for column in columns %}
{% if column.default_value %}
protected ${{ column.getFieldSanitised }} = '{{ column.default_value }}';
@ -60,86 +57,83 @@ abstract class Base{{ class_name }}Model
{% endfor %}
/**
* @param array $data An array of a {{ class_name }}Model's properties.
* @return {{ class_name }}Model
* @param array $data an array of a Models\{{ class_name }}Model's properties
*
* @return Models\{{ class_name }}Model
*/
static public function factory(array $data = [])
public static function factory(array $data = [])
{
return parent::factory($data);
}
/**
* Returns an array describing the properties of this model.
*
* @return array
*/
public function getPropertyMeta()
public function getPropertyMeta(): array
{
{% for column in columns %}
{% if column.hasRelatedObjects %}
${{ column.getField|replace({"Id":"s"}) }}Service = App::Container()->get(Services\{{ column.getModel.getClassName }}Service::class);
{% endif %}
{% endfor %}
$properties = [
return [
{% for column in columns %}
self::FIELD_{{ column.getFieldSanitised|upper }} => [
'type' => self::TYPE_{{ column.getFieldSanitised|upper }},
'type' => self::TYPE_{{ column.getFieldSanitised|upper }},
{% if column.getMaxLength > 0 %}
'length' => {{ column.getMaxLength }},
'length' => {{ column.getMaxLength }},
{% endif %}
{% if column.getDbType == 'enum' and column.getPhpType == 'string' %}
'options' => [
'options' => [
{% for permittedValue in column.getPermittedValues %}
'{{ permittedValue }}',
'{{ permittedValue }}',
{% endfor %}
],
],
{% if column.getDefaultvalue %}
'default' => '{{ column.getDefaultValue() }}',
'default' => '{{ column.getDefaultValue() }}',
{% endif %}
{% endif %}
{% if column.hasRelatedObjects %}
'remoteOptionsLoader' => ${{ column.getField|replace({"Id":"s"}) }}Service->getAll(),
'remoteOptionsLoader' => ${{ column.getField|replace({"Id":"s"}) }}Service->getAll(),
{% endif %}
],
],
{% endfor %}
];
return $properties;
}
{% for column in columns %}
/**
* @return {{ column.phptype }}
*/
public function get{{ column.getPropertyFunction }}() {% if column.phptype %}: ?{{ column.phptype }}{% endif %}
public function get{{ column.getPropertyFunction }}(){{ column.phptype ? ": ?#{column.phptype}" }}
{
return $this->{{ column.getFieldSanitised }};
}
/**
* @param {{ column.phptype }} ${{ column.getFieldSanitised }}
* @return {{ class_name }}Model
*
* @return self
*/
public function set{{ column.getPropertyFunction }}({{ column.phptype }} ${{ column.getFieldSanitised }} = null) : {{ class_name }}Model
public function set{{ column.getPropertyFunction }}({{ column.phptype }} ${{ column.getFieldSanitised }} = null): self
{
$this->{{ column.getFieldSanitised }} = ${{ column.getFieldSanitised }};
return $this;
}
{% endfor %}
/*****************************************************
* "Referenced To" Remote Constraint Object Fetchers *
*****************************************************/
{% for related_object in related_objects %}
/**
* @return null|Models\{{ related_object.getRemoteClass }}Model
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
{% if related_object.getRemoteFunctionName != class_name %}
public function fetch{{ related_object.getRemoteFunctionName|singularize|transform_camel_to_studly }}Object() : ?Models\{{ related_object.getRemoteClass }}Model
public function fetch{{ related_object.getRemoteFunctionName|singularize|transform_camel_to_studly }}Object(): ?Models\{{ related_object.getRemoteClass }}Model
{% else %}
public function fetchRelated{{ related_object.getRemoteFunctionName|singularize|transform_camel_to_studly }}Object() : ?Models\{{ related_object.getRemoteClass }}Model
public function fetchRelated{{ related_object.getRemoteFunctionName|singularize|transform_camel_to_studly }}Object(): ?Models\{{ related_object.getRemoteClass }}Model
{% endif %}
{
/** @var ${{ related_object.getRemoteClass }}Service Services\{{ related_object.getRemoteClass }}Service */
@ -153,31 +147,26 @@ abstract class Base{{ class_name }}Model
{% endfor %}
{% if remote_objects %}
/*****************************************************
* "Referenced By" Remote Constraint Object Fetchers *
*****************************************************/
{% for remote_object in remote_objects %}
{% if remote_object.getLocalClass != class_name %}
{% if remote_object.getLocalFunctionName|singularize|transform_camel_to_studly not in existingMethods %}
{% set existingMethods = existingMethods|merge([ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly ]) %}
/**
* Fetch a singular {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that references this {{ class_name }}Model.
* Fetch a singular {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that references this Models\{{ class_name }}Model.
*
* @param $orderBy string Column to order by. Recommended to use the Constants in Models\{{ remote_object.getLocalClass }}Model::
* @param $orderDirection string Either "DESC" or "ASC". Recommend using Select::ORDER_ASCENDING or Select::ORDER_DESCENDING
*
* @return null|Models\{{ remote_object.getLocalClass }}Model
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function fetch{{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }}Object(
{% if remote_object.local_model_key != 'id' %}
$orderBy = null,
$orderDirection='ASC'
{% endif %}
) : ?Models\{{ remote_object.getLocalClass }}Model {
): ?Models\{{ remote_object.getLocalClass }}Model {
/** @var ${{ remote_object.getLocalVariable }}Service Services\{{ remote_object.getLocalClass }}Service */
${{ remote_object.getLocalVariable }}Service = App::Container()->get(Services\{{ remote_object.getLocalClass }}Service::class);
{% if remote_object.local_model_key == 'id' %}
@ -188,7 +177,7 @@ abstract class Base{{ class_name }}Model
}
/**
* Fetch all matching {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that reference this {{ class_name }}Model.
* Fetch all matching {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that reference this Models\{{ class_name }}Model.
*
* @param $limit int Number to fetch maximum
* @param $orderBy string Column to order by. Recommended to use the Constants in Models\{{ remote_object.getLocalClass }}Model::
@ -196,14 +185,14 @@ abstract class Base{{ class_name }}Model
*
* @return Models\{{ remote_object.getLocalClass }}Model[]
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function fetch{{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }}Objects(
int $limit = null,
string $orderBy = null,
string $orderDirection='ASC'
) : ?array {
): ?array {
/** @var ${{ remote_object.getLocalVariable }}Service Services\{{ remote_object.getLocalClass }}Service */
${{ remote_object.getLocalVariable }}Service = App::Container()->get(Services\{{ remote_object.getLocalClass }}Service::class);
{% if remote_object.local_model_key == 'id' %}
@ -214,14 +203,13 @@ abstract class Base{{ class_name }}Model
}
/**
* Count the number of matching {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that reference this {{ class_name }}Model.
* Count the number of matching {{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }} that reference this Models\{{ class_name }}Model.
* Returns the number of objects found.
*
* @return int Number of objects found.
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function count{{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }}Objects() : int {
public function count{{ remote_object.getLocalFunctionName|singularize|transform_camel_to_studly }}Objects(): int {
/** @var ${{ remote_object.getLocalVariable }}Service Services\{{ remote_object.getLocalClass }}Service */
${{ remote_object.getLocalVariable }}Service = App::Container()->get(Services\{{ remote_object.getLocalClass }}Service::class);
{% if remote_object.local_model_key == 'id' %}
@ -234,44 +222,41 @@ abstract class Base{{ class_name }}Model
{% endif %}
{% endfor %}
{% endif %}
/**
* @return {{ class_name }}Model
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function save()
public function save(): Models\{{ class_name }}Model
{
/** @var $tableGateway TableGateways\{{ class_name }}TableGateway */
/** @var TableGateways\{{ class_name }}TableGateway $tableGateway */
$tableGateway = App::Container()->get(TableGateways\{{ class_name }}TableGateway::class);
return $tableGateway->save($this);
}
/**
* Destroy the current record.
* Returns the number of affected rows.
*
* @return int Number of affected rows.
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function destroy() : int
public function destroy(): int
{
/** @var $tableGateway TableGateways\{{ class_name }}TableGateway */
/** @var TableGateways\{{ class_name }}TableGateway $tableGateway */
$tableGateway = App::Container()->get(TableGateways\{{ class_name }}TableGateway::class);
return $tableGateway->delete($this->getPrimaryKeys());
}
/**
* Destroy the current record, and any dependencies upon it, recursively.
* Returns the number of affected rows.
*
* @return int Number of affected models.
*
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function destroyThoroughly() : int
public function destroyThoroughly(): int
{
{% if remote_objects.length > 0 %}
$countOfThingsDestroyed = 0;
@ -296,12 +281,12 @@ abstract class Base{{ class_name }}Model
{% endif %}
}
/**
* Provides an array of all properties in this model.
* @return array
*
* @return string[]
*/
public function getListOfProperties()
public function getListOfProperties(): array
{
return [
{% for column in columns %}
@ -309,4 +294,4 @@ abstract class Base{{ class_name }}Model
{% endfor %}
];
}
}
}

View file

@ -1,13 +1,12 @@
/********************************************************
* ___ __ *
* / _ \___ ____ ___ ____ ____/ / *
* / // / _ `/ _ \/ _ `/ -_) __/_/ *
* /____/\_,_/_//_/\_, /\__/_/ (_) *
* /___/ *
* *
* Anything in this file is prone to being overwritten! *
* *
* This file was programatically generated. To modify *
* this classes behaviours, do so in the class that *
* extends this, or modify the Zenderator Template! *
********************************************************/
/** ___ __
* / _ \___ ____ ___ ____ ____/ /
* / // / _ `/ _ \/ _ `/ -_) __/_/
* /____/\_,_/_//_/\_, /\__/_/ (_)
* /___/.
*
* Anything in this file is prone to being overwritten!
*
* This file was programatically generated. To modify
* this classes behaviours, do so in the class that
* extends this, or modify the Zenderator Template!
*/