Created
February 11, 2017 16:23
-
-
Save calina-c/926a0281724e95fea9df5c9f7d8d9756 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Forms; | |
use Kris\LaravelFormBuilder\Form; | |
use Yaml; | |
use Carbon\Carbon; | |
use App\Forms\Report\FormConfig; | |
use App\Forms\Entities\EntityFormConfig; | |
use App\Services\ReportServices\ReportStringProcessingTrait; | |
use App\Forms\Config\QueryBuilderTrait; | |
abstract class YamlConfigForm extends Form | |
{ | |
use ReportStringProcessingTrait; | |
use QueryBuilderTrait; | |
protected $folder = 'Entities'; | |
protected $saveLabel = 'Save changes'; | |
public function getModelClass() | |
{ | |
throw new \Exception("This form is not configured with a Model Class"); | |
} | |
public function getConfigurationKey() | |
{ | |
$baseClass = (new \ReflectionClass($this))->getShortName(); | |
return $baseClass; | |
} | |
public function buildForm() | |
{ | |
$configurationKey = $this->getConfigurationKey(); | |
$config = Yaml::parse( | |
file_get_contents( | |
app_path() . sprintf('/Forms/Config/%s/%s.yml', $this->folder, $configurationKey) | |
) | |
); | |
foreach ($config as $key => $value) { | |
$this->add($key, $value['type'], $this->replaceValues($value['properties'])); | |
} | |
$this->add('Submit', 'submit', ['label' => $this->saveLabel]); | |
} | |
protected function replaceValues($properties) | |
{ | |
foreach ($properties as $key => $value) { | |
$properties[$key] = $this->replaceValue($key, $value); | |
} | |
return $properties; | |
} | |
protected function replaceValue($key, $value) | |
{ | |
if (is_array($value) && array_key_exists('VALUE_FROM_MODEL', $value)) { | |
$model = $this->getModel(); | |
$key = $value['VALUE_FROM_MODEL']; | |
return $model ? $model->$key : null; | |
} | |
return $this->handleSimpleReplaceValue($value); | |
} | |
protected function handleSimpleReplaceValue($value) | |
{ | |
switch ($value) { | |
case 'FORMATTED_DATE': | |
return function ($value) { | |
return $value ? $value->format('Y-m-d') : null; | |
}; | |
case 'COERCE_ZERO': | |
return function ($value) { | |
return $value ? $value : 0; | |
}; | |
case 'CURRENT_YEAR': | |
return (new Carbon())->year; | |
default: | |
if (!is_string($value)) { | |
return $value; | |
} | |
return $this->handleStringValue($value); | |
} | |
} | |
protected function handleStringValue($value) | |
{ | |
if (self::endsWith($value, '_FORM')) { | |
$method = self::underscoreToCamelCase(strtolower($value)); | |
return $this->$method(); | |
} | |
if (self::endsWith($value, '_OPTIONS')) { | |
return $this->getValueAsOptions($value); | |
} | |
if (self::endsWith($value, '_QUERY_BUILDER')) { | |
$method = sprintf('get%s', self::underscoreToCamelCase(strtolower($value))); | |
$model = $this->getModel(); | |
return $this->$method($model); | |
} | |
return $value; | |
} | |
protected function getValueAsOptions($value) | |
{ | |
$modelClass = $this->getModelClass(); | |
$method = str_replace('_OPTIONS', '', $value); | |
$method = sprintf('get%sOptions', self::underscoreToCamelCase(strtolower($method))); | |
if (method_exists($modelClass, $method)) { | |
return $modelClass::$method(); | |
} | |
if (method_exists(EntityFormConfig::class, $method)) { | |
return EntityFormConfig::$method(); | |
} | |
return FormConfig::$method(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment