Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\LaravelIgnition\Support;
4
 
5
use Illuminate\Support\Collection;
6
use Illuminate\Support\Str;
7
use Livewire\LivewireManager;
8
use ReflectionClass;
9
use ReflectionMethod;
10
use ReflectionProperty;
11
 
12
class LivewireComponentParser
13
{
14
    protected string $componentClass;
15
 
16
    protected ReflectionClass $reflectionClass;
17
 
18
    public static function create(string $componentAlias): self
19
    {
20
        return new self($componentAlias);
21
    }
22
 
23
    public function __construct(protected string $componentAlias)
24
    {
25
        $this->componentClass = app(LivewireManager::class)->getClass($this->componentAlias);
26
        $this->reflectionClass = new ReflectionClass($this->componentClass);
27
    }
28
 
29
    public function getComponentClass(): string
30
    {
31
        return $this->componentClass;
32
    }
33
 
34
    public function getPropertyNamesLike(string $similar): Collection
35
    {
36
        $properties = collect($this->reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC))
37
            // @phpstan-ignore-next-line
38
            ->reject(fn (ReflectionProperty $reflectionProperty) => $reflectionProperty->class !== $this->reflectionClass->name)
39
            ->map(fn (ReflectionProperty $reflectionProperty) => $reflectionProperty->name);
40
 
41
        $computedProperties = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
42
            // @phpstan-ignore-next-line
43
            ->reject(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->class !== $this->reflectionClass->name)
44
            ->filter(fn (ReflectionMethod $reflectionMethod) => str_starts_with($reflectionMethod->name, 'get') && str_ends_with($reflectionMethod->name, 'Property'))
45
            ->map(fn (ReflectionMethod $reflectionMethod) => lcfirst(Str::of($reflectionMethod->name)->after('get')->before('Property')));
46
 
47
        return $this->filterItemsBySimilarity(
48
            $properties->merge($computedProperties),
49
            $similar
50
        );
51
    }
52
 
53
    public function getMethodNamesLike(string $similar): Collection
54
    {
55
        $methods = collect($this->reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
56
            // @phpstan-ignore-next-line
57
            ->reject(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->class !== $this->reflectionClass->name)
58
            ->map(fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->name);
59
 
60
        return $this->filterItemsBySimilarity($methods, $similar);
61
    }
62
 
63
    protected function filterItemsBySimilarity(Collection $items, string $similar): Collection
64
    {
65
        return $items
66
            ->map(function (string $name) use ($similar) {
67
                similar_text($similar, $name, $percentage);
68
 
69
                return ['match' => $percentage, 'value' => $name];
70
            })
71
            ->sortByDesc('match')
72
            ->filter(function (array $item) {
73
                return $item['match'] > 40;
74
            })
75
            ->map(function (array $item) {
76
                return $item['value'];
77
            })
78
            ->values();
79
    }
80
}