Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.20% covered (success)
90.20%
92 / 102
71.43% covered (warning)
71.43%
10 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessValidator
90.20% covered (success)
90.20%
92 / 102
71.43% covered (warning)
71.43%
10 / 14
40.43
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCollection
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProcess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDelegatedProcess
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateId
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 validateAuthKey
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 validateMail
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
8
 validateName
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 validateCustomField
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 validateTelephone
82.61% covered (warning)
82.61%
19 / 23
0.00% covered (danger)
0.00%
0 / 1
6.19
 validateSurvey
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 validateText
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
2.01
 validateReminderTimestamp
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 validateRequests
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace BO\Zmsentities\Validator;
4
5use BO\Mellon\Valid;
6use BO\Mellon\Unvalidated;
7use BO\Mellon\Validator;
8use BO\Mellon\Parameter;
9use BO\Mellon\Collection;
10use BO\Zmsentities\Process;
11use BO\Zmsentities\Helper\Delegate;
12
13/**
14 *
15 */
16class ProcessValidator
17{
18    protected $process;
19
20    protected $collection = [];
21
22    public function __construct(Process $process)
23    {
24        $this->process = $process;
25        $this->collection = new Collection([]);
26    }
27
28    public function getCollection(): Collection
29    {
30        return $this->collection;
31    }
32
33    public function getProcess(): Process
34    {
35        return $this->process;
36    }
37
38    public function getDelegatedProcess(): Delegate
39    {
40        $process = $this->getProcess();
41        $delegatedProcess = new Delegate($process);
42        return $delegatedProcess;
43    }
44
45    public function validateId(Unvalidated $unvalid, callable $setter, callable $isRequiredCallback = null): self
46    {
47        $valid = $unvalid->isNumber(
48            "Eine gültige Vorgangsnummer ist in der Regel eine sechsstellige Nummer wie '123456'"
49        );
50        $length = strlen((string)$valid->getValue());
51        if ($length) {
52            $valid->isGreaterThan(100000, "Eine Vorgangsnummer besteht aus mindestens 6 Ziffern");
53            $valid->isLowerEqualThan(99999999999, "Eine Vorgangsnummer besteht aus maximal 11 Ziffern");
54        } elseif (!$length && $isRequiredCallback && $isRequiredCallback()) {
55            $valid->isRequired("Eine Vorgangsnummer wird benötigt.");
56        }
57        $this->getCollection()->validatedAction($valid, $setter);
58        return $this;
59    }
60
61    public function validateAuthKey(Unvalidated $unvalid, callable $setter, callable $isRequiredCallback = null): self
62    {
63        $valid = $unvalid->isString();
64        $length = strlen((string)$valid->getValue());
65        if ($length || ($isRequiredCallback && $isRequiredCallback())) {
66            $valid
67                ->isBiggerThan(4, "Es müssen mindestens 4 Zeichen eingegeben werden.")
68                ;
69        }
70        $this->getCollection()->validatedAction($valid, $setter);
71        return $this;
72    }
73
74    public function validateMail(Unvalidated $unvalid, callable $setter, callable $isRequiredCallback = null): self
75    {
76        $valid = $unvalid->isString();
77        $length = strlen((string)$valid->getUnvalidated());
78        $process = $this->getProcess();
79
80        /*
81        error_log(
82            "Mail validate: ".$valid->getUnvalidated()
83            ." ($length) with scope mail required="
84            . ($process->getCurrentScope()->isEmailRequired() ? 'yes' : 'no')
85            ." with appointment="
86            . ($process->isWithAppointment() ? 'yes' : 'no')
87            ." with callback="
88            . ( ($isRequiredCallback && $isRequiredCallback()) ? 'yes' : 'no')
89        );
90        */
91        if (!$length && $process->getCurrentScope()->isEmailRequired() && $process->isWithAppointment()) {
92            $valid->isBiggerThan(
93                6,
94                "Für den Standort muss eine gültige E-Mail Adresse eingetragen werden"
95            );
96        } elseif (!$length && $isRequiredCallback && $isRequiredCallback()) {
97            $valid->isBiggerThan(
98                6,
99                "Für den Email-Versand muss eine gültige E-Mail Adresse angegeben werden"
100            );
101        } elseif ($length) {
102            $valid = $unvalid
103                ->isMail("Die E-Mail Adresse muss im Format max@mustermann.de eingeben werden.")
104                ->hasDNS(
105                    "Zu der angegebenen E-Mail-Adresse können keine Mails verschickt werden. " .
106                    "Der Host zur Domain nach dem '@' ist nicht erreichbar. "
107                );
108        }
109        $this->getCollection()->validatedAction($valid, $setter);
110        return $this;
111    }
112
113    public function validateName(Unvalidated $unvalid, callable $setter): self
114    {
115        $valid = $unvalid->isString();
116        $length = strlen((string)$valid->getValue());
117        if ($length || $this->getProcess()->isWithAppointment()) {
118            $valid
119                ->isBiggerThan(2, "Es muss ein aussagekräftiger Name eingegeben werden")
120                ->isSmallerThan(50, "Der Name sollte 50 Zeichen nicht überschreiten");
121        }
122        $this->getCollection()->validatedAction($valid, $setter);
123        return $this;
124    }
125
126    public function validateCustomField(Unvalidated $unvalid, callable $setter): self
127    {
128        $valid = $unvalid->isString();
129        $valid->isBiggerThan(1, "Dieses Feld darf nicht leer sein");
130        $this->getCollection()->validatedAction($valid, $setter);
131        return $this;
132    }
133
134    public function validateTelephone(Unvalidated $unvalid, callable $setter): self
135    {
136        $valid = $unvalid->isString();
137        $length = strlen((string)$valid->getValue());
138
139        try {
140            $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
141            $phoneNumberObject = $phoneNumberUtil->parse($valid->getValue(), 'DE');
142            $telephone = '+' . $phoneNumberObject->getCountryCode() . $phoneNumberObject->getNationalNumber();
143        } catch (\Exception $exception) {
144            $telephone = $valid->getValue();
145        }
146        $valid = (new \BO\Mellon\Unvalidated($telephone, 'telephone'))->isString();
147
148        if (
149            !$length
150            && $this->getProcess()->getCurrentScope()->isTelephoneRequired()
151            && $this->getProcess()->isWithAppointment()
152        ) {
153            $valid
154                ->isBiggerThan(10, "Für den Standort muss eine gültige Telefonnummer eingetragen werden");
155        } elseif ($length) {
156            $valid
157                ->isSmallerThan(
158                    15,
159                    "Die Telefonnummer ist zu lang, bitte prüfen Sie Ihre Eingabe"
160                )
161                ->isBiggerThan(10, "Für den Standort muss eine gültige Telefonnummer eingetragen werden")
162                ->isMatchOf("/^\+?[\d\s]*$/", "Die Telefonnummer muss im Format 0170 1234567 eingegeben werden");
163        }
164        $this->getCollection()->validatedAction($valid, $setter);
165        return $this;
166    }
167
168    public function validateSurvey(Unvalidated $unvalid, callable $setter): self
169    {
170        $valid = $unvalid->isNumber("Bitte wählen Sie eine Option");
171        $this->getCollection()->validatedAction($valid, $setter);
172        return $this;
173    }
174
175    public function validateText(Unvalidated $unvalid, callable $setter): self
176    {
177        $valid = $unvalid->isString();
178        $length = strlen((string)$valid->getUnvalidated());
179        if ($length) {
180            $valid->isSmallerThan(500, "Die Anmerkung sollte 500 Zeichen nicht überschreiten");
181            $this->getCollection()->validatedAction($valid, $setter);
182        } else {
183            $this->getCollection()->addValid($valid);
184        }
185        return $this;
186    }
187
188    public function validateReminderTimestamp(Unvalidated $unvalid, callable $setter, callable $conditionCallback): self
189    {
190        $valid = $unvalid->isNumber();
191        if ($conditionCallback && $conditionCallback()) {
192            $this->getCollection()->validatedAction($valid, $setter);
193        } else {
194            $this->getCollection()->addValid($valid);
195        }
196        return $this;
197    }
198
199    public function validateRequests(Unvalidated $unvalid, callable $setter): self
200    {
201        if ($this->getProcess()->isWithAppointment()) {
202             $valid = $unvalid->isArray("Es muss mindestens eine Dienstleistung ausgewählt werden!");
203             $this->getCollection()->validatedAction($valid, $setter);
204        }
205        return $this;
206    }
207}