2021-03-12 13:41:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\View\Components;
|
|
|
|
|
2021-03-12 14:54:10 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2021-03-12 13:41:32 +01:00
|
|
|
use Illuminate\View\Component;
|
|
|
|
|
2021-03-15 14:48:56 +01:00
|
|
|
class InputWrapper extends Component
|
2021-03-12 13:41:32 +01:00
|
|
|
{
|
2021-03-12 14:54:10 +01:00
|
|
|
public $props;
|
2021-03-12 13:41:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new component instance.
|
|
|
|
*
|
2021-03-13 14:13:19 +01:00
|
|
|
* @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
|
2021-03-12 13:41:32 +01:00
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$name,
|
|
|
|
$id = null,
|
2021-03-12 14:54:10 +01:00
|
|
|
$value = null,
|
2021-03-12 13:41:32 +01:00
|
|
|
$required = false,
|
|
|
|
$label = null,
|
|
|
|
$placeholder = null
|
|
|
|
) {
|
2021-03-12 14:54:10 +01:00
|
|
|
// Definizione ID dell'elemento
|
|
|
|
$id = isset($id) ? $id : $name;
|
2021-03-12 13:41:32 +01:00
|
|
|
$rand = rand(0, 9999);
|
2021-03-12 14:54:10 +01:00
|
|
|
$unique_id = $id.$rand;
|
2021-03-12 13:41:32 +01:00
|
|
|
|
2021-03-12 14:54:10 +01:00
|
|
|
// Elemento obbligatorio o meno
|
|
|
|
$required = is_string($required) ? $required == 'true' : (bool) $required;
|
2021-03-12 13:41:32 +01:00
|
|
|
|
|
|
|
// 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 .= '*';
|
2021-03-12 13:41:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Aggiunta
|
2021-03-12 14:54:10 +01:00
|
|
|
elseif (!empty($placeholder)) {
|
|
|
|
$placeholder .= '*';
|
2021-03-12 13:41:32 +01:00
|
|
|
}
|
|
|
|
}
|
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;
|
2021-03-12 13:41:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the view / contents that represent the component.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Contracts\View\View|string
|
|
|
|
*/
|
|
|
|
public function render()
|
|
|
|
{
|
2021-03-15 14:48:56 +01:00
|
|
|
return view('components.input-wrapper');
|
2021-03-12 13:41:32 +01:00
|
|
|
}
|
|
|
|
}
|