MyComments/src/Utils/UserValidator.php
Skylar Sadlier 3ed53ac05e - Basic HTML templates
- Created entities & new migration
- Added packages symfony/stimulus-bundle & symfony/webpack-encore-bundle
2024-02-12 19:51:33 -07:00

73 lines
2.1 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use function Symfony\Component\String\u;
/**
* This class is used to provide an example of integrating simple classes as
* services into a Symfony application.
* See https://symfony.com/doc/current/service_container.html#creating-configuring-services-in-the-container.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
final class UserValidator
{
public function validateDisplayName(?string $displayName): string
{
if (empty($displayName)) {
throw new InvalidArgumentException('The display name can not be empty.');
}
if (1 !== preg_match('/^[0-9a-zA-Z _\-]+$/', $displayName)) {
throw new InvalidArgumentException('The display name must contain only latin characters and underscores.');
}
return $displayName;
}
public function validatePassword(?string $plainPassword): string
{
if (empty($plainPassword)) {
throw new InvalidArgumentException('The password can not be empty.');
}
if (u($plainPassword)->trim()->length() < 6) {
throw new InvalidArgumentException('The password must be at least 6 characters long.');
}
return $plainPassword;
}
public function validateEmail(?string $email): string
{
if (empty($email)) {
throw new InvalidArgumentException('The email can not be empty.');
}
if (null === u($email)->indexOf('@')) {
throw new InvalidArgumentException('The email should look like a real email.');
}
return $email;
}
public function validateFullName(?string $fullName): string
{
if (empty($fullName)) {
throw new InvalidArgumentException('The full name can not be empty.');
}
return $fullName;
}
}