Skip to content

Commit

Permalink
Added 'InKeys' rule
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalbaljet committed Oct 13, 2020
1 parent d916138 commit 3b787be
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Changelog

All notable changes to `laravel-mixins` will be documented in this file
All notable changes to `laravel-mixins` will be documented in this file.

## 1.0.0 - 2020-08-xx
## 2.1.0 - 2020-10-13

- Initial release
- Added `InKeys` rule.

## 2.0.0 - 2020-10-05

- [ConvertsBase64ToFiles] Renamed `base64ImageKeys` to `base64FileKeys`.

## 1.0.0 - 2020-09-22

- Initial release.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ There's no Service Provider or automatic discovery/registration of anything. All
* [Current password](#current-password)
* [Dimensions With Margin](#dimensions-with-margin)
* [Host](#host)
* [In Keys](#in-keys)
* [Max Words](#max-words)
* [URL Without Scheme](#url-without-scheme)

Expand Down Expand Up @@ -163,6 +164,22 @@ use ProtoneMedia\LaravelMixins\Rules\Host;
$rule = Host::make(['facebook.com', 'fb.me']);
```

### In Keys

Verifies if the given key or index exists in the array.

```php
use ProtoneMedia\LaravelMixins\Rules\InKeys;

$rule = new InKeys(['laravel' => 'Laravel Framework', 'tailwindcss' => 'Tailwind CSS framework']);

// same as

use Illuminate\Validation\Rules\In;

$rule = new In(['laravel', 'tailwindcss']);
```

### Max Words

Passes if the values contains no more words than specified.
Expand Down
24 changes: 24 additions & 0 deletions src/Rules/InKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ProtoneMedia\LaravelMixins\Rules;

use Illuminate\Validation\Rules\In;

class InKeys extends In
{
/**
* Create a new rule instance with the keys of the given values.
*
* @param array $values
* @return void
*/
public function __construct(array $values)
{
$this->values = array_keys($values);
}

public static function make(array $values): self
{
return new static($values);
}
}
26 changes: 26 additions & 0 deletions tests/Rules/InKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ProtoneMedia\Mixins\Tests\Rules;

use Illuminate\Support\Facades\Validator;
use Orchestra\Testbench\TestCase;
use ProtoneMedia\LaravelMixins\Rules\InKeys;

class InKeysTest extends TestCase
{
/** @test */
public function it_validates_the_keys()
{
$rule = InKeys::make([
'a' => 'foo',
'b' => 'bar',
'c' => 'baz',
]);

$validator = Validator::make([], ['attribute' => $rule]);

$this->assertTrue($validator->setData(['attribute' => 'a'])->passes());
$this->assertFalse($validator->setData(['attribute' => 'd'])->passes());
$this->assertFalse($validator->setData(['attribute' => 'foo'])->passes());
}
}

0 comments on commit 3b787be

Please sign in to comment.