Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LaravelEloquentPlusServiceProvider
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 boot
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 registerValidationRules
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 registerCastAliases
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCastAliases
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace DevToolbelt\LaravelEloquentPlus;
6
7use DevToolbelt\LaravelEloquentPlus\Casts\OnlyNumbers;
8use DevToolbelt\LaravelEloquentPlus\Casts\RemoveSpecialCharacters;
9use DevToolbelt\LaravelEloquentPlus\Casts\UuidToIdCast;
10use DevToolbelt\LaravelEloquentPlus\Validators\CpfCnpjValidator;
11use DevToolbelt\LaravelEloquentPlus\Validators\HexColor;
12use Illuminate\Support\Facades\Validator;
13use Illuminate\Support\ServiceProvider;
14
15/**
16 * Service Provider for Laravel Eloquent Plus package.
17 *
18 * Registers custom validation rules and cast aliases for convenient usage.
19 *
20 * @package DevToolbelt\LaravelEloquentPlus
21 */
22class LaravelEloquentPlusServiceProvider extends ServiceProvider
23{
24    /**
25     * Custom cast type aliases.
26     *
27     * Maps short names to their full cast class names.
28     *
29     * @var array<string, class-string>
30     */
31    public static array $castAliases = [
32        'only_numbers' => OnlyNumbers::class,
33        'remove_special_chars' => RemoveSpecialCharacters::class,
34        'uuid_to_id' => UuidToIdCast::class,
35    ];
36
37    /**
38     * Register any application services.
39     */
40    public function register(): void
41    {
42        $this->mergeConfigFrom(
43            __DIR__ . '/../config/eloquent-plus.php',
44            'devToolbelt.eloquent-plus'
45        );
46    }
47
48    /**
49     * Bootstrap any application services.
50     */
51    public function boot(): void
52    {
53        $this->publishes([
54            __DIR__ . '/../config/eloquent-plus.php' => config_path('devToolbelt/eloquent-plus.php'),
55        ], 'eloquent-plus-config');
56
57        $this->registerValidationRules();
58        $this->registerCastAliases();
59    }
60
61    /**
62     * Register custom validation rules with short aliases.
63     *
64     * After registration, you can use these rules like built-in validators:
65     *
66     * ```php
67     * $rules = [
68     *     'document' => ['required', 'cpf_cnpj'],
69     *     'color' => ['nullable', 'hex_color'],
70     * ];
71     * ```
72     */
73    protected function registerValidationRules(): void
74    {
75        $cpfCnpjValidator = new CpfCnpjValidator();
76        $hexColorValidator = new HexColor();
77
78        // CPF/CNPJ Validator - validates Brazilian CPF or CNPJ documents
79        Validator::extend(
80            'cpf_cnpj',
81            fn($attribute, $value) => $cpfCnpjValidator->passes($value),
82            'The :attribute must be a valid CPF or CNPJ.'
83        );
84
85        // CPF only validator
86        Validator::extend(
87            'cpf',
88            fn($attribute, $value) => $cpfCnpjValidator->passesCpf($value),
89            'The :attribute must be a valid CPF.'
90        );
91
92        // CNPJ only validator
93        Validator::extend(
94            'cnpj',
95            fn($attribute, $value) => $cpfCnpjValidator->passesCnpj($value),
96            'The :attribute must be a valid CNPJ.'
97        );
98
99        // Hex Color validator
100        Validator::extend(
101            'hex_color',
102            fn($attribute, $value) => $hexColorValidator->passes($value),
103            'The :attribute must be a valid hex color.'
104        );
105    }
106
107    /**
108     * Register cast aliases for convenient usage in models.
109     *
110     * After registration, you can use short names in the $casts array:
111     *
112     * ```php
113     * protected $casts = [
114     *     'phone' => 'only_numbers',
115     *     'name' => 'remove_special_chars',
116     *     'category_id' => 'uuid_to_id:categories,external_id',
117     * ];
118     * ```
119     */
120    protected function registerCastAliases(): void
121    {
122        // Register cast aliases by extending the Model's cast types
123        // This uses Laravel's castAttribute method override approach
124        ModelBase::registerCastAliases(self::$castAliases);
125    }
126
127    /**
128     * Get all registered cast aliases.
129     *
130     * @return array<string, class-string>
131     */
132    public static function getCastAliases(): array
133    {
134        return self::$castAliases;
135    }
136}