Skip to content

Commit

Permalink
Added url pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
rudashi committed May 17, 2024
1 parent de25811 commit 0db522a
Show file tree
Hide file tree
Showing 4 changed files with 116 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 @@ -72,5 +72,5 @@ You can start creating your regex by using `Regex::build()`. The `build()` metho
- IPv6 address
- mac address
- [`email`](usage/patterns#email)
- url
- [`url`](usage/patterns#url)
- [`credit card`](usage/patterns#credit-card)
15 changes: 15 additions & 0 deletions docs/usage/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ $pattern = Regex::build([\Rudashi\Patterns\EmailPattern::class])
// /^\w+(?:[\.\-]\w+)*@([\w-]+\.)+[\w-]{2,}$/
```

### `Url`

To check whether a given text contains a website, you can use the predefined `UrlPattern` pattern. It only accepts addresses with the **http** or https **protocol** entered.

```php
use Rudashi\Regex;

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

// /^https?\:\/\/[^-][a-z\d.-]+[^-]\.[a-z]{2,}(\/[a-z\d\/-]*)?$/
```

### `Credit card`

To find if there is any credit card number in a given text, you can use the predefined `CreditCardPattern` pattern. The pattern identifies **Visa** and **MasterCard** cards.
Expand Down
15 changes: 15 additions & 0 deletions src/Patterns/UrlPattern.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 UrlPattern extends Pattern implements PatternContract
{
protected string $pattern = 'https?\:\/\/[^-][a-z\d.-]+[^-]\.[a-z]{2,}(\/[a-z\d\/-]*)?';

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

declare(strict_types=1);

use Rudashi\FluentBuilder;
use Rudashi\Patterns\UrlPattern;
use Rudashi\Regex;

dataset('urls', [
['http://github.com', true],
['http://www.github.com', true],
['https://github.com', true],
['https://www.github.com', true],
['https://github.com/blog', true],
['https://foobar.github.co', true],
['https://www.example.com', true],
['http://social.example.org', true],
['http://social.example.org/profile/name', true],
['http://social.example.org/profil-asd4e/name', true],
['http://4-social.example.org', true],
['http://social.example.com.pl', true],
['https://www.example.com/profile', true],
['', false],
['www.creative-business.com', false],
['http://-example.com', false],
['http://example-.com', false],
[' http://github.com', false],
['foo', false],
['htps://github.com', false],
['http:/github.com', false],
['https://github.com /blog', false],
['https://.com', false],
['http://.com', false],
]);

it('validate urls', function (string $context, bool $expectation) {
$regex = Regex::for($context)
->start()
->exactly('https')->zeroOrOne()
->then("://")
->not->character('-')
->anyOf(
fn (FluentBuilder $fluent) => $fluent->lowerLetter()->digit()->character('.-')
)->oneOrMore()
->not->character('-')
->then('.')
->lowerLetter()->min(2)
->maybe(fn (FluentBuilder $fluent) => $fluent->exactly('/')->anyOf(
fn (FluentBuilder $fluent) => $fluent->lowerLetter()->digit()->exactly('/')->character('-')
)->zeroOrMore())
->utf8()
->end();

expect($regex)
->toBeInstanceOf(FluentBuilder::class)
->get()->toBe('/^https?\:\/\/[^-][a-z\d.-]+[^-]\.[a-z]{2,}(\/[a-z\d\/-]*)?$/u')
->check()->toBe($expectation);
})->with('urls');

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

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

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

it('finds a credit cards in text', function () {
$context = "Find the best business solutions at http://develop-yourself.com. Need some inspiration? Visit \n
www.creative-business.com. And if you're looking for entertainment, check out https://creative-hobbies.com!";

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

expect($regex->match())
->toHaveCount(2)
->toMatchArray([
'http://develop-yourself.com',
'https://creative-hobbies.com',
]);
});
});

0 comments on commit 0db522a

Please sign in to comment.