Skip to content

Instantly share code, notes, and snippets.

@Dinir
Created December 20, 2017 03:25
Show Gist options
  • Save Dinir/5857b3b47ebcf5df022a05e39fec33c3 to your computer and use it in GitHub Desktop.
Save Dinir/5857b3b47ebcf5df022a05e39fec33c3 to your computer and use it in GitHub Desktop.
you're a terrible coddrererrerewrewrarfawerf
<?php
class FormOption
{
public
$style,
$suggested_value,
$placeholder,
$autofocus;
protected $format = array(
'style' => 'style=":value"',
'autofocus' => 'autofocus'
);
public function __construct($settings_array, $formats_array = null)
{
$this->assignSettings($settings_array);
$this->applyFormats($formats_array);
}
protected function assignSettings($array)
{
foreach ($array as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
}
protected function applyFormats($array)
{
if (!is_null($array)) {
$this->addFormats($array);
}
foreach (get_object_vars($this) as $key => $value) {
if (
is_null($value) ||
!array_key_exists($key, $this->format)
) {
continue;
}
if ($key === 'format') {
continue;
}
$this->{$key} = str_replace(':value', $value, $this->format[$key]);
}
}
protected function addFormats($array)
{
$this->format = array_merge($this->format, $array);
}
}
class FormListOption extends FormOption
{
public $list;
public $item_format = array(
'select' => "<option value=\":value\">:item</option>\n"
);
}
class FormFileOption extends FormOption
{
public
$multiple = true,
$submit_as_array = true;
public function __construct($settings_array)
{
$additional_formats = array(
'muptiple' => 'multiple',
'submit_as_array' => '[]'
);
parent::__construct($settings_array, $additional_formats);
}
}
$subject = new FormOption(array('suggested_value' => 'abcde?', 'autofocus' => true));
$hidden = new FormOption(array('suggested_value' => 'we_are_trying'));
$category = new FormListOption(array(
'suggested_value' => '3',
'list' => array('a' => '1', 'b' => '2', 'c' => '3'),
'style' => 'width: 16em;',
'placeholder' => 'Choose...'
));
$photo = new FormFileOption(array(
'multiple' => false,
'submit_as_array' => false,
'style' => 'width: 16em;'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment