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 all 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
225 changes: 225 additions & 0 deletions src/Data.php
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');
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
*/
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

this could just be return (bool) filter_var(...); without the if statement. though i guess similar changes could be made with almost every method here. i'm fine with it either way.

Copy link
Author

Choose a reason for hiding this comment

The 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 ){
//process success code
}

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