Overview

Namespaces

  • SpanishGuestReportGenerator
    • Util

Classes

  • SpanishGuestReportGenerator\AbstractFactory
  • SpanishGuestReportGenerator\Guest
  • SpanishGuestReportGenerator\GuestFactory
  • SpanishGuestReportGenerator\GuestReport
  • SpanishGuestReportGenerator\Hotel
  • SpanishGuestReportGenerator\HotelFactory
  • SpanishGuestReportGenerator\Util\Helper

Traits

  • SpanishGuestReportGenerator\FormatterTrait
  • SpanishGuestReportGenerator\SanitizerTrait

Exceptions

  • SpanishGuestReportGenerator\FactoryException
  • SpanishGuestReportGenerator\GuestReportException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 
<?php
/**
 * Spanish Guest Report Generator
 *
 * @package    Spanish Guest Report Generator
 * @author     Javier Zapata <javierzapata82@gmail.com>
 * @copyright  2021 Javier Zapata <javierzapata82@gmail.com>
 * @license    https://opensource.org/licenses/MIT  The MIT License
 * @link       https://github.com/bahiazul/spanish-guest-report-generator
 */

namespace SpanishGuestReportGenerator;

use SpanishGuestReportGenerator\Util\Helper;

/**
 * Sanitizers
 *
 * @package    Spanish Guest Report Generator
 * @author     Javier Zapata <javierzapata82@gmail.com>
 * @copyright  2021 Javier Zapata <javierzapata82@gmail.com>
 * @license    https://opensource.org/licenses/MIT  The MIT License
 * @link       https://github.com/bahiazul/spanish-guest-report-generator
 */
trait SanitizerTrait
{
    /**
     * Maximum length for string properties
     *
     * @var array
     */
    private static $lengths = [
        'code'        => 10,
        'bizName'     => 40,
        'spanishID'   => 11,
        'foreignID'   => 14,
        'personName'  => 30,
        'countryName' => 21,
    ];

    /**
     * Accepted Gender Codes
     *
     * @var array
     */
    private static $genders = [
        'F', // Female
        'M', // Male
    ];

    /**
     * Accepted Document Type Codes
     *
     * @var array
     */
    private static $docTypes = [
        'D', // Spanish ID (DNI)
        'P', // Passport
        'C', // Driving License
        'I', // Foreign ID
        'N', // Spanish Residence Permit
        'X', // Foreign (EU) Residence Permit
    ];

    /**
     * Accepted date patterns indexed by its human-readable format
     *
     * @var array
     */
    private static $dateFormats = [
        'DD-MM-YYYY' => '/^(?<d>\d{2})-(?<m>\d{2})-(?<y>\d{4})$/',
        'DD.MM.YYYY' => '/^(?<d>\d{2})\.(?<m>\d{2})\.(?<y>\d{4})$/',
        'DD/MM/YYYY' => '/^(?<d>\d{2})\/(?<m>\d{2})\/(?<y>\d{4})$/',
        'YYYY-MM-DD' => '/^(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})$/',
        'YYYY.MM.DD' => '/^(?<y>\d{4})\.(?<m>\d{2})\.(?<d>\d{2})$/',
        'YYYY/MM/DD' => '/^(?<y>\d{4})\/(?<m>\d{2})\/(?<d>\d{2})$/',
        'YYYYMMDD'   => '/^(?<y>\d{4})(?<m>\d{2})(?<d>\d{2})$/',
    ];

    /**
     * Sanitizes a given gender
     *
     * @param   string $gender  Gender
     * @return  string
     */
    private function sanitizeGender($gender)
    {
        $gender = strtoupper($gender);

        return $this->sanitizePresence($gender, self::$genders);
    }

    /**
     * Sanitizes a given document type
     *
     * @param   string $docType     Document Type
     * @return  string
     */
    private function sanitizeDocType($docType)
    {
        $docType = strtoupper($docType);

        return $this->sanitizePresence($docType, self::$docTypes);
    }

    /**
     * Sanitizes the format of a date (not the date itself)
     *
     * @param   string $date        The date which format is to sanitize
     * @return  string
     */
    private function sanitizeDate($date)
    {
        $date = Helper::stringify($date);

        $matches = [];
        foreach (self::$dateFormats as $pattern) {
            if (preg_match($pattern, $date, $matches)) break;
        }

        return !empty($matches)
             ? $matches['y'].$matches['m'].$matches['d']
             : '00000000';
    }

    /**
     * Sanitizes an internal code
     *
     * @param   string $string       Code
     * @return  string
     */
    private function sanitizeCode($string)
    {
        return $this->sanitizeLength($string, self::$lengths['code']);
    }

    /**
     * Sanitizes a business name
     *
     * @param   string $string       Business name
     * @return  string
     */
    private function sanitizeBizName($string)
    {
        return $this->sanitizeLength($string, self::$lengths['bizName']);
    }

    /**
     * Sanitizes a spanish ID number
     *
     * @param   string $string       Spanish ID number
     * @return  string
     */
    private function sanitizeSpanishID($string)
    {
        return $this->sanitizeLength($string, self::$lengths['spanishID']);
    }

    /**
     * Sanitizes a foreign ID number
     *
     * @param   string $string       Foreign ID number
     * @return  string
     */
    private function sanitizeForeignID($string)
    {
        return $this->sanitizeLength($string, self::$lengths['foreignID']);
    }

    /**
     * Sanitizes a person name
     *
     * @param   string $string       Person name
     * @return  string
     */
    private function sanitizePersonName($string)
    {
        return $this->sanitizeLength($string, self::$lengths['personName']);
    }

    /**
     * Sanitizes a country name
     *
     * @param   string $string       Country name
     * @return  string
     */
    private function sanitizeCountryName($string)
    {
        return $this->sanitizeLength($string, self::$lengths['countryName']);
    }

    /**
     * Sanitizes the presence of a value in a given list
     *
     * @param   mixed  $string       Value to sanitize
     * @param   array  $array        List of values to be checked against
     * @return  string
     */
    private function sanitizePresence($string, array $array)
    {
        $string = Helper::stringify($string);

        return in_array($string, $array)
             ? $string
             : Helper::stringify(array_shift($array));
    }

    /**
     * Sanitizes the length of a string
     *
     * @param   string  $string     String to sanitize
     * @param   int     $length     Maximum length allowed
     */
    private function sanitizeLength($string, $length)
    {
        $string = trim(Helper::stringify($string));
        $length >= 0 || $length = 0;

        return substr($string, 0, $length);
    }
}
API documentation generated by ApiGen