2

Magento 2 recommends us to use Service Contracts and we have to define all getter/setters explicitly in the interface and them provide a base implementation to save fields in the database.

It's a lot of manual work and my question is: is there any code generator available for this task? Similar as we had for m1?

Marius
  • 197,939
  • 53
  • 422
  • 830
Amasty
  • 6,508
  • 1
  • 22
  • 59

1 Answers1

2

Here is a small script that can generate the code for your interface. I haven't tested it, so watch out for typos.
You can make it look a bit nicer if you have the time.

    $attributes = [
        'first_attribute_code' => [
                'label'=>'Label 1',
                'type' => 'string'
            ],
        'second_attribute_code' => [
            'label'=>'Label 2',
            'type' => 'int'
        ],
    ];
    $interfaceName = '\Interface\Name\Here';
    $tab = '    '; //4 spaces
    $eol = "\n"; //end of line
    $content = '';
    //generate constants
    foreach ($attributes as $code=>$label) {
        $content .= $tab.'const '.strtoupper($code).' = '.$code.';'.$eol;
    }
    $content .= $eol;
    //generate getters and setters for interface
    foreach ($attributes as $code=>$settings) {
        $camelCaseCode = str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
        $lowerCamelCaseCode = lcfirst($camelCaseCode);
        $content .= $tab.'/**'.$eol;
        $content .= $tab.' * Getter for '.$settings['label'].$eol;
        $content .= $tab.' * '.$eol;
        $content .= $tab.' * @return '.$settings['type'].$eol;
        $content .= $tab.' */'.$eol;
        $content .= $tab.'public function get'.$camelCaseCode.'();'.$eol;
        $content .= $tab.'/**'.$eol;
        $content .= $tab.' * setter for '.$settings['label'].$eol;
        $content .= $tab.' * '.$eol;
        $content .= $tab.' * @param '.$settings['type'].' $'.$lowerCamelCaseCode.$eol;
        $content .= $tab.' * @return '.$interfaceName.$eol;
        $content .= $tab.' */'.$eol;
        $content .= $tab.'public function set'.$camelCaseCode.'($'.$lowerCamelCaseCode.');'.$eol;
    }

    //generate the implementation methods
    foreach ($attributes as $code=>$settings) {
        $camelCaseCode = str_replace(' ', '', ucwords(str_replace('_', ' ', $code)));
        $lowerCamelCaseCode = lcfirst($camelCaseCode);
        $content .= $tab.'/**'.$eol;
        $content .= $tab.' * Getter for '.$settings['label'].$eol;
        $content .= $tab.' * '.$eol;
        $content .= $tab.' * @return '.$settings['type'].$eol;
        $content .= $tab.' */'.$eol;
        $content .= $tab.'public function get'.$camelCaseCode.'()'.$eol;
        $content .= $tab.'{'.$eol;
        $content .= $tab.$tab.'return $this->getData('.$interfaceName.'::'.strtoupper($code).')'.$eol;
        $content .= $tab.'}'.$eol;
        $content .= $tab.'/**'.$eol;
        $content .= $tab.' * setter for '.$settings['label'].$eol;
        $content .= $tab.' * '.$eol;
        $content .= $tab.' * @param '.$settings['type'].' $'.$lowerCamelCaseCode.$eol;
        $content .= $tab.' * @return '.$interfaceName.$eol;
        $content .= $tab.' */'.$eol;
        $content .= $tab.'public function set'.$camelCaseCode.'($'.$lowerCamelCaseCode.')'.$eol;
        $content .= $tab.'{'.$eol;
        $content .= $tab.$tab.'return $this->setData('.$interfaceName.'::'.strtoupper($code).', '.$lowerCamelCaseCode.')'.$eol;
        $content .= $tab.'}'.$eol;
    } 
    //do something with $content
Marius
  • 197,939
  • 53
  • 422
  • 830
  • Yes, something like this. I hoped it had been implemented before. I'll check if we can develop some ready to use script and post it as well. – Amasty Sep 13 '16 at 16:40
  • 1
    I didn't implement it before. I started on it when i saw your question. :) – Marius Sep 13 '16 at 17:02