Appzcoder\CrudGenerator\Commands\CrudModelCommand::buildClass PHP Method

buildClass() protected method

Build the model class with the given name.
protected buildClass ( string $name ) : string
$name string
return string
    protected function buildClass($name)
    {
        $stub = $this->files->get($this->getStub());
        $table = $this->option('table') ?: $this->argument('name');
        $fillable = $this->option('fillable');
        $primaryKey = $this->option('pk');
        $relationships = trim($this->option('relationships')) != '' ? explode(',', trim($this->option('relationships'))) : [];
        if (!empty($primaryKey)) {
            $primaryKey = <<<EOD
/**
    * The database primary key value.
    *
    * @var string
    */
    protected \$primaryKey = '{$primaryKey}';
EOD;
        }
        $ret = $this->replaceNamespace($stub, $name)->replaceTable($stub, $table)->replaceFillable($stub, $fillable)->replacePrimaryKey($stub, $primaryKey);
        foreach ($relationships as $rel) {
            // relationshipname#relationshiptype#args_separated_by_pipes
            // e.g. employees#hasMany#App\Employee|id|dept_id
            // user is responsible for ensuring these relationships are valid
            $parts = explode('#', $rel);
            if (count($parts) != 3) {
                continue;
            }
            // blindly wrap each arg in single quotes
            $args = explode('|', trim($parts[2]));
            $argsString = '';
            foreach ($args as $k => $v) {
                if (trim($v) == '') {
                    continue;
                }
                $argsString .= "'" . trim($v) . "', ";
            }
            $argsString = substr($argsString, 0, -2);
            // remove last comma
            $ret->createRelationshipFunction($stub, trim($parts[0]), trim($parts[1]), $argsString);
        }
        $ret->replaceRelationshipPlaceholder($stub);
        return $ret->replaceClass($stub, $name);
    }