Skip to content

Commit

Permalink
Merge pull request #52 from mikey179/master
Browse files Browse the repository at this point in the history
add net\stubbles\input\ValueReader::asMonth()
  • Loading branch information
mikey179 committed May 7, 2014
2 parents 1d46ea8 + 2695f71 commit 0440d9f
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2.5.1 (2014-05-07)
------------------

* added `net\stubbles\input\ValueReader::asMonth()`


2.5.0 (2014-05-06)
------------------

Expand Down
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_create_2e_tests=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=true
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.bootstrap_2e_path=vendor/autoload.php
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=true
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.configuration_2e_path=phpunit.xml.dist
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.customSuite_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_enabled=false
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path=
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_enabled=true
auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path=vendor/bin/phpunit
auxiliary.org-netbeans-modules-php-phpunit.test_2e_groups_2e_ask=false
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_all=false
file.reference.docs-api=docs/api
Expand Down
22 changes: 22 additions & 0 deletions src/main/php/net/stubbles/input/ValueReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use net\stubbles\input\filter\range\NumberRange;
use net\stubbles\lang\types\Date;
use net\stubbles\lang\types\datespan\Day;
use net\stubbles\lang\types\datespan\Month;
use net\stubbles\peer\http\HttpUri;
/**
* Value object for request values to filter them or retrieve them after validation.
Expand Down Expand Up @@ -356,6 +357,27 @@ public function asDay(Day $default = null, DatespanRange $range = null)
);
}

/**
* read as month
*
* @api
* @param Month $default
* @param DatespanRange $range
* @return \net\stubbles\lang\types\datespan\Month
* @since 2.5.1
*/
public function asMonth(Month $default = null, DatespanRange $range = null)
{
return $this->handleFilter(function() use($range)
{
return filter\RangeFilter::wrap(new filter\MonthFilter(),
$range
);
},
$default
);
}

/**
* returns value if it is an ip address, and null otherwise
*
Expand Down
52 changes: 52 additions & 0 deletions src/main/php/net/stubbles/input/filter/MonthFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package net\stubbles\input
*/
namespace net\stubbles\input\filter;
use net\stubbles\input\Filter;
use net\stubbles\input\Param;
use net\stubbles\lang\types\datespan\Month;
use net\stubbles\lang\exception\IllegalArgumentException;
/**
* Class for filtering months.
*
* The following rules apply:
* - If given value is empty the returned value is null.
* - If given value is not a valid month the returned value is null.
* - If given value is a valid month the returned value is an instance of
* net\stubbles\lang\types\datespan\Month.
*
* @since 2.5.1
*/
class MonthFilter implements Filter
{
/**
* apply filter on given param
*
* In case the given value can not be transformed into the target type
* the return value is null. Additionally the $param instance is filled
* with a FilterError.
*
* @param Param $param
* @return Date
*/
public function apply(Param $param)
{
if ($param->isEmpty()) {
return null;
}

try {
return Month::fromString($param->getValue());
} catch (IllegalArgumentException $iae) {
$param->addErrorWithId('MONTH_INVALID');
}

return null;
}
}
5 changes: 5 additions & 0 deletions src/main/resources/input/error/message.ini
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,8 @@ de_*="Die Datei {PATH} existiert nicht."
default="The directory {PATH} does not exist."
en_*="The directory {PATH} does not exist."
de_*="Die Varzeichnis {PATH} existiert nicht."

[MONTH_INVALID]
default="Please select a correct month."
en_*="Please select a correct month."
de_*="Bitte wählen Sie einen korrekten Monat."
173 changes: 173 additions & 0 deletions src/test/php/net/stubbles/input/filter/MonthFilterTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package net\stubbles\input
*/
namespace net\stubbles\input\filter;
use net\stubbles\input\filter\range\DatespanRange;
use net\stubbles\lang\types\Date;
use net\stubbles\lang\types\datespan\Month;
require_once __DIR__ . '/FilterTestCase.php';
/**
* Tests for net\stubbles\input\filter\MonthFilter.
*
* @group filter
* @since 2.5.1
*/
class MonthFilterTestCase extends FilterTestCase
{
/**
* instance to test
*
* @type MonthFilter
*/
private $monthFilter;

/**
* set up test environment
*/
public function setUp()
{
$this->monthFilter = new MonthFilter();
parent::setUp();
}

/**
* @return scalar
*/
public function getEmptyValues()
{
return array(array(''),
array(null)
);
}

/**
* @param scalar $value
* @test
* @dataProvider getEmptyValues
*/
public function emptyParamsAreReturnedAsNull($value)
{
$this->assertNull($this->monthFilter->apply($this->createParam($value)));
}

/**
* @test
*/
public function validParamsAreReturnedAsDateInstance()
{
$month = $this->monthFilter->apply($this->createParam('2008-09-27'));
$this->assertInstanceOf('net\stubbles\lang\types\datespan\Month', $month);
$this->assertEquals('2008-09', $month->asString());
}

/**
* @test
*/
public function applyReturnsNullForInvalidMonth()
{
$this->assertNull($this->monthFilter->apply($this->createParam('invalid day')));
}

/**
* @test
*/
public function applyAddsErrorForInvalidDay()
{
$param = $this->createParam('invalid day');
$this->monthFilter->apply($param);
$this->assertTrue($param->hasError('MONTH_INVALID'));
}

/**
* @test
*/
public function asMonthReturnsNullIfParamIsNullAndNotRequired()
{
$this->assertNull($this->createValueReader(null)->asMonth());
}

/**
* @test
*/
public function asMonthReturnsDefaultIfParamIsNullAndNotRequired()
{
$default = new Month();
$this->assertEquals($default,
$this->createValueReader(null)
->asMonth($default)
);
}

/**
* @test
*/
public function asMonthReturnsNullIfParamIsNullAndRequired()
{
$this->assertNull($this->createValueReader(null)->required()->asMonth());
}

/**
* @test
*/
public function asMonthAddsParamErrorIfParamIsNullAndRequired()
{
$this->createValueReader(null)->required()->asMonth();
$this->assertTrue($this->paramErrors->existForWithId('bar', 'FIELD_EMPTY'));
}

/**
* @test
*/
public function asMonthReturnsNullIfParamIsInvalid()
{
$this->assertNull($this->createValueReader('foo')->asMonth());
}

/**
* @test
*/
public function asDayAddsParamErrorIfParamIsInvalid()
{
$this->createValueReader('foo')->asMonth();
$this->assertTrue($this->paramErrors->existFor('bar'));
}

/**
* @test
*/
public function asMonthReturnsValidValue()
{
$this->assertEquals('2012-03',
$this->createValueReader('2012-03-11')
->asMonth()
->asString()
);

}

/**
* @test
*/
public function asMonthReturnsNullIfParamIsOutOfRange()
{
$this->assertNull($this->createValueReader(new Month())
->asMonth(null, new DatespanRange(Date::now(), null))
);
}

/**
* @test
*/
public function asMonthAddsParamErrorIfParamIsOutOfRange()
{
$this->createValueReader(new Month())
->asMonth(null, new DatespanRange(Date::now(), null));
$this->assertTrue($this->paramErrors->existFor('bar'));
}
}

0 comments on commit 0440d9f

Please sign in to comment.