-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidationTrait.php
More file actions
71 lines (62 loc) · 1.89 KB
/
ValidationTrait.php
File metadata and controls
71 lines (62 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace ProgrammatorDev\OpenWeatherMap\Resource\Util;
use ProgrammatorDev\OpenWeatherMap\Language\Language;
use ProgrammatorDev\OpenWeatherMap\UnitSystem\UnitSystem;
use ProgrammatorDev\Validator\Exception\ValidationException;
use ProgrammatorDev\Validator\Validator;
trait ValidationTrait
{
/**
* @throws ValidationException
*/
protected function validateQuery(string $query, string $name): void
{
Validator::notBlank()->assert($query, $name);
}
/**
* @throws ValidationException
*/
protected function validatePositive(int $number, string $name): void
{
Validator::greaterThan(0)->assert($number, $name);
}
/**
* @throws ValidationException
*/
protected function validateCoordinate(float $latitude, float $longitude): void
{
Validator::range(-90, 90)->assert($latitude, 'latitude');
Validator::range(-180, 180)->assert($longitude, 'longitude');
}
/**
* @throws ValidationException
*/
protected function validateCountryCode(string $countryCode): void
{
Validator::country()->assert($countryCode, 'countryCode');
}
/**
* @throws ValidationException
*/
protected function validateLanguage(string $language): void
{
Validator::choice(Language::getOptions())->assert($language, 'language');
}
/**
* @throws ValidationException
*/
protected function validateUnitSystem(string $unitSystem): void
{
Validator::choice(UnitSystem::getOptions())->assert($unitSystem, 'unitSystem');
}
/**
* @throws ValidationException
*/
protected function validateDateOrder(\DateTimeInterface $startDate, \DateTimeInterface $endDate): void
{
Validator::greaterThan(
constraint: $startDate,
message: 'The endDate must be after the startDate.'
)->assert($endDate);
}
}