Skip to content

Commit

Permalink
Refactoring of the Cleaner API
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk committed Oct 15, 2019
1 parent 28c2367 commit 26f438d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 13 deletions.
11 changes: 11 additions & 0 deletions src/Exceptions/CallableNotResolvableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

namespace ItalyStrap\Cleaner\Exceptions;

use InvalidArgumentException;

class CallableNotResolvableException extends InvalidArgumentException
{

}
12 changes: 12 additions & 0 deletions src/Exceptions/IncorrectRuleTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Cleaner\Exceptions;

use InvalidArgumentException;

class IncorrectRuleTypeException extends InvalidArgumentException
{

}
12 changes: 12 additions & 0 deletions src/Exceptions/NoRuleWasProvidedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace ItalyStrap\Cleaner\Exceptions;

use InvalidArgumentException;

class NoRuleWasProvidedException extends InvalidArgumentException
{

}
2 changes: 1 addition & 1 deletion src/Rules_Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function addRules( $rules ): self {
}

if ( ! \is_array( $rules ) ) {
throw new \InvalidArgumentException( 'Incorrect $rules type, only strings and arrays are accepted', 0 );
throw new Exceptions\IncorrectRuleTypeException( 'Incorrect $rules type, only strings and arrays are accepted', 0 );
}

$this->rules = \array_merge( $this->rules, $rules );
Expand Down
4 changes: 2 additions & 2 deletions src/Sanitization.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Sanitization implements Sanitizable_Interface {
public function sanitize( $data = '' ) {

if ( ! $this->count() ) {
throw new \RuntimeException( 'No rule was provided, use ::addRules( $rules )', 0 );
throw new Exceptions\NoRuleWasProvidedException( 'No rule was provided, use ::addRules( $rules )', 0 );
}

foreach ( $this->getRules() as $rule ) {
Expand Down Expand Up @@ -72,7 +72,7 @@ public function sanitize( $data = '' ) {
private function do_filter( $rule, $data ) {

if ( ! \is_callable( $rule ) ) {
throw new \InvalidArgumentException( 'Could not resolve a callable', 0 );
throw new Exceptions\CallableNotResolvableException( 'Could not resolve a callable', 0 );
}

return \call_user_func( $rule, $data );
Expand Down
4 changes: 3 additions & 1 deletion src/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Validation implements Validable_Interface {
/**
* Validate the give value
*
* @todo Build a message error in case validation fail in ajax requests
*
* @todo Aggiungere rule required
* Prendere spunto da questo articolo
* https://tommcfarlin.com/validation-and-sanitization-wordpress-settings-api
Expand All @@ -36,7 +38,7 @@ class Validation implements Validable_Interface {
public function validate( $data = '' ): bool {

if ( ! $this->count() ) {
throw new \RuntimeException( 'No rule was provided, use ::addRules( $rules )', 0 );
throw new Exceptions\NoRuleWasProvidedException( 'No rule was provided, use ::addRules( $rules )', 0 );
}

foreach ( $this->getRules() as $rule ) {
Expand Down
18 changes: 9 additions & 9 deletions tests/unit/SanitizationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use ItalyStrap\Cleaner\IncorrectRuleTypeException;
use ItalyStrap\Cleaner\Sanitization;

class SanitizationTest extends \Codeception\Test\Unit
Expand All @@ -22,27 +23,26 @@ public function testThrownRuntimeExceptionIfNoRulesAreProvided()
{
$sut = new Sanitization();

$this->expectException( '\RuntimeException' );
$this->expectException( '\ItalyStrap\Cleaner\Exceptions\NoRuleWasProvidedException' );
$sut->sanitize( 'Value' );
}

// tests
public function testThrownInvalidArgumentExceptionIfCallbackIsNotExiting()
// tests
public function testThrownInvalidArgumentExceptionIfIncorrectRuleType()
{
$sut = new Sanitization();
$this->expectException( '\InvalidArgumentException' );
$this->expectException( '\ItalyStrap\Cleaner\Exceptions\IncorrectRuleTypeException' );

$sut->addRules('callbackNotExisting');
$sut->sanitize( 'Value' );
$sut->addRules( 1 );
}

// tests
public function testThrownInvalidArgumentExceptionIfCouldNotResolveCallable()
public function testThrownInvalidArgumentExceptionIfCallbackIsNotExiting()
{
$sut = new Sanitization();
$this->expectException( '\InvalidArgumentException' );
$this->expectException( '\ItalyStrap\Cleaner\Exceptions\CallableNotResolvableException' );

$sut->addRules( 1 );
$sut->addRules('callbackNotExisting');
$sut->sanitize( 'Value' );
}

Expand Down

0 comments on commit 26f438d

Please sign in to comment.