-
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 5 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,216 @@ | ||
<?php | ||
|
||
namespace Ohanzee\Helper; | ||
|
||
class Data { | ||
|
||
/** | ||
* | ||
* Regexp to validate string as Binary | ||
* @var PCRE | ||
* | ||
*/ | ||
private static $binary_regexp = '/^(?:[01]{8}){0,12}$/'; // UNTESTED | ||
|
||
/** | ||
* | ||
* Regexp to validate string as Timestamp | ||
* @var PCRE | ||
* | ||
*/ | ||
private static $timestamp_regexp = '/\d{4}-\d{1,2}-\d{1,2}\s\d{2}:\d{2}:\d{2}/'; | ||
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.
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. yes. \s is checking for a space between date and time. |
||
|
||
/** | ||
* | ||
* Array of invalid Timestamp values | ||
* @var array | ||
* | ||
*/ | ||
private static $invalid_timestamps = array( | ||
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. Because arrays cannot be constants, I would convert them into getter methods eg 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. Sorry for the long delay. Been working a new job/busy during the holidays. Moved constant arrays to getter methods. |
||
'0000-00-00 00:00:00' | ||
); | ||
|
||
/** | ||
* | ||
* Regexp to validate string as Date | ||
* @var PCRE | ||
* | ||
*/ | ||
private static $date_regexp = '/\d{4}-\d{1,2}-\d{1,2}/'; | ||
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. are we talking about 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. The original use case was based on MySQL timestamps, but can be adapted to fit any, via Regexp. |
||
|
||
/** | ||
* | ||
* Array of invalid Date values | ||
* @var array | ||
* | ||
*/ | ||
private static $invalid_dates = array( | ||
'0000-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){ | ||
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 is not matching PSR-2 code style 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. updated to psr-2 spec |
||
if( !empty( $data ) && strlen( $data ) > 0 && !is_null( $data ) && is_string( $data ) ){ | ||
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.
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. yes. empty should suffice. would you recommend a strlen($data) > 0? 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. Removed is_null and moved strlen to end of conditions |
||
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( !empty( $data ) && is_int( $data ) ){ | ||
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. what about about 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 like the ctype_digit idea. That way a user could theoretically pass in a string that is a valid integer. 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. added ctype digit check |
||
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'); | ||
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. regex doesn't have anything that accounts for spaces. 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'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 commentThe reason will be displayed to describe this comment to others. Learn more. updated binary regexp after testing |
||
* | ||
* // Returns false | ||
* Data::isBinary('some non binary string'); | ||
* | ||
* @param string $data data to check | ||
* @return boolean | ||
*/ | ||
/* | ||
UNTESTED - NOT EVEN SURE IF THERE'S MUCH USE CASE FOR THIS METHOD | ||
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 timestamp $data data to check | ||
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. param type is "string" not "timestamp":
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. fixed |
||
* @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::$invalid_timestamps) ){ | ||
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::$invalid_dates) ){ | ||
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) ){ | ||
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.
we should avoid
static
whenever possible in Ohanzee.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.
What do you prefer in that instance? const??
Context: I strayed away from 'const' originally because the data would be public, I guess it seems trivial for it not to be in this case.
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 also, notice that you are calling local methods using static::method(), do you prefer that to self::method(). Not sure if there's a need for static::method() in a no dependency library since there should be no late static bindings. Unless you foresee users extending these classes in such a way.
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.
replaced static props with constants where possible.