Skip to content

Commit

Permalink
add DateTimeService
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed May 30, 2020
1 parent 2a61a57 commit ca23ccc
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
60 changes: 60 additions & 0 deletions src/Service/DateTime/DateTimeService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPUtil\Service\DateTime;

use DateTime;

/**
* Class DateTimeService
*
* @package doganoo\PHPUtil\Service\DateTime
* @author Dogan Ucar <dogan@dogan-ucar.de>
*/
class DateTimeService {

/**
* @param int|null $timestamp
*
* @return DateTime|null
*/
public function fromNullableTimestamp(?int $timestamp): ?DateTime {
if (null === $timestamp) return null;
return $this->fromTimestamp($timestamp);
}

/**
* @param int $timestamp
*
* @return DateTime
*/
public function fromTimestamp(int $timestamp): DateTime {
$dateTime = new DateTime();
$dateTime->setTimestamp($timestamp);
return $dateTime;
}

}
33 changes: 29 additions & 4 deletions src/Service/User.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
<?php

/**
* MIT License
*
* Copyright (c) 2018 Dogan Ucar, <dogan@dogan-ucar.de>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

namespace doganoo\PHPUtil\Service;

use function filter_var;
use function strlen;
use const FILTER_VALIDATE_EMAIL;

class User {

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

public function validEmail(string $email): bool {
return \filter_var($email, \FILTER_VALIDATE_EMAIL) !== false;
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}

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

private function validatePasswordUnsafe(string $password): bool {
return \strlen($password) >= 8;
return strlen($password) >= 8;
}

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

}
}

0 comments on commit ca23ccc

Please sign in to comment.