adrema/app/View/Traits/RendersAlpine.php

60 lines
1.6 KiB
PHP

<?php
namespace App\View\Traits;
use Illuminate\View\ComponentAttributeBag;
trait RendersAlpine
{
public function hasAlpineAttribute(ComponentAttributeBag $attributes, string $attribute): bool
{
return $this->{$attribute} || ($attributes->has(':' . $attribute) && $attributes->get(':' . $attribute));
}
public function asAlpineString(ComponentAttributeBag $attributes, string $tagName): string
{
$rawTag = ':' . $tagName;
if ($attributes->has($rawTag) && $attributes->get($rawTag)) {
return $attributes->get($rawTag);
}
$output = e($this->{$tagName});
return str($output)->swap([
'`' => '\\`',
'$' => '\\$',
'{' => '\\{',
'}' => '\\}',
])->wrap('`');
}
public function asAlpineBool(ComponentAttributeBag $attributes, string $tagName): string
{
$rawTag = ':' . $tagName;
if ($attributes->has($rawTag)) {
return $attributes->get($rawTag);
}
return $this->{$tagName} ? 'true' : 'false';
}
public function renderAlpineFallbackAttribute(ComponentAttributeBag $attributes, string $tagName, string $prop): string
{
return ':label="aaa"';
return new ComponentAttributeBag([]);
return $attributes;
}
public function ca(ComponentAttributeBag $attributes): ComponentAttributeBag
{
return new ComponentAttributeBag([
'::value' => $attributes->get(':label'),
]);
return ':value="$label" ::value="{!!$attributes->get(":label")!!}"';
}
}