]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/nickname.php
Tickets #1610, #1949: show permalinks on Popular Notices, attachments aside sections
[quix0rs-gnu-social.git] / lib / nickname.php
index 48269f3b9f5f4de08d81e461f8885ef3103635bd..4d3795e392231fe5a8bd9966f8f28f504dd847c7 100644 (file)
 class Nickname
 {
     /**
-     * Regex fragment for pulling an arbitrarily-formated nickname.
+     * Regex fragment for pulling a formated nickname *OR* ID number.
+     * Suitable for router def of 'id' parameters on API actions.
      *
      * Not guaranteed to be valid after normalization; run the string through
-     * Nickname::normalize() to get the canonical form, or Nickname::validate()
+     * Nickname::normalize() to get the canonical form, or Nickname::isValid()
+     * if you just need to check if it's properly formatted.
+     *
+     * This, DISPLAY_FMT, and CANONICAL_FMT replace the old NICKNAME_FMT,
+     * but be aware that these should not be enclosed in []s.
+     *
+     * @fixme would prefer to define in reference to the other constants
+     */
+    const INPUT_FMT = '(?:[0-9]+|[0-9a-zA-Z_]{1,64})';
+
+    /**
+     * Regex fragment for acceptable user-formatted variant of a nickname.
+     * This includes some chars such as underscore which will be removed
+     * from the normalized canonical form, but still must fit within
+     * field length limits.
+     *
+     * Not guaranteed to be valid after normalization; run the string through
+     * Nickname::normalize() to get the canonical form, or Nickname::isValid()
      * if you just need to check if it's properly formatted.
      *
      * This and CANONICAL_FMT replace the old NICKNAME_FMT, but be aware
      * that these should not be enclosed in []s.
      */
-    const DISPLAY_FMT = '[0-9a-zA-Z_]+';
+    const DISPLAY_FMT = '[0-9a-zA-Z_]{1,64}';
 
     /**
      * Regex fragment for checking a canonical nickname.
@@ -61,7 +79,7 @@ class Nickname
      * @param string $str
      * @return boolean
      */
-    public static function validate($str)
+    public static function isValid($str)
     {
         try {
             self::normalize($str);
@@ -85,15 +103,17 @@ class Nickname
      */
     public static function normalize($str)
     {
+        if (mb_strlen($str) > self::MAX_LEN) {
+            // Display forms must also fit!
+            throw new NicknameTooLongException();
+        }
+
         $str = trim($str);
         $str = str_replace('_', '', $str);
         $str = mb_strtolower($str);
 
-        $len = mb_strlen($str);
-        if ($len < 1) {
+        if (mb_strlen($str) < 1) {
             throw new NicknameEmptyException();
-        } else if ($len > self::MAX_LEN) {
-            throw new NicknameTooLongException();
         }
         if (!self::isCanonical($str)) {
             throw new NicknameInvalidException();