Skip to content

Commit

Permalink
added a validation rule for MAC addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
WanWizard committed Oct 27, 2015
1 parent 268271e commit a8b7554
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Rule/Mac.php
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);
}

}
45 changes: 45 additions & 0 deletions tests/unit/Rule/MacTest.php
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),
);
}

}

0 comments on commit a8b7554

Please sign in to comment.