openstamanager/app/View/Components/InputWrapper.php

105 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace App\View\Components;
2021-03-12 14:54:10 +01:00
use Illuminate\Support\Collection;
use Illuminate\View\Component;
class InputWrapper extends Component
{
2021-03-12 14:54:10 +01:00
public $props;
/**
* Create a new component instance.
*
* @param string $name
2021-03-12 14:54:10 +01:00
* @param string|null $id
* @param string|null $value
* @param bool|string $required
* @param string|null $label
* @param string|null $placeholder
*/
public function __construct(
$name,
$id = null,
2021-03-12 14:54:10 +01:00
$value = null,
$required = false,
$label = null,
$placeholder = null
) {
2021-03-12 14:54:10 +01:00
// Definizione ID dell'elemento
$id = isset($id) ? $id : $name;
$rand = rand(0, 9999);
2021-03-12 14:54:10 +01:00
$unique_id = $id.$rand;
2021-03-12 14:54:10 +01:00
// Elemento obbligatorio o meno
$required = is_string($required) ? $required == 'true' : (bool) $required;
// Label e placeholder corretti in base al contenuti obbligatorio o meno
2021-03-12 14:54:10 +01:00
if ($required) {
if (!empty($label)) {
$label .= '*';
}
// Aggiunta
2021-03-12 14:54:10 +01:00
elseif (!empty($placeholder)) {
$placeholder .= '*';
}
}
2021-03-12 14:54:10 +01:00
$this->props = $this->newAttributeBag([
'name' => $name,
'id' => $id,
'value' => $value,
'unique_id' => $unique_id,
'required' => $required,
'label' => $label,
'placeholder' => $placeholder,
'class' => collect(['form-control', 'openstamanager-input']),
]);
// Operazioni finali
$this->init();
}
public function get($key, $default = null)
{
return $this->props->get($key, $default);
}
public function set($values)
{
$this->props->setAttributes(array_merge($this->props->getAttributes(), $values));
}
public function init()
{
}
/**
* Extract the public properties for the component.
*
* @return array
*/
public function extractPublicProperties()
{
$values = parent::extractPublicProperties();
foreach ($this->props as $key => $value) {
$values[$key] = $value instanceof Collection ? $value->join(' ') : $value;
}
return $values;
}
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('components.input-wrapper');
}
}