daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. declare(strict_types=1);
  3.  
  4. namespace Reports\Common\Application\Validator;
  5.  
  6.  
  7. final class StringFilter
  8. {
  9.     const EMPTYSTRING    = 'EmptyString';
  10.     const NOTNULL        = 'NotNull';
  11.     const VALIDATIONCODE = 422;
  12.  
  13.     /** @var string $value */
  14.     private $value;
  15.  
  16.     /**
  17.      * @var array
  18.      */
  19.     protected $messageTemplates = [
  20.         self::EMPTYSTRING => 'Given value cannot be empty string',
  21.         self::NOTNULL     => 'Given value cannot be null',
  22.     ];
  23.  
  24.     public function __construct(string $valueToValid)
  25.     {
  26.         $this->isValid($valueToValid);
  27.         $this->translate($valueToValid);
  28.     }
  29.  
  30.     private function isValid(string $valueToValid)
  31.     {
  32.         if ($valueToValid === '') {
  33.             throw new \Exception($this->messageTemplates[self::EMPTYSTRING], self::VALIDATIONCODE);
  34.         }
  35.  
  36.         if (!$valueToValid) {
  37.             throw new \Exception($this->messageTemplates[self::NOTNULL], self::VALIDATIONCODE);
  38.         }
  39.     }
  40.  
  41.     private function translate(string $valueToTranslate)
  42.     {
  43.         $this->value = strtr($valueToTranslate, 'ĘÓĄŚŁŻŹĆŃęóąśłżźćń', 'EOASLZZCNeoaslzzcn');
  44.     }
  45.  
  46.     public function getValue(): string{
  47.         return $this->value;
  48.     }
  49. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top