Skip to content
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

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ac95b5c
added Data helper for basic validation and manipulation
ericuldall Nov 5, 2014
0e1853e
fixed namespace typo
ericuldall Nov 5, 2014
525e7b7
added Session helper
ericuldall Nov 7, 2014
8676b66
upgraded getVar to allow default value and added getVars
ericuldall Nov 7, 2014
838ceb9
added Session::refresh()
ericuldall Nov 7, 2014
5ffa7ef
updated Data:: to PSR-2 coding specs
ericuldall Nov 8, 2014
70c13b7
added ctype_digit check to isInt
ericuldall Nov 8, 2014
a32e683
fixed binary regexp
ericuldall Nov 8, 2014
823482f
updated TRUE|FALSE to true|false
ericuldall Nov 8, 2014
0137f86
added updates for vars support on destroyVar and destroyVars, updated…
ericuldall Nov 8, 2014
cc5f326
switched constant arrays to getter methods
ericuldall Jan 23, 2015
1974db4
fix for ctype usage
ericuldall Jan 23, 2015
186c52a
updated binary grouping to limitless regexp matches
ericuldall Jan 23, 2015
381fe5e
changed self refs to static
ericuldall Jan 23, 2015
7793c5c
fixed timestamp param type in comment
ericuldall Jan 23, 2015
054085f
fixed funky doc blocks
ericuldall Jan 23, 2015
cb4b08c
replaced self with static calls and added LogicException for setting …
ericuldall Jan 23, 2015
f42dba7
removed unneeded ternary
ericuldall Jan 23, 2015
9256cdd
added new default error suppression settings and changed default to f…
ericuldall Jan 23, 2015
d83d407
fixed session end logic
ericuldall Jan 23, 2015
dbb063b
added LogicException to refresh
ericuldall Jan 23, 2015
a3726f3
removed bool return for setVar
ericuldall Jan 23, 2015
3f8fde7
removed return value for setVars
ericuldall Jan 23, 2015
4b182aa
removed check for default val. returns null if no default set
ericuldall Jan 23, 2015
9889d40
removed active check and returned entire session if not vars are spec…
ericuldall Jan 23, 2015
10dfc7c
unpacked destroyVars method and removed return value
ericuldall Jan 23, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions src/Data.php
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
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Author

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.

Copy link
Author

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.


/**
*
* 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}/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\s or a single space?

Copy link
Author

Choose a reason for hiding this comment

The 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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 getInvalidTimestamps

Copy link
Author

Choose a reason for hiding this comment

The 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}/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we talking about DATE columns in SQL, or something else?

Copy link
Author

Choose a reason for hiding this comment

The 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){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not matching PSR-2 code style

Copy link
Author

Choose a reason for hiding this comment

The 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 ) ){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_null and empty are redundent here i think... and strlen needs to be run after is_string.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. empty should suffice. would you recommend a strlen($data) > 0?

Copy link
Author

Choose a reason for hiding this comment

The 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 ) ){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about about ctype_digit?

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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');
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Author

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

*
* // 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param type is "string" not "timestamp":

@param   string   $data  timestamp to check

Copy link
Author

Choose a reason for hiding this comment

The 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;
}

}
Loading