Skip to content

Commit ca23ccc

Browse files
committed
add DateTimeService
1 parent 2a61a57 commit ca23ccc

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace doganoo\PHPUtil\Service\DateTime;
28+
29+
use DateTime;
30+
31+
/**
32+
* Class DateTimeService
33+
*
34+
* @package doganoo\PHPUtil\Service\DateTime
35+
* @author Dogan Ucar <dogan@dogan-ucar.de>
36+
*/
37+
class DateTimeService {
38+
39+
/**
40+
* @param int|null $timestamp
41+
*
42+
* @return DateTime|null
43+
*/
44+
public function fromNullableTimestamp(?int $timestamp): ?DateTime {
45+
if (null === $timestamp) return null;
46+
return $this->fromTimestamp($timestamp);
47+
}
48+
49+
/**
50+
* @param int $timestamp
51+
*
52+
* @return DateTime
53+
*/
54+
public function fromTimestamp(int $timestamp): DateTime {
55+
$dateTime = new DateTime();
56+
$dateTime->setTimestamp($timestamp);
57+
return $dateTime;
58+
}
59+
60+
}

src/Service/User.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
<?php
2-
2+
/**
3+
* MIT License
4+
*
5+
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
325

426
namespace doganoo\PHPUtil\Service;
527

28+
use function filter_var;
29+
use function strlen;
30+
use const FILTER_VALIDATE_EMAIL;
631

732
class User {
833

@@ -11,7 +36,7 @@ class User {
1136
public const PASSWORD_VALIDATION_SECURE = 3;
1237

1338
public function validEmail(string $email): bool {
14-
return \filter_var($email, \FILTER_VALIDATE_EMAIL) !== false;
39+
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
1540
}
1641

1742
public function validatePassword(string $password, int $mode = self::PASSWORD_VALIDATION_SECURE): bool {
@@ -26,7 +51,7 @@ public function validatePassword(string $password, int $mode = self::PASSWORD_VA
2651
}
2752

2853
private function validatePasswordUnsafe(string $password): bool {
29-
return \strlen($password) >= 8;
54+
return strlen($password) >= 8;
3055
}
3156

3257
private function validatePasswordBasic(string $password): bool {
@@ -40,4 +65,4 @@ private function validatePasswordSecure(string $password): bool {
4065
return $valid && ((bool) preg_match("(^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$)", $password));
4166
}
4267

43-
}
68+
}

0 commit comments

Comments
 (0)