Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.57% covered (warning)
88.57%
93 / 105
71.43% covered (warning)
71.43%
10 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProcessValidator
88.57% covered (warning)
88.57%
93 / 105
71.43% covered (warning)
71.43%
10 / 14
44.63
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
76.92% covered (warning)
76.92%
20 / 26
0.00% covered (danger)
0.00%
0 / 1
10.00
 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($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($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($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, callable $isRequiredCallback = null): self
135    {
136        $valid = $unvalid->isString();
137        $length = strlen($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 && $isRequiredCallback && $isRequiredCallback()) {
156            $valid
157                ->isBiggerThan(10, "Für den SMS-Versand muss eine gültige Mobilfunknummer angegeben werden");
158        } elseif ($length) {
159            $valid
160                ->isSmallerThan(
161                    15,
162                    "Die Telefonnummer ist zu lang, bitte prüfen Sie Ihre Eingabe"
163                )
164                ->isBiggerThan(10, "Für den Standort muss eine gültige Telefonnummer eingetragen werden")
165                ->isMatchOf("/^\+?[\d\s]*$/", "Die Telefonnummer muss im Format 0170 1234567 eingegeben werden");
166        }
167        $this->getCollection()->validatedAction($valid, $setter);
168        return $this;
169    }
170
171    public function validateSurvey(Unvalidated $unvalid, callable $setter): self
172    {
173        $valid = $unvalid->isNumber("Bitte wählen Sie eine Option");
174        $this->getCollection()->validatedAction($valid, $setter);
175        return $this;
176    }
177
178    public function validateText(Unvalidated $unvalid, callable $setter): self
179    {
180        $valid = $unvalid->isString();
181        $length = strlen($valid->getUnvalidated());
182        if ($length) {
183            $valid->isSmallerThan(500, "Die Anmerkung sollte 500 Zeichen nicht überschreiten");
184            $this->getCollection()->validatedAction($valid, $setter);
185        } else {
186            $this->getCollection()->addValid($valid);
187        }
188        return $this;
189    }
190
191    public function validateReminderTimestamp(Unvalidated $unvalid, callable $setter, callable $conditionCallback): self
192    {
193        $valid = $unvalid->isNumber();
194        if ($conditionCallback && $conditionCallback()) {
195            $this->getCollection()->validatedAction($valid, $setter);
196        } else {
197            $this->getCollection()->addValid($valid);
198        }
199        return $this;
200    }
201
202    public function validateRequests(Unvalidated $unvalid, callable $setter): self
203    {
204        if ($this->getProcess()->isWithAppointment()) {
205             $valid = $unvalid->isArray("Es muss mindestens eine Dienstleistung ausgewählt werden!");
206             $this->getCollection()->validatedAction($valid, $setter);
207        }
208        return $this;
209    }
210}