-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a validation rule for MAC addresses
- Loading branch information
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* @package Fuel\Validation | ||
* @version 2.0 | ||
* @author Fuel Development Team | ||
* @license MIT License | ||
* @copyright 2010 - 2013 Fuel Development Team | ||
* @link http://fuelphp.com | ||
*/ | ||
|
||
namespace Fuel\Validation\Rule; | ||
|
||
use Fuel\Validation\AbstractRule; | ||
|
||
/** | ||
* Validates that a field is a valid IP address. Returns true for both IPv4 and v6. | ||
* | ||
* @package Fuel\Validation\Rule | ||
* @author Fuel Development Team | ||
* | ||
* @since 2.0 | ||
*/ | ||
class Mac extends AbstractRule | ||
{ | ||
|
||
/** | ||
* Contains the rule failure message | ||
* | ||
* @var string | ||
*/ | ||
protected $message = 'The field is not a valid MAC address.'; | ||
|
||
/** | ||
* Returns true if the given value is a valid MAC address | ||
* | ||
* @param mixed $value Value to validate | ||
* @param string $field Unused by this rule | ||
* @param array $allFields Unused by this rule | ||
* | ||
* @return bool | ||
* | ||
* @since 2.0 | ||
*/ | ||
public function validate($value, $field = null, $allFields = null) | ||
{ | ||
return (preg_match('/([a-fA-F0-9]{2}[:|\-]?){6}/', $value) == 1); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* @package Fuel\Validation | ||
* @version 2.0 | ||
* @author Fuel Development Team | ||
* @license MIT License | ||
* @copyright 2010 - 2013 Fuel Development Team | ||
* @link http://fuelphp.com | ||
*/ | ||
|
||
namespace Fuel\Validation\Rule; | ||
|
||
class MacTest extends AbstractRuleTest | ||
{ | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
protected $message = 'The field is not a valid MAC address.'; | ||
|
||
protected function _before() | ||
{ | ||
$this->object = new Mac; | ||
} | ||
|
||
/** | ||
* {@inheritdocs} | ||
*/ | ||
public function validateProvider() | ||
{ | ||
return array( | ||
array('000CF15698AD', true), | ||
array('00:0C:F1:56:98:AD', true), | ||
array('00-0C-F1-56-98-AD', true), | ||
array('000Cf15698ad', true), | ||
array('00-0c:f1:56-98ad', true), | ||
array('CF15698AD', false), | ||
array('00:0C:Q1:56:98:AD', false), | ||
array('00-0C-F1/56-98-AD', false), | ||
array('000Cf15698aq', false), | ||
array('0c:f1:56-98ad', false), | ||
); | ||
} | ||
|
||
} |