Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
LaravelEloquentPlusServiceProvider
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 boot
100.00% covered (success)
100.00%
2 / 2
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        //
43    }
44
45    /**
46     * Bootstrap any application services.
47     */
48    public function boot(): void
49    {
50        $this->registerValidationRules();
51        $this->registerCastAliases();
52    }
53
54    /**
55     * Register custom validation rules with short aliases.
56     *
57     * After registration, you can use these rules like built-in validators:
58     *
59     * ```php
60     * $rules = [
61     *     'document' => ['required', 'cpf_cnpj'],
62     *     'color' => ['nullable', 'hex_color'],
63     * ];
64     * ```
65     */
66    protected function registerValidationRules(): void
67    {
68        $cpfCnpjValidator = new CpfCnpjValidator();
69        $hexColorValidator = new HexColor();
70
71        // CPF/CNPJ Validator - validates Brazilian CPF or CNPJ documents
72        Validator::extend(
73            'cpf_cnpj',
74            fn($attribute, $value) => $cpfCnpjValidator->passes($value),
75            'The :attribute must be a valid CPF or CNPJ.'
76        );
77
78        // CPF only validator
79        Validator::extend(
80            'cpf',
81            fn($attribute, $value) => $cpfCnpjValidator->passesCpf($value),
82            'The :attribute must be a valid CPF.'
83        );
84
85        // CNPJ only validator
86        Validator::extend(
87            'cnpj',
88            fn($attribute, $value) => $cpfCnpjValidator->passesCnpj($value),
89            'The :attribute must be a valid CNPJ.'
90        );
91
92        // Hex Color validator
93        Validator::extend(
94            'hex_color',
95            fn($attribute, $value) => $hexColorValidator->passes($value),
96            'The :attribute must be a valid hex color.'
97        );
98    }
99
100    /**
101     * Register cast aliases for convenient usage in models.
102     *
103     * After registration, you can use short names in the $casts array:
104     *
105     * ```php
106     * protected $casts = [
107     *     'phone' => 'only_numbers',
108     *     'name' => 'remove_special_chars',
109     *     'category_id' => 'uuid_to_id:categories,external_id',
110     * ];
111     * ```
112     */
113    protected function registerCastAliases(): void
114    {
115        // Register cast aliases by extending the Model's cast types
116        // This uses Laravel's castAttribute method override approach
117        ModelBase::registerCastAliases(self::$castAliases);
118    }
119
120    /**
121     * Get all registered cast aliases.
122     *
123     * @return array<string, class-string>
124     */
125    public static function getCastAliases(): array
126    {
127        return self::$castAliases;
128    }
129}