1. Go to this page and download the library: Download popcorn4dinner/policies library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
popcorn4dinner / policies example snippets
namespace Popcorn4dinner\Policies\Examples\Policies;
use Popcorn4dinner\Policies\AbstractPolicy;
use Popcorn4dinner\Policies\Examples\User;
use Popcorn4dinner\Policies\Examples\UserAction;
use Popcorn4dinner\Policies\Examples\UserRepositoryInterface;
class EmailFormatPolicy extends AbstractPolicy
{
protected const ERROR_MESSAGE = 'Invalid email address';
/**
* @param MvpUser $user
* @return bool
* @throws PolicyValidationException
*/
protected function isViolatedWith(MvpUser $user, UserAction $action): bool
{
return $this->isInvalidEmail($user->getEmail());
}
/**
* @param string $email
* @return bool
*
*/
private function isInvalidEmail(string $email): bool
{
return filter_var($email, FILTER_VALIDATE_EMAIL) === false;
}
namespace Popcorn4dinner\Policies\Examples\Policies;
use Popcorn4dinner\Policies\AbstractPolicy;
use Popcorn4dinner\Policies\Examples\User;
use Popcorn4dinner\Policies\Examples\UserAction;
use Popcorn4dinner\Policies\Examples\UserRepositoryInterface;
class EmailUniquenessPolicy extends AbstractPolicy
{
protected const ERROR_MESSAGE = 'User with email already exists.';
/**
* @var UserRepositoryInterface
*/
private $userRepository;
/**
* EmailPresencePolicy constructor.
* @param UserRepositoryInterface $userRepository
*/
public function __construct(UserRepositoryInterface $userRepository, UserAction ...$excludedActions)
{
parent::__construct(...$excludedActions);
$this->userRepository = $userRepository;
}
/**
* @param MvpUser $user
* @return bool
* @throws PolicyValidationException
*/
protected function isViolatedWith(MvpUser $user, UserAction $action): bool
{
return !$this->isUniqueEmail($user->getEmail());
}
private function isUniqueEmail(string $email): bool
{
return $this->userRepository->findByEmail($email) === null;
}
namespace Popcorn4dinner\Policies\Examples\Policies;
use Popcorn4dinner\Policies\AbstractPolicy;
use Popcorn4dinner\Policies\Examples\User;
use Popcorn4dinner\Policies\Examples\UserAction;
use Popcorn4dinner\Policies\Examples\UserRepositoryInterface;
class PasswordLengthPolicy extends AbstractPolicy
{
protected const ERROR_MESSAGE = 'Password not long enough.';
private const MIN_PASSWORD_LENGTH = 8;
/**
* @param MvpUser $user
* @return bool
* @throws PolicyValidationException
*/
protected function isViolatedWith(MvpUser $user, UserAction $action): bool
{
return $this->isInvalidPassword($user->getPassword());
}
private function isInvalidPassword(string $password): bool
{
return strlen($password) < static::MIN_PASSWORD_LENGTH;
}
namespace Popcorn4dinner\Policies\Examples\Policies;
use Popcorn4dinner\Policies\AbstractPolicy;
use Popcorn4dinner\Policies\Examples\User;
use Popcorn4dinner\Policies\Examples\UserAction;
use Popcorn4dinner\Policies\Examples\UserRepositoryInterface;
class PasswordCharactersPolicy extends AbstractPolicy
{
protected const ERROR_MESSAGE = 'Password not strong enough.';
private const NUMBER_IN_PASSWORD = '#[0-9]+#';
private const LOWERCASE_CHARACTERS = '#[a-z]+#';
private const UPPERCASE_CHARACTERS = '#[A-Z]+#';
/**
* @param MvpUser $user
* @return bool
* @throws PolicyValidationException
*/
protected function isViolatedWith(MvpUser $user, UserAction $action): bool
{
return $this->isInvalidPassword($user->getPassword());
private function isInvalidPassword(string $password): bool
{
$
namespace Popcorn4dinner\Policies\Examples;
use Popcorn4dinner\Policies\Examples\Policies\EmailFormatPolicy;
use Popcorn4dinner\Policies\Examples\Policies\EmailUniquenessPolicy;
use Popcorn4dinner\Policies\Examples\Policies\PasswordLengthPolicy;
use Popcorn4dinner\Policies\Examples\Policies\PasswordCharactersPolicy;
use Popcorn4dinner\Policies\BasicValidator;
class UserValidatorFactory
{
/**
* @param UserRepositoryInterface $userRepository
* @return UserValidator
*/
public function create(UserRepositoryInterface $userRepository)
{
return new BasicValidator(
new EmailFormatPolicy(),
new EmailUniquenessPolicy($userRepository),
new PasswordLengthPolicy(UserAction::ADMIN_UPDATE()),
new PasswordStrengthPolicy(UserAction::ADMIN_UPDATE())
);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.