Skip to content

Commit

Permalink
Added MACAddress pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed Jun 2, 2024
1 parent b71c7cd commit 9f261ba
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ You can start creating your regex by using `Regex::build()`. The `build()` metho
- time
- [`IPv4 address`](usage/patterns#ipv4-address)
- IPv6 address
- mac address
- [`mac address`](usage/patterns#mac-address)
- [`email`](usage/patterns#email)
- [`url`](usage/patterns#url)
- [`credit card`](usage/patterns#credit-card)
16 changes: 16 additions & 0 deletions docs/usage/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ $pattern = Regex::build([\Rudashi\Patterns\IPAddressPattern::class])
// /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
```

### `MAC address`

To find whether a given text contains MAC addresses, you can use the predefined `MACAddressPattern` pattern.
The pattern can distinguish addresses that use not only the default colon `:`, but also dot `.` and dash `-`.

```php
use Rudashi\Regex;

$pattern = Regex::build([\Rudashi\Patterns\MACAddressPattern::class])
->start()
->macAddress()
->end();

// /^(?<![0-9A-Fa-f.:-])(?:[0-9A-Fa-f]{2}[:.-]){5}(?:[0-9A-Fa-f]{2})(?![0-9A-Fa-f:-])$/
```

### `Email`

To verify whether an e-mail address is included in a given text, you can use the predefined `EmailPattern` pattern. It will allow you not only to check whether the e-mail is correct but also to isolate it.
Expand Down
15 changes: 15 additions & 0 deletions src/Patterns/MACAddressPattern.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rudashi\Patterns;

use Rudashi\Contracts\PatternContract;
use Rudashi\Pattern;

class MACAddressPattern extends Pattern implements PatternContract
{
protected string $pattern = '(?<![0-9A-Fa-f.:-])(?:[0-9A-Fa-f]{2}[:.-]){5}(?:[0-9A-Fa-f]{2})(?![0-9A-Fa-f:-])';

public static string $name = 'macAddress';
}
98 changes: 98 additions & 0 deletions tests/Feature/MACAddressTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

use Rudashi\FluentBuilder;
use Rudashi\Patterns\MACAddressPattern;
use Rudashi\Regex;

dataset('macs', [
['01:02:03:04:ab:cd', true],
['01-02-03-04-ab-cd', true],
['01.02.03.04.ab.cd', true],
['49.33.0E.E0.B2.30', true],
['DD.54.D2.0D.3A.1C', true],
['D5.62.40.19.26.8A', true],
['', false],
['F2937E757902', false],
['B538.D6B0.CBF0', false],
['9D4F.B5DD.94C7', false],
['3D:F2:AC9:A6:B3:4F', false],
['3D:F2:C9:A6:B3:4F:00', false],
[':F2:C9:A6:B3:4F', false],
['8C:1F:64:88:A', false],
['8C:1F:64:18:3', false],
['B8:A2:5D', false],
['087A52F9F2AB', false],
['E4D03DD64CDF', false],
['78FBF68497F1', false],
['aa.aa.bbbb.cccc', false],
['', false],

]);

it('validate macs', function (string $context, bool $expectation) {
$regex = Regex::for($context)
->not->group(fn (FluentBuilder $fluent) => $fluent->anyOf(
fn (FluentBuilder $fluent) => $fluent->number()->letter(last: 'f')->character('.:-')
), lookbehind: true)
->not->group(fn (FluentBuilder $fluent) => $fluent
->anyOf(fn (FluentBuilder $fluent) => $fluent->number()->letter(last: 'f'))
->times(2)
->anyOf(fn (FluentBuilder $fluent) => $fluent->character(':.-'))
)->times(5)
->not->group(fn (FluentBuilder $fluent) => $fluent->anyOf(
fn (FluentBuilder $fluent) => $fluent->number()->letter(last: 'f')->times(2)
))
->not->group(fn (FluentBuilder $fluent) => $fluent->anyOf(
fn (FluentBuilder $fluent) => $fluent->number()->letter(last: 'f')->character(':-')
), lookahead: true);

expect($regex)
->toBeInstanceOf(FluentBuilder::class)
->get()->toBe('/(?<![0-9a-fA-F.:-])(?:[0-9a-fA-F]{2}[:.-]){5}(?:[0-9a-fA-F]{2})(?![0-9a-fA-F:-])/')
->check()->toBe($expectation);
})->with('macs')
->todo();

describe('predefined MAC ADDRESS pattern', function () {
beforeEach(function () {
$this->builder = new FluentBuilder(patterns: [MACAddressPattern::class]);
});

it('validates', function (string $context, bool $expectation) {
$regex = $this->builder->setContext($context)->start()->macAddress()->end();

expect($regex->check())
->toBe($expectation);
})->with('macs');

it('finds a mac addresses in text', function () {
$context = "Each device in the network has a unique MAC address, for example, 00:1A:2B:3C:4D:5E. MAC addresses
are used to identify devices in a local network. Another example of a MAC address is '66-77-88-99-AA-BB-66'.
Remember that MAC addresses can be written with colons or hyphens. Here is another example: '11:22:33:44:55:66'.
MAC addresses are assigned to network interfaces, which enables communication between devices.
The last example of a MAC address is CC-DD-EE-FF-00-11-aa.";

$regex = $this->builder
->setContext($context)
->boundary()
->macAddress()
->boundary();

expect($regex->match())
->toHaveCount(2)
->toMatchArray([
'00:1A:2B:3C:4D:5E',
'11:22:33:44:55:66',
]);
});
});

it('check MACAddressPattern', function () {
$pattern = new MACAddressPattern();

expect($pattern)
->getName()->toBe('macAddress')
->getPattern()->toBe('(?<![0-9A-Fa-f.:-])(?:[0-9A-Fa-f]{2}[:.-]){5}(?:[0-9A-Fa-f]{2})(?![0-9A-Fa-f:-])');
});

0 comments on commit 9f261ba

Please sign in to comment.