AloFramework documentation
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Namespaces

  • Alo
    • Cache
    • CLI
    • Controller
    • Db
    • Exception
    • FileSystem
    • Session
    • Statics
    • Validators
    • Windows
  • Controller
  • None
  • PHP

Classes

  • Cookie
  • Format
  • Security
  1 <?php
  2 
  3     namespace Alo\Statics;
  4 
  5     if (!defined('GEN_START')) {
  6         http_response_code(404);
  7     } else {
  8 
  9         /**
 10          * Format validation statics
 11          *
 12          * @author  Art <a.molcanovas@gmail.com>
 13          * @package Statics
 14          */
 15         abstract class Format {
 16 
 17             /**
 18              * Defines an method as "serialize"
 19              *
 20              * @var int
 21              */
 22             const M_SERIALIZE = 0;
 23 
 24             /**
 25              * Defines a method as "json_encode"
 26              *
 27              * @var int
 28              */
 29             const M_JSON = 1;
 30 
 31             /**
 32              * Defines a method as "print_r"
 33              *
 34              * @var int
 35              */
 36             const M_PRINT_R = 2;
 37 
 38             /**
 39              * Checks whether the data is valid JSON
 40              *
 41              * @author Art <a.molcanovas@gmail.com>
 42              *
 43              * @param mixed $data The data to check
 44              *
 45              * @return boolean
 46              */
 47             static function isJSON($data) {
 48                 if (!is_scalar($data)) {
 49                     return false;
 50                 } else {
 51                     json_decode($data, true);
 52 
 53                     return json_last_error() === JSON_ERROR_NONE;
 54                 }
 55             }
 56 
 57             /**
 58              * Checks if the supplied string is a valid IPv4 IP
 59              *
 60              * @author Art <a.molcanovas@gmail.com>
 61              *
 62              * @param string $input The input
 63              *
 64              * @return bool
 65              */
 66             static function isIpv4($input) {
 67                 if (!is_scalar($input)) {
 68                     return false;
 69                 } else {
 70                     $e = explode('.', explode('/', $input)[0]);
 71 
 72                     if (count($e) != 4) {
 73                         return false;
 74                     } else {
 75                         foreach ($e as $v) {
 76                             if (!is_numeric($v)) {
 77                                 return false;
 78                             } else {
 79                                 $v = (int)$v;
 80 
 81                                 if ($v < 0 || $v > 255) {
 82                                     return false;
 83                                 }
 84                             }
 85                         }
 86                     }
 87 
 88                     return true;
 89                 }
 90             }
 91 
 92             /**
 93              * Makes output scalar. If $input is already scalar, simply returns it; otherwise uses a function specified in
 94              * $prettifyMethod to make the output scalar
 95              *
 96              * @param mixed $input           The input to scalarise
 97              * @param int   $prettifyMethod  Function to use to make output scalar if $input isn't already scalar. See class
 98              *                               M_* constants.
 99              *
100              * @return string
101              */
102             static function scalarOutput($input, $prettifyMethod = self::M_PRINT_R) {
103                 if (is_scalar($input)) {
104                     return $input;
105                 } else {
106                     switch ($prettifyMethod) {
107                         case self::M_JSON:
108                             return json_encode($input);
109                         case self::M_SERIALIZE:
110                             return serialize($input);
111                         default:
112                             return print_r($input, true);
113                     }
114                 }
115             }
116 
117             /**
118              * Typecasts a variable to float or int if it's numeric
119              *
120              * @author Art <a.molcanovas@gmail.com>
121              *
122              * @param mixed   $var      The variable
123              * @param boolean $boolMode Whether we're checking for boolean mode FULLTEXT search values
124              *
125              * @return int|float|mixed
126              */
127             static function makeNumeric($var, $boolMode = false) {
128                 if (is_numeric($var)) {
129                     if ($boolMode) {
130                         $first = substr($var, 0, 1);
131                         if ($first == '-' || $first == '+') {
132                             return $var;
133                         }
134                     }
135 
136                     if (stripos($var, '.') === false) {
137                         $var = (int)$var;
138                     } else {
139                         $var = (float)$var;
140                     }
141                 }
142 
143                 return $var;
144             }
145 
146         }
147     }
148 
AloFramework documentation API documentation generated byApiGen 2.8.0