Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format rule for required_without #4196

Open
wants to merge 5 commits into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/Validators/RowValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ private function formatRule($rules)
return $rules;
}

if (Str::contains($rules, 'required_without') && preg_match('/(.*?):(.*)/', $rules, $matches)) {
$column = array_map(function ($match) {
return Str::startsWith($match, '*.') ? $match : '*.' . $match;
}, explode(',', $matches[2]));

return $matches[1] . ':' . implode(',', $column);
}

if (Str::contains($rules, 'required_') && preg_match('/(.*?):(.*),(.*)/', $rules, $matches)) {
$column = Str::startsWith($matches[2], '*.') ? $matches[2] : '*.' . $matches[2];

Expand Down
96 changes: 96 additions & 0 deletions tests/Validators/RowValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Maatwebsite\Excel\Tests\Validators;

use Illuminate\Contracts\Validation\Factory;
use Maatwebsite\Excel\Tests\TestCase;
use Maatwebsite\Excel\Validators\RowValidator;

class RowValidatorTest extends TestCase
{
/**
* The RowValidator instance.
*/
protected $validator;

/**
* Set up the test.
*/
public function setUp(): void
{
parent::setUp();

$this->validator = new RowValidator(app(Factory::class));
}

public function test_format_rule_with_array_input()
{
$rules = ['rule1', 'rule2'];

$result = $this->callPrivateMethod('formatRule', [$rules]);

$this->assertEquals($rules, $result);
}

public function test_format_rule_with_object_input()
{
$rule = new \stdClass();

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

public function test_format_rule_with_callable_input()
{
$rule = function () {
return 'callable';
};

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

public function test_format_rule_with_required_without_all()
{
$rule = 'required_without_all:first_name,last_name';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals('required_without_all:*.first_name,*.last_name', $result);
}

public function test_format_rule_with_required_without()
{
$rule = 'required_without:first_name';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals('required_without:*.first_name', $result);
}

public function test_format_rule_with_string_input_not_matching_pattern()
{
$rule = 'rule';

$result = $this->callPrivateMethod('formatRule', [$rule]);

$this->assertEquals($rule, $result);
}

/**
* Call a private function.
*
* @param string $name
* @param array $args
* @return mixed
*/
public function callPrivateMethod(string $name, array $args)
{
$method = new \ReflectionMethod(RowValidator::class, $name);
$method->setAccessible(true);

return $method->invokeArgs($this->validator, $args);
}
}
Loading