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

proposal: refactoring of options configuration #60

Open
wants to merge 1 commit into
base: 2.14.x
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
13 changes: 2 additions & 11 deletions src/Between.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,8 @@ public function __construct($options = null)
$options = ArrayUtils::iteratorToArray($options);
}
if (! is_array($options)) {
$options = func_get_args();
$temp['min'] = array_shift($options);
if (! empty($options)) {
$temp['max'] = array_shift($options);
}

if (! empty($options)) {
$temp['inclusive'] = array_shift($options);
}

$options = $temp;
$validatorOptions = new ValidatorOptionsObject(['min', 'max', 'inclusive']);
$options = $validatorOptions->argumentsAsArray(func_get_args());
}

if (! array_key_exists('min', $options) || ! array_key_exists('max', $options)) {
Expand Down
14 changes: 3 additions & 11 deletions src/StringLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

class StringLength extends AbstractValidator
{
use ValidatorOptionsTrait;

const INVALID = 'stringLengthInvalid';
const TOO_SHORT = 'stringLengthTooShort';
const TOO_LONG = 'stringLengthTooLong';
Expand Down Expand Up @@ -52,17 +54,7 @@ class StringLength extends AbstractValidator
public function __construct($options = [])
{
if (! is_array($options)) {
$options = func_get_args();
$temp['min'] = array_shift($options);
if (! empty($options)) {
$temp['max'] = array_shift($options);
}

if (! empty($options)) {
$temp['encoding'] = array_shift($options);
}

$options = $temp;
$options = $this->argumentsAsArray(['min', 'max', 'encoding'], func_get_args());
}

parent::__construct($options);
Expand Down
34 changes: 34 additions & 0 deletions src/ValidatorOptionsObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace Laminas\Validator;

class ValidatorOptionsObject
{
private $keys;

public function __construct(array $keys)
{
$this->shouldHaveOnlyStringValues($keys);
$this->keys = $keys;
}

private function shouldHaveOnlyStringValues(array $keys) : void
{
foreach ($keys as $key) {
if (! is_string($key)) {
throw new \InvalidArgumentException('The values of $keys should be only strings');
}
}
}

public function argumentsAsArray(array $args) : array
{
$keys = $this->keys;
$result = [];
foreach ($args as $arg) {
$result[array_shift($keys)] = $arg;
}
return $result;
}
}
25 changes: 25 additions & 0 deletions src/ValidatorOptionsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Laminas\Validator;

trait ValidatorOptionsTrait
{
public function argumentsAsArray(array $keys, array $args) : array
{
$this->shouldHaveOnlyStringValues($keys);
$result = [];
foreach ($args as $arg) {
$result[array_shift($keys)] = $arg;
}
return $result;
}

private function shouldHaveOnlyStringValues(array $keys) : void
{
foreach ($keys as $key) {
if (! is_string($key)) {
throw new \InvalidArgumentException('The values of $keys should be only strings');
}
}
}
}
19 changes: 11 additions & 8 deletions test/BetweenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,19 +258,22 @@ public function testMissingMinOrMax(array $args)
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Missing option: 'min' and 'max' have to be given");

new Between($args);
new Between(...$args);
}

public function constructBetweenValidatorInvalidDataProvider()
{
return [
'only-min' => [['min' => 1]],
'only-max' => [['max' => 5]],
'min-inclusive' => [['min' => 0, 'inclusive' => true]],
'max-inclusive' => [['max' => 5, 'inclusive' => true]],
'min-undefined' => [['min' => 0, 'foo' => 'bar']],
'max-undefined' => [['max' => 5, 'foo' => 'bar']],
'no-min-or-max' => [['bar' => 'foo', 'foo' => 'bar']],
'array-only-min' => [[['min' => 1]]],
'array-only-max' => [[['max' => 5]]],
'array-min-inclusive' => [[['min' => 0, 'inclusive' => true]]],
'array-max-inclusive' => [[['max' => 5, 'inclusive' => true]]],
'array-min-undefined' => [[['min' => 0, 'foo' => 'bar']]],
'array-max-undefined' => [[['max' => 5, 'foo' => 'bar']]],
'array-no-min-or-max' => [[['bar' => 'foo', 'foo' => 'bar']]],

'arguments-only-min' => [[1]],
'arguments-no-min-or-max' => [[]],
];
}

Expand Down
24 changes: 24 additions & 0 deletions test/GreaterThanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ public function testBasic()
}
}

/**
* @covers \Laminas\Validator\GreaterThan::__construct()
* @dataProvider constructGreaterThanValidatorInvalidDataProvider
*
* @param array $args
*/
public function testMissingMin(array $args)
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Missing option 'min'");

new GreaterThan(...$args);
}

public function constructGreaterThanValidatorInvalidDataProvider()
{
return [
'array-only-inclusive' => [[['inclusive' => true]]],
'array-inclusive' => [[['foo' => 5, 'inclusive' => true]]],
'array-only-fake' => [[['bar' => 'foo',]]],
'array-only-fake-2' => [[['bar' => 'foo', 'foo' => 'bar']]],
];
}

/**
* Ensures that getMessages() returns expected default value
*
Expand Down