• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sdkconsultoria / whatsapp-cloud-api / #57

27 Mar 2024 04:48AM UTC coverage: 96.499% (-0.6%) from 97.147%
#57

push

hellsythe
Added header media validation

689 of 714 relevant lines covered (96.5%)

5.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.37
src/Http/Requests/SendTemplateRequest.php
1
<?php
2

3
namespace Sdkconsultoria\WhatsappCloudApi\Http\Requests;
4

5
use Illuminate\Foundation\Http\FormRequest;
6
use Sdkconsultoria\WhatsappCloudApi\Models\Template;
7

8
class SendTemplateRequest extends FormRequest
9
{
10
    /**
11
     * Determine if the user is authorized to make this request.
12
     */
13
    public function authorize(): bool
14
    {
15
        return true;
5✔
16
    }
17

18
    /**
19
     * Get the validation rules that apply to the request.
20
     *
21
     * @return array<string, mixed>
22
     */
23
    public function rules(): array
24
    {
25
        $this->getValidationsComponents();
5✔
26

27
        return array_merge([
5✔
28
            'waba_phone' => 'required',
5✔
29
            'to' => 'required',
5✔
30
            'template' => 'required',
5✔
31
            'vars' => 'nullable|array',
5✔
32
        ], $this->getValidationsComponents());
5✔
33
    }
34

35
    private function getValidationsComponents(): array|false
36
    {
37
        $validations = [];
5✔
38
        $template = Template::find($this->template);
5✔
39

40
        if (! $template) {
5✔
41
            return false;
×
42
        }
43

44
        foreach ($template->getComponents() as $index => $component) {
5✔
45
            switch (strtoupper($index)) {
5✔
46
                case 'BODY':
5✔
47
                    $this->getComponentVarsValidations($component, $validations, 'body');
4✔
48
                    break;
4✔
49
                case 'HEADER':
3✔
50
                    $this->getHeaderValidations($component, $validations);
2✔
51
                    break;
2✔
52

53
                default:
54
                    break;
1✔
55
            }
56
        }
57

58
        return $validations;
5✔
59
    }
60

61
    private function getComponentVarsValidations(array $component, array &$validations, $type): void
62
    {
63
        $uniques = $this->countUniqueVars($component['text']);
4✔
64

65
        if ($uniques === 0) {
4✔
66
            $validations["vars.$type.parameters"] = 'nullable';
2✔
67

68
            return;
2✔
69
        }
70

71
        $validations["vars.$type.parameters"] = 'required|array|size:'.$uniques;
2✔
72

73
        for ($i = 0; $i < $uniques; $i++) {
2✔
74
            $validations["vars.$type.parameters.$i.text"] = 'required|string';
2✔
75
            $validations["vars.$type.parameters.$i.type"] = 'required|string';
2✔
76
        }
77
    }
78

79
    private function countUniqueVars(string $text): int
80
    {
81
        preg_match_all('/{{([0-9]{1,2})}}/', $text, $matches);
4✔
82
        $uniques = array_unique(array_map('intval', $matches[1]));
4✔
83

84
        return count($uniques);
4✔
85
    }
86

87
    private function getHeaderValidations(array $component, array &$validations): void
88
    {
89
        switch ($component['format']) {
2✔
90
            case 'text':
2✔
91
                $this->getComponentVarsValidations($component, $validations, 'header');
×
92
                break;
×
93
            case 'image':
2✔
94
            case 'document':
×
95
            case 'video':
×
96
                $validations['vars.header.parameters.0.type'] = 'required|string';
2✔
97
                $validations['vars.header.parameters.0.'.$component['format'].'.link'] = 'required|string';
2✔
98
                break;
2✔
99
        }
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc