Initial commit

This commit is contained in:
2018-08-23 16:44:53 +02:00
commit 1f06564778
115 changed files with 13984 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Base;
use Magdev\Dossier\Service\ConfigService;
use Magdev\Dossier\Service\TranslatorService;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\PersonFormBuilder;
use Magdev\Dossier\Form\Field\TextField;
use Droath\ConsoleForm\FieldGroup;
abstract class BaseFormBuilder implements FormBuilderInterface
{
protected $flatGroupFields = array();
/**
* Configuration service
* @var \Magdev\Dossier\Service\ConfigService
*/
protected $config = null;
/**
* Translation service
* @var \Magdev\Dossier\Service\TranslatorService
*/
protected $translator = null;
/**
* Get the configuration service
*
* @return \Magdev\Dossier\Service\ConfigService
*/
public function getConfig(): ConfigService
{
return $this->config;
}
/**
* Set the configuration service
*
* @param \Magdev\Dossier\Service\ConfigService $config
* @return \Magdev\Dossier\Form\Base\FormBuilderInterface
*/
public function setConfig(ConfigService $config): FormBuilderInterface
{
$this->config = $config;
return $this;
}
/**
* Get the translator service
*
* @return \Magdev\Dossier\Service\TranslatorService
*/
public function getTranslator(): TranslatorService
{
return $this->translator;
}
/**
* Set the translator service
*
* @param \Magdev\Dossier\Service\TranslatorService $translator
* @return \Magdev\Dossier\Form\Base\FormBuilderInterface
*/
public function setTranslator(TranslatorService $translator): FormBuilderInterface
{
$this->translator = $translator;
return $this;
}
/**
* Get the flatten callback
*
* @return callable
*/
protected function getFlattenCallback(): callable
{
$flatGroupFields = $this->flatGroupFields;
return function(array $results, Form $form) use ($flatGroupFields){
foreach ($flatGroupFields as $field) {
if (isset($results[$field]) && is_array($results[$field])) {
array_walk($results[$field], function(&$value, $key) {
if (is_array($value)) {
$value = (string) array_shift($value);
}
});
}
}
return $results;
};
}
/**
* Get the unflatten callback
*
* @return callable
*/
protected function getUnflattenCallback(): callable
{
$flatGroupFields = $this->flatGroupFields;
return function(array $results, Form $form) use ($flatGroupFields){
foreach ($flatGroupFields as $field) {
if (isset($results[$field]) && is_array($results[$field])) {
array_walk($results[$field], function(&$value, $key) {
if (!is_array($value)) {
$value = array($value);
}
});
}
}
return $results;
};
}
/**
* Add a flat group array for single line array
*
* @param \Magdev\Dossier\Form\Extension\Form $form
* @param string $key
* @param bool $required
* @param string $langPrefix
* @return \Magdev\Dossier\Form\Base\PersonFormBuilder
*/
protected function addFlatGroupField(Form $form, string $key, bool $required = true, string $langPrefix = ''): FormBuilderInterface
{
$name = substr($key, 0, -1);
$label = 'form.';
if ($langPrefix) {
$label .= $langPrefix.'.';
}
$label .= $key;
$this->flatGroupFields[] = $key;
$form->addField((new FieldGroup($key))
->addFields(array(
(new TextField($name, $this->translator->trans($label), false)),
))->setLoopUntil(function($result) use ($name) {
if (!isset($result[$name]) || empty($result[$name])) {
return false;
}
return true;
}));
return $this;
}
}

View File

@@ -0,0 +1,82 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Base;
/**
* Interface for elements with decorable label
*
* @author magdev
*/
interface DecoratableLabelInterface
{
/**
* Set a label prefix
*
* @param string $prefix
* @return \Magdev\Dossier\Form\Base\DecoratableLabelInterface
*/
public function setLabelPrefix(string $prefix): DecoratableLabelInterface;
/**
* Set the fixed length of the label
*
* @param int $length
* @return \Magdev\Dossier\Form\Base\DecoratableLabelInterface
*/
public function setLabelLength(int $length): DecoratableLabelInterface;
/**
* Get the label prefix
*
* @return string
*/
public function getLabelPrefix(): string;
/**
* Get the label length
*
* @return int
*/
public function getLabelLength(): int;
/**
* Get the decorated label
*
* @param string $label
* @return string
*/
public function decorateLabel(string $label): string;
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Base;
use Magdev\Dossier\Service\ConfigService;
use Magdev\Dossier\Service\TranslatorService;
use Droath\ConsoleForm\FormInterface;
interface FormBuilderInterface extends FormInterface
{
/**
* Get the configuration service
*
* @return \Magdev\Dossier\Service\ConfigService
*/
public function getConfig(): ConfigService;
/**
* Set the configuration service
*
* @param \Magdev\Dossier\Service\ConfigService $config
* @return \Magdev\Dossier\Form\Base\FormBuilderInterface
*/
public function setConfig(ConfigService $config): FormBuilderInterface;
/**
* Get the translator service
*
* @return \Magdev\Dossier\Service\TranslatorService
*/
public function getTranslator(): TranslatorService;
/**
* Set the translator service
*
* @param \Magdev\Dossier\Service\TranslatorService $translator
* @return \Magdev\Dossier\Form\Base\FormBuilderInterface
*/
public function setTranslator(TranslatorService $translator): FormBuilderInterface;
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Base\BaseFormBuilder;
use Magdev\Dossier\Form\Base\FormBuilderInterface;
use Magdev\Dossier\Form\Field\TextField;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Field\BooleanField;
class EmailFormBuilder extends BaseFormBuilder implements FormBuilderInterface
{
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::getName()
*/
public function getName()
{
return 'form.email';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::buildForm()
*/
public function buildForm()
{
$form = new Form();
$form->setLabelPrefix($this->config->get('form.label.prefix'))
->setLabelLength($this->config->get('form.label.length'));
$form->addField(new TextField('company', $this->translator->trans('form.email.company')));
$form->addField((new SelectField('salutation', $this->translator->trans('form.email.salutation')))
->setOptions(array(
$this->translator->trans('form.email.salutation.female'),
$this->translator->trans('form.email.salutation.male'),
))
->setRequired(true));
$form->addField(new TextField('firstname', $this->translator->trans('form.email.firstname')));
$form->addField(new TextField('lastname', $this->translator->trans('form.email.lastname')));
$form->addField(new TextField('email', $this->translator->trans('form.email.email')));
$form->addField(new TextField('offer_title', $this->translator->trans('form.email.offer_title')));
$form->addField(new TextField('offer_link', $this->translator->trans('form.email.offer_link'), false));
$form->addField((new BooleanField('add_subject', $this->translator->trans('form.email.add_subject')))
->setSubform(function($subform, $value) {
if ($value === true) {
$form->addField(new TextField('subject', $this->translator->trans('form.email.subject')));
}
}));
return $form;
}
}

View File

@@ -0,0 +1,85 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Field\TextField;
use Magdev\Dossier\Form\Field\BooleanField;
/**
* EntryFormBuilder
*
* @author magdev
*/
class EntryFormBuilder extends Base\BaseFormBuilder implements Base\FormBuilderInterface
{
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::getName()
*/
public function getName()
{
return 'form.cv';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::buildForm()
*/
public function buildForm()
{
$form = new Form();
$form->setLabelPrefix($this->config->get('form.label.prefix'))
->setLabelLength($this->config->get('form.label.length'))
->addFormLoadCallback($this->getUnflattenCallback())
->addFormResultsCallback($this->getFlattenCallback());
$form->addField(new TextField('start_date', $this->translator->trans('form.cv.start_date')));
$form->addField(new TextField('end_date', $this->translator->trans('form.cv.end_date')));
$form->addField(new TextField('position', $this->translator->trans('form.cv.position')));
$form->addField(new TextField('company', $this->translator->trans('form.cv.company')));
$form->addField((new SelectField('tag', $this->translator->trans('form.cv.tag')))
->setOptions($this->config->get('cv.tags'))
->setDefault($this->config->get('cv.default_tag'))
->setRequired(true));
$form->addField(new TextField('industry', $this->translator->trans('form.cv.industry')));
$form->addField(new TextField('qualification', $this->translator->trans('form.cv.qualification'), false));
$this->addFlatGroupField($form, 'skills', false, 'cv')
->addFlatGroupField($form, 'achievements', false, 'cv')
->addFlatGroupField($form, 'toolbox', false, 'cv');
$form->addField(new TextField('text', $this->translator->trans('form.cv.text'), false));
$form->addField(new BooleanField('use_in_resume', $this->translator->trans('form.cv.use_in_resume')));
return $form;
}
}

View File

@@ -0,0 +1,121 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Extension;
use Droath\ConsoleForm\Field\Field as BaseField;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Base\DecoratableLabelInterface;
use Magdev\Dossier\Form\Traits\DecoratableLabelTrait;
/**
* Field extension to use multiselect option on SelectField
*
* @author magdev
*/
class Field extends BaseField implements DecoratableLabelInterface
{
use DecoratableLabelTrait;
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\Field::setCondition()
*/
public function setCondition($field_name, $value, $operation = '=')
{
if (is_callable($value)) {
$this->condition[$field_name] = $value;
return $this;
}
$this->condition[$field_name] = [
'value' => $value,
'operation' => $operation
];
return $this;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\Field::asQuestion()
*/
public function asQuestion()
{
$instance = $this->questionClassInstance()
->setMaxAttempts($this->maxAttempt);
if ($this->hidden) {
$instance->setHidden(true);
}
if ($this instanceof SelectField) {
$instance->setMultiselect($this->isMultiSelect());
}
if ($this->isRequire()) {
$this->setValidation(function ($answer) {
if ($answer == '' && $this->dataType() !== 'boolean') {
throw new \Exception('Field is required.');
}
});
}
$instance->setValidator(function ($answer) {
foreach ($this->validation as $callback) {
if (!is_callable($callback)) {
continue;
}
$callback($answer);
}
return $answer;
});
if (isset($this->normalizer) && is_callable($this->normalizer)) {
$instance->setNormalizer($this->normalizer);
}
return $instance;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\Field::formattedLabel()
*/
protected function formattedLabel()
{
$label[] = '<fg=cyan;options=bold>'.$this->decorateLabel($this->label, (string) $this->default, '[*]').'</> ';
$label[] = isset($this->default) ? "<fg=yellow;options=bold>[$this->default]</> " : '';
$label[] = $this->isRequire() ? '<fg=red;options=bold>[*]</>: ' : ': ';
return implode('', $label);
}
}

339
src/Form/Extension/Form.php Normal file
View File

@@ -0,0 +1,339 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Extension;
use Droath\ConsoleForm\Form as BaseForm;
use Droath\ConsoleForm\Field\Field;
use Droath\ConsoleForm\Exception\FormException;
use Droath\ConsoleForm\FieldDefinitionInterface;
use Droath\ConsoleForm\Field\FieldInterface;
use Magdev\Dossier\Form\Base\DecoratableLabelInterface;
use Magdev\Dossier\Form\Traits\DecoratableLabelTrait;
/**
* Base Form class
*
* @author magdev
*/
class Form extends BaseForm implements DecoratableLabelInterface
{
use DecoratableLabelTrait;
/**
* Callback to post-process form results
* @var array
*/
protected $formResultsCallbacks = array();
/**
* Callback to pre-process data on load
* @var array
*/
protected $formLoadCallbacks = array();
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Form::addField()
*/
public function addField(FieldDefinitionInterface $field)
{
if ($field instanceof DecoratableLabelInterface) {
$field->setLabelLength($this->getLabelLength())
->setLabelPrefix($this->getLabelPrefix());
}
$this->fields[] = $field;
return $this;
}
/**
* Get a field by name
*
* @param string $name
* @throws \Droath\ConsoleForm\Exception\FormException
* @return \Droath\ConsoleForm\FieldDefinitionInterface
*/
public function getField(string $name): FieldDefinitionInterface
{
foreach ($this->fields as $field) {
if ($field->getName() == $name) {
return $field;
}
}
throw new FormException('Field '.$field.' not found');
}
/**
* Check if a field exists
*
* @param string $name
* @return bool
*/
public function hasField(string $name): bool
{
try {
$this->getField($name);
return true;
} catch (FormException $e) {
return false;
}
}
/**
* Set default values
*
* @param array $defaults
* @return \Magdev\Dossier\Form\Extension\Form
*/
public function setDefaults(array $defaults): Form
{
if ($this->hasFormLoadCallbacks()) {
$defaults = $this->onFormLoad($defaults);
}
foreach ($defaults as $field => $value) {
if ($this->hasField($field)) {
$field = $this->getField($field)->setDefault($value);
}
}
return $this;
}
/**
* Check if the is already processed
*
* @return bool
*/
public function isProcessed(): bool
{
return sizeof($this->results) > 0 ? true : false;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Form::process()
*/
public function process()
{
if (!$this->isProcessed()) {
$this->results = $this->processFields($this->fields, $this->results);
if ($this->hasFormResultsCallbacks()) {
$this->results = $this->onFormResults($this->results);
}
}
return $this;
}
/**
* Ceck if a data value is set
*
* @param string $name
* @throws \Droath\ConsoleForm\Exception\FormException
* @return bool
*/
public function hasData(string $name): bool
{
if (!$this->isProcessed()) {
throw new FormException('Please process the form first');
}
return isset($this->results[$name]) && !empty($this->results[$name]) ? true : false;
}
/**
* Get a value from result and optional delete the field
*
* @param string $name
* @param bool $unset
* @throws \Droath\ConsoleForm\Exception\FormException
* @return string|bool|array
*/
public function getData(string $name = null, bool $unset = false, $default = null)
{
if (!$this->isProcessed()) {
throw new FormException('Please process the form first');
}
if (is_null($name)) {
return $this->results;
}
$value = $default;
if ($this->hasData($name)) {
$value = $this->results[$name];
if ($unset) {
unset($this->results[$name]);
}
}
return $value;
}
/**
* Get a value and strip it from results
*
* @param string $key
* @param string|boolean|array|null $default
* @return string|boolean|array
*/
public function stripData(string $key, $default = null)
{
return $this->hasData($key) ? $this->getData($key, true) : $default;
}
/**
* Check if the form has result callbacks
*
* @return bool
*/
public function hasFormResultsCallbacks(): bool
{
return sizeof($this->formResultsCallbacks) > 0;
}
/**
* Add a callback to perform on results after processing
*
* @param callable $function
* @return \Magdev\Dossier\Form\Extension\Form
*/
public function addFormResultsCallback(callable $callback): Form
{
$this->formResultsCallbacks[] = $callback;
return $this;
}
/**
* Form results callback handler
*
* @param array $results
* @return array
*/
public function onFormResults(array $results): array
{
foreach ($this->formResultsCallbacks as $callback) {
$results = call_user_func_array($callback, array($results, $this));
}
return $results;
}
/**
* Check if the form has load callbacks
*
* @return bool
*/
public function hasFormLoadCallbacks(): bool
{
return sizeof($this->formLoadCallbacks) > 0;
}
/**
* Add a callback to perform on results after processing
*
* @param callable $function
* @return \Magdev\Dossier\Form\Extension\Form
*/
public function addFormLoadCallback(callable $callback): Form
{
$this->formLoadCallbacks[] = $callback;
return $this;
}
/**
* Form results callback handler
*
* @param array $results
* @return array
*/
public function onFormLoad(array $results): arry
{
foreach ($this->formLoadCallbacks as $callback) {
$results = call_user_func_array($callback, array($results, $this));
}
return $results;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Form::fieldConditionMet()
*/
protected function fieldConditionMet(FieldInterface $field, array $results)
{
$conditions = [];
foreach ($field->getCondition() as $field_name => $condition) {
$field_value = $this->getFieldValue($field_name, $results);
if (is_callable($condition)) {
$conditions[] = call_user_func_array($condition, array($field_value, $results, $this));
} else {
if (is_array($field_value) && key_exists($field_name, $field_value)) {
$field_value = $field_value[$field_name];
}
$value = $condition['value'];
$operation = $condition['operation'];
switch ($operation) {
case "!=":
$conditions[] = $field_value != $value;
break;
case '=':
default:
$conditions[] = $field_value == $value;
break;
}
}
}
$conditions = array_unique($conditions);
return sizeof($conditions) === 1 ? reset($conditions) : false;
}
}

View File

@@ -0,0 +1,97 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Droath\ConsoleForm\FormDiscovery as BaseFormDiscovery;
use Droath\ConsoleForm\FormInterface;
use Magdev\Dossier\Form\Base\FormBuilderInterface;
class FormDiscovery extends BaseFormDiscovery
{
/**
* Container
* @var \Symfony\Component\DependencyInjection\ContainerBuilder
*/
protected $container = null;
/**
* Constructor
*
* @param string $depth
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function __construct(ContainerBuilder $container, $depth = '< 3')
{
parent::__construct($depth);
$this->container = $container;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormDiscovery::doDiscovery()
*/
protected function doDiscovery(array $directories, $base_namespace)
{
$forms = [];
$filter = $this->filterByNamespace($base_namespace);
$config = $this->container->get('config');
$translator = $this->container->get('translator');
foreach ($this->searchFiles($directories, $filter) as $file) {
$ext = $file->getExtension();
$classname = $base_namespace . '\\' . $file->getBasename(".$ext");
if (!class_exists($classname)) {
throw new \Exception('Missing class found during form discovery. '.$classname);
}
$instance = new $classname();
/* @var $instance \Magdev\Dossier\Form\Base\FormBuilderInterface */
if (!$instance instanceof FormInterface) {
throw new \Exception(sprintf('Form class (%s) is missing \Droath\ConsoleForm\Form\FormInterface.', $classname));
}
if ($instance instanceof FormBuilderInterface) {
$forms[$instance->getName()] = $instance->setConfig($config)
->setTranslator($translator)
->buildForm();
} else {
$forms[$instance->getName()] = $instance->buildForm();
}
}
return $forms;
}
}

View File

@@ -0,0 +1,71 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Field;
use Droath\ConsoleForm\Field\BooleanField as BaseBooleanField;
use Magdev\Dossier\Form\Base\DecoratableLabelInterface;
use Magdev\Dossier\Form\Traits\DecoratableLabelTrait;
/**
* Override BooleanField
*
* @author magdev
*/
class BooleanField extends BaseBooleanField implements DecoratableLabelInterface
{
use DecoratableLabelTrait;
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\BooleanField::formattedValue()
*/
public function formattedValue($value)
{
return $value === true ? true : false;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\BooleanField::formattedLabel()
*/
protected function formattedLabel()
{
// Convert boolean default into a readable format.
$default = $this->default ? 'yes' : 'no';
$l = $this->decorateLabel($this->label);
$label[] = "<fg=cyan;options=bold>'.$l.'</>";
$label[] = isset($default) ? "<fg=yellow>[$default]</>" : '';
return implode(' ', $label);
}
}

View File

@@ -0,0 +1,100 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Field;
use Droath\ConsoleForm\Field\SelectField as BaseSelectField;
use Magdev\Dossier\Form\Base\DecoratableLabelInterface;
use Magdev\Dossier\Form\Traits\DecoratableLabelTrait;
/**
* Extension for Field class to allow multiselects
*
* @author magdev
*/
class SelectField extends BaseSelectField implements DecoratableLabelInterface
{
use DecoratableLabelTrait;
/**
* Multiselect status
* @var bool
*/
protected $multiSelect = false;
/**
* Set field to multiselect
*
* @param bool $multiSelect
* @return \Magdev\Dossier\Form\Field\SelectField
*/
public function setMultiSelect(bool $multiSelect = true): SelectField
{
$this->multiSelect = $multiSelect;
return $this;
}
/**
* Checkif field is multiselect
*
* @return bool
*/
public function isMultiSelect(): bool
{
return $this->multiSelect;
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\SelectField::dataType()
*/
public function dataType()
{
return $this->isMultiSelect() ? 'array' : 'string';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\Field\Field::formattedLabel()
*/
protected function formattedLabel()
{
$label[] = '<fg=cyan;options=bold>'.$this->decorateLabel($this->label).'</>';
$label[] = isset($this->default) ? '<fg=yellow;options=bold>['.$this->default.']</>' : '';
$label[] = $this->isRequire() ? '<fg=red;options=bold>[*]</>: ' : ': ';
return implode(' ', $label);
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace Magdev\Dossier\Form\Field;
use Droath\ConsoleForm\Field\FieldInterface;
use Droath\ConsoleForm\Exception\FormException;
use Magdev\Dossier\Form\Extension\Field;
/**
* Define the text field as input via vim.
*/
class TextField extends Field implements FieldInterface
{
/**
* {@inheritdoc}
*/
public function dataType()
{
return 'string';
}
/**
* {@inheritdoc}
*/
public function questionClassArgs()
{
return [$this->formattedLabel(), $this->default];
}
/**
* {@inheritdoc}
*/
public function questionClass()
{
return '\Symfony\Component\Console\Question\Question';
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Field\TextField;
use Magdev\Dossier\Form\Field\BooleanField;
/**
* IntroFormBuilder
*
* @author magdev
*/
class InitFormBuilder extends Base\BaseFormBuilder implements Base\FormBuilderInterface
{
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::getName()
*/
public function getName()
{
return 'form.init';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::buildForm()
*/
public function buildForm()
{
$form = new Form();
$form->setLabelPrefix($this->config->get('form.label.prefix'))
->setLabelLength($this->config->get('form.label.length'));
$form->addField((new BooleanField('use_userstyles', $this->translator->trans('form.init.use_userstyles')))
->setSubform(function($subform, $value) {
if ($value === true) {
$subform->addFields(array(
(new SelectField('userstyle_type', $this->translator->trans('form.init.userstyle_type')))
->setOptions(array('SCSS', 'LESS'))
->setDefault('SCSS')
->setRequired(true)
));
}
}));
return $form;
}
}

View File

@@ -0,0 +1,92 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Field\TextField;
use Magdev\Dossier\Form\Field\BooleanField;
/**
* IntroFormBuilder
*
* @author magdev
*/
class IntroFormBuilder extends Base\BaseFormBuilder implements Base\FormBuilderInterface
{
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::getName()
*/
public function getName()
{
return 'form.intro';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::buildForm()
*/
public function buildForm()
{
$form = new Form();
$form->setLabelPrefix($this->config->get('form.label.prefix'))
->setLabelLength($this->config->get('form.label.length'));
$form->addField(new TextField('text', $this->translator->trans('form.intro.text'), false));
$form->addField((new BooleanField('addquotes', $this->translator->trans('form.intro.add_quotes')))
->setSubform(function($subform, $value) {
if ($value === true) {
$subform->addField((new FieldGroup('quotes'))
->addFields(array(
(new TextField('quoteText', $this->translator->trans('form.intro.quote_text'), false)),
(new TextField('quoteAuthor', $this->translator->trans('form.intro.quote_author')))
->setCondition('quoteText', '', '!=')
))->setLoopUntil(function($result) {
if (!isset($result['quoteText']) || empty($result['quoteText'])) {
return false;
}
return true;
}));
$subform->addFeld((new SelectField('show_quotes', $this->translator->trans('form.intro.show_quotes')))
->setOptions(array('top', 'bottom'))
->setDefault('bottom')
->setCondition('quotes', function($value, $results, $f) {
return is_array($value) && sizeof($value) > 0 ? true : false;
}));
}
}));
return $form;
}
}

View File

@@ -0,0 +1,105 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form;
use Droath\ConsoleForm\FieldGroup;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Field\SelectField;
use Magdev\Dossier\Form\Field\TextField;
use Magdev\Dossier\Form\Field\BooleanField;
/**
* IntroFormBuilder
*
* @author magdev
*/
class PersonFormBuilder extends Base\BaseFormBuilder implements Base\FormBuilderInterface
{
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::getName()
*/
public function getName()
{
return 'form.person';
}
/**
* {@inheritDoc}
* @see \Droath\ConsoleForm\FormInterface::buildForm()
*/
public function buildForm()
{
$form = new Form();
$form->setLabelPrefix($this->config->get('form.label.prefix'))
->setLabelLength($this->config->get('form.label.length'))
->addFormLoadCallback($this->getUnflattenCallback())
->addFormResultsCallback($this->getFlattenCallback());
$form->addField(new TextField('firstame', $this->translator->trans('form.person.firstname')));
$form->addField(new TextField('lastame', $this->translator->trans('form.person.lastname')));
$form->addField(new TextField('tagline', $this->translator->trans('form.person.tagline'), false));
$form->addField(new TextField('nationality', $this->translator->trans('form.person.nationality'), false));
$form->addField(new TextField('birthdate', $this->translator->trans('form.person.birthdate'), false));
$form->addField((new TextField('birthplace', $this->translator->trans('form.person.birthplace'), false))
->setCondition('birthdate', '', '!='));
$form->addField(new TextField('residence', $this->translator->trans('form.person.residence'), false));
$form->addField(new TextField('status', $this->translator->trans('form.person.status'), false));
$form->addField(new TextField('work_license', $this->translator->trans('form.person.work_license'), false));
$form->addField((new BooleanField('addlang', $this->translator->trans('form.person.add_languages')))
->setSubform(function($subform, $value) {
if ($value === true) {
$subform->addField((new FieldGroup('languages'))
->addFields(array(
(new TextField('language', $this->translator->trans('form.person.language'), false)),
(new SelectField('level', $this->translator->trans('form.person.level')))
->setOptions($this->config->get('language.levels'))
->setDefault($this->config->get('language.default_level'))
->setRequired(true)
->setCondition('language', '', '!=')
))->setLoopUntil(function($result) {
if (!isset($result['language']) || empty($result['language'])) {
return false;
}
return true;
}));
}
}));
$this->addFlatGroupField($form, 'skills', false, 'person')
->addFlatGroupField($form, 'projects', false, 'person')
->addFlatGroupField($form, 'links', false, 'person')
->addFlatGroupField($form, 'interests', false, 'person');
$form->addField(new TextField('text', $this->translator->trans('form.person.text'), false));
return $form;
}
}

View File

@@ -0,0 +1,129 @@
<?php
/**
* The MIT License (MIT)
*
* Copyright (c) 2018 magdev
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author magdev
* @copyright 2018 Marco Grätsch
* @package magdev/dossier
* @license http://opensource.org/licenses/MIT MIT License
*/
namespace Magdev\Dossier\Form\Traits;
use Magdev\Dossier\Form\Extension\Form;
use Magdev\Dossier\Form\Base\DecoratableLabelInterface;
/**
* Trait for classes with decoratable labels
*
* @author magdev
*/
trait DecoratableLabelTrait
{
/**
* Label prefix
* @var string
*/
protected $labelPrefix = '';
/**
* Label length
* @var int
*/
protected $labelLength = -1;
/**
* Set a label prefix
*
* @param string $prefix
* @return \Magdev\Dossier\Form\Base\DecoratableLabelInterface
*/
public function setLabelPrefix(string $prefix): DecoratableLabelInterface
{
$this->labelPrefix = $prefix;
return $this;
}
/**
* Set a label fixed length
*
* @param int $length
* @return \Magdev\Dossier\Form\Base\DecoratableLabelInterface
*/
public function setLabelLength(int $length): DecoratableLabelInterface
{
$this->labelLength = $length;
return $this;
}
/**
* Get the label prefix
*
* @return string
*/
public function getLabelPrefix(): string
{
return $this->labelPrefix;
}
/**
* Get the label length
*
* @return int
*/
public function getLabelLength(): int
{
return $this->labelLength;
}
/**
* Get the decorated label
*
* @param string $label
* @return string
*/
public function decorateLabel(string $label, string $default = '', string $required = ''): string
{
$labelLength = strlen($label);
$defaultLength = (strlen($default) + 2);
$requiredLength = strlen($required);
if ($requiredLength > 0) {
$requiredLength++;
}
$length = $this->labelLength - ($defaultLength + $requiredLength);
$l = $this->labelPrefix;
if ($this->labelLength > 0) {
$l .= str_pad($label, $length, ' ', STR_PAD_RIGHT);
} else {
$l .= $label;
}
return $l;
}
}