| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Filter; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Filter - Filter functions for Nile framework. | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | $filter = $me->filter; | ||
| 24 | |||
| 25 | # trim leading and traling spaces | ||
| 26 | $str = $filter->trim($str); | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | Nile::Filter - Filter functions for Nile framework. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use Nile::Base; | ||
| 35 | use URI; | ||
| 36 | use Email::Valid; | ||
| 37 | use Data::Validate::URI; | ||
| 38 | use HTML::Entities; | ||
| 39 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 40 | =head2 trim() | ||
| 41 | |||
| 42 | $str = $filter->trim($str); | ||
| 43 | @str = $filter->trim(@str); | ||
| 44 | |||
| 45 | Remove white spaces from left and right of a string. | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub trim { | ||
| 50 | my ($self) = shift; | ||
| 51 | #return if not defined wantarray; # void context | ||
| 52 | # /r - perform non-destructive substitution and return the new value | ||
| 53 | return map { s/^\s+|\s+$//gr } @_ if wantarray; | ||
| 54 | return shift =~ s/^\s+|\s+$//gr; | ||
| 55 | } | ||
| 56 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 57 | =head2 ltrim() | ||
| 58 | |||
| 59 | $str = $filter->ltrim($str); | ||
| 60 | @str = $filter->ltrim(@str); | ||
| 61 | |||
| 62 | Remove white spaces from left of a string. | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | sub ltrim { | ||
| 67 | my ($self) = shift; | ||
| 68 | return map { s/^\s+//gr } @_ if wantarray; | ||
| 69 | return shift =~ s/^\s+//gr; | ||
| 70 | } | ||
| 71 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 72 | =head2 rtrim() | ||
| 73 | |||
| 74 | $str = $filter->rtrim($str); | ||
| 75 | @str = $filter->rtrim(@str); | ||
| 76 | |||
| 77 | Remove white spaces from right of a string. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub rtrim { | ||
| 82 | my ($self) = shift; | ||
| 83 | return map { s/\s+$//gr } @_ if wantarray; | ||
| 84 | return shift =~ s/\s+$//gr; | ||
| 85 | } | ||
| 86 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 87 | =head2 trims() | ||
| 88 | |||
| 89 | $str = $filter->trims($str); | ||
| 90 | @str = $filter->trims(@str); | ||
| 91 | |||
| 92 | Remove all white spaces from a string. | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub trims { | ||
| 97 | my ($self) = shift; | ||
| 98 | #return if not defined wantarray; # void context | ||
| 99 | return map { s/\s+//gr } @_ if wantarray; | ||
| 100 | return shift =~ s/\s+//gr; | ||
| 101 | } | ||
| 102 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 103 | =head2 strip_html() | ||
| 104 | |||
| 105 | $str = $filter->strip_html($str); | ||
| 106 | |||
| 107 | Remove all html tags from a string. | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub strip_html { | ||
| 112 | my ($self, $str) = @_; | ||
| 113 | $str =~ s/<.+?>//sg; | ||
| 114 | #$str =~ s/<[^>]+>//ig; | ||
| 115 | return $str; | ||
| 116 | } | ||
| 117 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 118 | =head2 disable_html() | ||
| 119 | |||
| 120 | $str = $filter->disable_html($str); | ||
| 121 | |||
| 122 | Disable all html tags in a string. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub disable_html { | ||
| 127 | my ($self, $str) = @_; | ||
| 128 | HTML::Entities::encode_entities($str, q{<>}); | ||
| 129 | } | ||
| 130 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 131 | =head2 escape_html() | ||
| 132 | |||
| 133 | $str = $filter->escape_html($str); | ||
| 134 | |||
| 135 | Encode entities C< ' " & < > > in string. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | sub escape_html { | ||
| 140 | my ($self, $str) = @_; | ||
| 141 | HTML::Entities::encode_entities($str, q{'"&<>}); | ||
| 142 | } | ||
| 143 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 144 | =head2 strip_script() | ||
| 145 | |||
| 146 | # remove <script...>...</script> tags | ||
| 147 | $str = $filter->strip_script($str); | ||
| 148 | |||
| 149 | Remove script tags from a string. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | sub strip_script { | ||
| 154 | my ($self, $str) = @_; | ||
| 155 | $str =~ s/<script[^>]*>.*?<\/script>//igs; | ||
| 156 | $str; | ||
| 157 | } | ||
| 158 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 159 | #s/<script[^>]*>.*?<\/script>//igs; | ||
| 160 | #quotemeta uc ucfirst lc lc_email uri | ||
| 161 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 162 | =head2 quotemeta() | ||
| 163 | |||
| 164 | $str = $filter->quotemeta($str); | ||
| 165 | |||
| 166 | Returns all the ASCII non-"word" characters backslashed. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub quotemeta { | ||
| 171 | my ($self, $str) = @_; | ||
| 172 | quotemeta($str); | ||
| 173 | } | ||
| 174 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 175 | =head2 uc() | ||
| 176 | |||
| 177 | $str = $filter->uc($str); | ||
| 178 | |||
| 179 | Upper case the string letters. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub uc { | ||
| 184 | my ($self, $str) = @_; | ||
| 185 | uc($str); | ||
| 186 | } | ||
| 187 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 188 | =head2 lc() | ||
| 189 | |||
| 190 | $str = $filter->lc($str); | ||
| 191 | |||
| 192 | Lower case the string letters. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub lc { | ||
| 197 | my ($self, $str) = @_; | ||
| 198 | lc($str); | ||
| 199 | } | ||
| 200 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 201 | =head2 ucfirst() | ||
| 202 | |||
| 203 | $str = $filter->ucfirst($str); | ||
| 204 | |||
| 205 | Upper case the first letter in a string. | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | sub ucfirst { | ||
| 210 | my ($self, $str) = @_; | ||
| 211 | ucfirst($str); | ||
| 212 | } | ||
| 213 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 214 | =head2 lcfirst() | ||
| 215 | |||
| 216 | $str = $filter->lcfirst($str); | ||
| 217 | |||
| 218 | Lower case the first letter in a string. | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub lcfirst { | ||
| 223 | my ($self, $str) = @_; | ||
| 224 | lcfirst($str); | ||
| 225 | } | ||
| 226 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 227 | =head2 email() | ||
| 228 | |||
| 229 | $valid_email = $filter->email('ahmed@email.com'); | ||
| 230 | |||
| 231 | Returns the valid email or undef if not passed. | ||
| 232 | |||
| 233 | =cut | ||
| 234 | |||
| 235 | sub email { | ||
| 236 | my ($self, $email) = @_; | ||
| 237 | $email = CORE::lc($email); | ||
| 238 | $email =~s/\s+//g; | ||
| 239 | return Email::Valid->address($email) ? $email : undef; | ||
| 240 | } | ||
| 241 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 242 | =head2 uri() | ||
| 243 | |||
| 244 | $valid_url = $filter->uri('http://www.domain.com/path/file'); | ||
| 245 | |||
| 246 | Returns the valid url or undef if not passed. | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | sub uri { | ||
| 251 | my ($self, $uri) = @_; | ||
| 252 | $uri =~s/\s+//sg; | ||
| 253 | #my $uri = URI->new($uri); | ||
| 254 | #return $uri->canonical; | ||
| 255 | my $v = Data::Validate::URI->new; | ||
| 256 | return $v->is_uri($uri) ? $uri : undef; | ||
| 257 | } | ||
| 258 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 259 | =head2 digit() | ||
| 260 | |||
| 261 | $digits = $filter->digit($str); | ||
| 262 | |||
| 263 | Removes none digits from string and converts to integer. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub digit { | ||
| 268 | my ($self, $str) = @_; | ||
| 269 | $str =~ s/\D+//g; | ||
| 270 | $str += 0; | ||
| 271 | return $str; | ||
| 272 | } | ||
| 273 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 274 | =head2 number() | ||
| 275 | |||
| 276 | $str = "data -123.45678"; | ||
| 277 | $number = $filter->number($str); | ||
| 278 | say $number; # -123.45678 | ||
| 279 | |||
| 280 | Converts string to number. | ||
| 281 | |||
| 282 | =cut | ||
| 283 | |||
| 284 | sub number { | ||
| 285 | my ($self, $str) = @_; | ||
| 286 | #if ($str =~ /(\+|-)?([0-9]+(\.[0-9]+)?)/) { | ||
| 287 | if ($str =~ /(-)?([0-9]+(\.[0-9]+)?)/) { | ||
| 288 | return $1? "$1$2" : $2; | ||
| 289 | } | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 293 | =head2 commify() | ||
| 294 | |||
| 295 | $number = 12345678; | ||
| 296 | $commified = $filter->commify($number); | ||
| 297 | say $commified; # 12,345,678 | ||
| 298 | |||
| 299 | Format numbers with commas nicely for easy reading. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub commify { | ||
| 304 | my ($self, $str) = @_; | ||
| 305 | $str = reverse $str; | ||
| 306 | $str =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/g; | ||
| 307 | return scalar reverse $str; | ||
| 308 | } | ||
| 309 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 310 | |||
| 311 | =pod | ||
| 312 | |||
| 313 | =head1 Bugs | ||
| 314 | |||
| 315 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 316 | |||
| 317 | =head1 HOMEPAGE | ||
| 318 | |||
| 319 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 320 | |||
| 321 | =head1 SOURCE | ||
| 322 | |||
| 323 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 324 | |||
| 325 | =head1 SEE ALSO | ||
| 326 | |||
| 327 | See L<Nile> for details about the complete framework. | ||
| 328 | |||
| 329 | =head1 AUTHOR | ||
| 330 | |||
| 331 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 332 | Website: http://www.mewsoft.com | ||
| 333 | |||
| 334 | =head1 COPYRIGHT AND LICENSE | ||
| 335 | |||
| 336 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 337 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 338 | |||
| 339 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | 1; |