RepositoryEloquentGenerator.php 1.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php

/**
 * Created by PhpStorm.
 * User: wangy
 * Date: 2020/2/12
 * Time: 9:15
 */

namespace Meibuyu\Micro\Generator;


class RepositoryEloquentGenerator extends Generator
{

    protected $stub = 'repository/eloquent';

    protected $classPath = 'Repository/Eloquent';

    protected $extension = 'RepositoryEloquent';

    /**
     * @return array
     * @throws FileAlreadyExistsException
     */
    public function getReplacements()
    {
        return array_merge(parent::getReplacements(), [
            'interface' => $this->getInterface(),
            'validator' => $this->getValidator(),
            'validator_method' => $this->getValidatorMethod(),
        ]);
    }

    /**
     * @return string
     * @throws FileAlreadyExistsException
     */
    public function getInterface()
    {
        $interfaceGenerator = new RepositoryInterfaceGenerator(['name' => $this->name]);
        $interfaceNamespace = $interfaceGenerator->_namespace . $interfaceGenerator->_name;
        $interfaceGenerator->run();
        return str_replace(["\\", '/'], '\\', $interfaceNamespace) . $interfaceGenerator->getExtension();
    }

    /**
     * @return string
     * @throws FileAlreadyExistsException
     */
    public function getValidator()
    {
        if ($this->validator) {
            $validatorGenerator = new ValidatorGenerator(['name' => $this->name]);
            $validatorNamespace = $validatorGenerator->_namespace . $validatorGenerator->_name;
            $validatorGenerator->run();
            return 'use ' . str_replace(["\\", '/'], '\\', $validatorNamespace) . $validatorGenerator->getExtension() . ';';
        } else {
            return '';
        }

    }

    public function getValidatorMethod()
    {
        if ($this->validator) {
            $class = $this->getClass();
            return 'public function validator()' . PHP_EOL . '    {' . PHP_EOL . '        return ' . $class . 'Validator::class;' . PHP_EOL . '    }' . PHP_EOL;
        } else {
            return '';
        }
    }

}