public static function encode_filename($st, $maxlength = 50, $extension = null, $is_path = false): string
  {

    $st = self::remove_accents(self::cast($st));
    $allowed = '~\-_.,\(\[\)\]';

    // Arguments order doesn't matter
    $args = \func_get_args();
    foreach ( $args as $i => $a ){
      if ( $i > 0 ){
        if ( \is_string($a) ){
          $extension = $a;
        }
        else if ( \is_int($a) ){
          $maxlength = $a;
        }
        else if ( \is_bool($a) ){
          $is_path = $a;
        }
      }
    }

    if ( !\is_int($maxlength) ){
      $maxlength = mb_strlen($st);
    }

    if ( $is_path ){
      $allowed .= '/';
    }

    if (
      $extension &&
      (self::file_ext($st) === self::change_case($extension, 'lower'))
    ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    else if ( $extension = self::file_ext($st) ){
      $st = substr($st, 0, -(\strlen($extension)+1));
    }
    $st = mb_ereg_replace("([^\w\s\d".$allowed.".])", '', $st);
    $st = mb_ereg_replace("([\.]{2,})", '', $st);
    $res = mb_substr($st, 0, $maxlength);
    if ( $extension ){
      $res .= '.' . $extension;
    }
    return $res;
  }