lundi 18 mars 2019

PHP: Using Classes to store Non-dynamic data instead of database

In a BI project we have multiple reporter functionalities. so we was defined some classes to implement this features. this classes needs many attributes to chain and build complex queries to generate reports. any other classes should set specific values for this attributes to get reports from this class. values of this attributes are Non-Dynamic so and i don't using database to store that. blow codes are the current model i am using:

Report generator (Main class):

class Report

    {
        private $indicator;
        private $ratio;
        private $divider;
        private $criteria;
        private $plantation;
        private $reporter;

        public function reporter($reporter)
        {
            $this->reporter = (new Reporters())->get($reporter);
            return $this;
        }

        public function plantation($plantationId)
        {
            $this->plantation = $plantationId;
            return $this;
        }

        public function ratio($ratio)
        {
            $this->ratio = (new Ratios())->get($ratio);
            return $this;
        }

        public function divider($divider)
        {
            $this->divider = (new Dividers())->get($divider);
            return $this;
        }

        public function criteria($criteria)
        {
            $this->criteria = $criteria;
            return $this;
        }

        public function get()
        {
            return $this->mocker();
        }

    }

Dividers Class:

class Dividers
{
    public $dividers = [
        'sum' => [
            'name' => 'مجموع',
            'alias' => 'sum',
        ],
        'plantations' => [
            'name' => 'مجموعه انتخابی',
            'alias' => 'plantations',
            'model' => Plantation::class
        ],
        'operation_types' => [
            'name' => 'نوع عملیات',
            'alias' => 'operation_type',
            'model' => OperationType::class
        ],
        'planting_years' => [
            'name' => 'سال زراعی',
            'alias' => 'planting_years',
            'model' => Planting_year::class
        ],
        'crops' => [
            'name' => 'انواع گیاهان',
            'alias' => 'crops',
            'model' => Crop::class
        ],
    ];

    public function get($divider)
    {
        if(!array_key_exists($divider, $this->dividers)){
            return false;
        }
        return $this->dividers[$divider];
    }
}

Ratio Class:

class Ratios
{
    public $ratios = [
        'SUM' => 'انباشته',
        'KILOGRAM' => 'کیلوگرم',
        'HECTARE' => 'هکتار',
        'RIALPERKILO' => 'ریال به کیلوگرم',
        'MILIONRIALPERTON' => 'میلیون ریال بر تن',

    ];

    public function get($ratio)
    {
        if(!array_key_exists($ratio, $this->ratios)){
            return false;
        }
        return $this->ratios[$ratio];
    }
}

So for using report generator i will use this method:

$report = (new Report())
    ->plantation(352)
    ->divider('sum')
    ->reporter('NetProfit', ['operation_type'=> 364])
    ->criteria([['criteriaType'=> 'human_resources', 'value'=> '256'],['criteriaType'=> 'human_resources', 'value'=> '326']])
    ->ratio('sum')
    ->indicator(324, 523, 632)
    ->get();

My question is: what is the best pattern to store this data objects to reduce human mistakes?

Aucun commentaire:

Enregistrer un commentaire