-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aded Data Helper and Session Helper #12
base: master
Are you sure you want to change the base?
Changes from all commits
ac95b5c
0e1853e
525e7b7
8676b66
838ceb9
5ffa7ef
70c13b7
a32e683
823482f
0137f86
cc5f326
1974db4
186c52a
381fe5e
7793c5c
054085f
cb4b08c
f42dba7
9256cdd
d83d407
dbb063b
a3726f3
3f8fde7
4b182aa
9889d40
10dfc7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
<?php | ||
|
||
namespace Ohanzee\Helper; | ||
|
||
class Data | ||
{ | ||
/** | ||
* | ||
* Regexp to validate string as Binary | ||
* @var PCRE | ||
* | ||
*/ | ||
const BINARY_REGEXP = '/(?:[01]{8}\s?){1,}/'; | ||
|
||
/** | ||
* | ||
* Regexp to validate string as Timestamp | ||
* @var PCRE | ||
* | ||
*/ | ||
const TIMESTAMP_REGEXP = '/\d{4}-\d{1,2}-\d{1,2}\s\d{2}:\d{2}:\d{2}/'; | ||
|
||
/** | ||
* | ||
* Regexp to validate string as Date | ||
* @var PCRE | ||
* | ||
*/ | ||
const DATE_REGEXP = '/\d{4}-\d{1,2}-\d{1,2}/'; | ||
|
||
/** | ||
* | ||
* Array of invalid Date values - Getter Method | ||
* @var array | ||
* | ||
*/ | ||
public static function getInvalidDates(){ | ||
return array( | ||
'0000-00-00' | ||
); | ||
} | ||
|
||
/** | ||
* | ||
* Array of invalid Timestamp values - Getter Method | ||
* @var array | ||
* | ||
*/ | ||
|
||
public static function getInvalidTimestamps(){ | ||
return array( | ||
'0000-00-00 00:00:00' | ||
); | ||
} | ||
|
||
/** | ||
* Tests if data is a valid string | ||
* | ||
* // Returns true | ||
* Data::isString('some string'); | ||
* | ||
* // Returns false | ||
* Data::isString(98765); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isString($data) | ||
{ | ||
if (!empty($data) && is_string( $data ) && strlen($data) > 0) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid integer | ||
* | ||
* // Returns true | ||
* Data::isInt(98765); | ||
* | ||
* // Returns false | ||
* Data::isInt('some string'); | ||
* | ||
* @param integer $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isInt($data) | ||
{ | ||
if (is_int($data) || (is_string($data) && ctype_digit($data))) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid float | ||
* | ||
* // Returns true | ||
* Data::isFloat(987.65); | ||
* | ||
* // Returns false | ||
* Data::isFloat(98765); | ||
* | ||
* @param float $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isFloat($data) | ||
{ | ||
if (!empty($data) && is_float($data)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid binary string | ||
* | ||
* // Returns true | ||
* Data::isBinary('01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100'); | ||
* | ||
* // Returns false | ||
* Data::isBinary('some non binary string'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isBinary($data) | ||
{ | ||
if (!empty($data) && preg_match(static::BINARY_REGEXP, $data)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid timestamp | ||
* | ||
* // Returns true | ||
* Data::isTimestamp('2014-10-31 12:22:36'); | ||
* | ||
* // Returns false | ||
* Data::isTimestamp('0000-00-00 00:00:00'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
//TODO: Add support for "real" datetime check, instead of possibly allowing something like 2014-99-99 88:77:66 | ||
public static function isTimestamp($data) | ||
{ | ||
if (!empty($data) && preg_match(static::TIMESTAMP_REGEXP, $data) && !in_array($data, static::getInvalidTimestamps())) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid date | ||
* | ||
* // Returns true | ||
* Data::isDate('2014-10-31'); | ||
* | ||
* // Returns false | ||
* Data::isDate('0000-00-00'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
//TODO: Add support for "real" date check, instead of possibly allowing something like 2014-99-99 | ||
public static function isDate($data) | ||
{ | ||
if (!empty($data) && preg_match(static::DATE_REGEXP, $data) && !in_array($data, static::getInvalidDates())) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid email | ||
* | ||
* // Returns true | ||
* Data::isEmail('[email protected]'); | ||
* | ||
* // Returns false | ||
* Data::isEmail('foo[at]bar[dot]com'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isEmail($data) | ||
{ | ||
if (filter_var($data, FILTER_VALIDATE_EMAIL)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should leave it as is, for the following reason. I would expect the following code to work: if( is_email('[email protected]') === true ){ just to stay true to the is_* returning a boolean. |
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Tests if data is a valid url | ||
* | ||
* // Returns true | ||
* Data::isUrl('http://devlifeline.com'); | ||
* | ||
* // Returns false | ||
* Data::isUrl('#definitelynotarealurl'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
public static function isUrl($data) | ||
{ | ||
if (filter_var($data, FILTER_VALIDATE_URL)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regex doesn't have anything that accounts for spaces.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do some testing on that tonght. It's the only one I've never had a real use case for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated binary regexp after testing