Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

add AcceptFilter #28

Merged
merged 2 commits into from

1 participant

@mikey179
Owner

No description provided.

@mikey179 mikey179 merged commit c389c6a into stubbles:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 20, 2012
  1. @mikey179

    fix typo

    mikey179 authored
  2. @mikey179

    add AcceptFilter

    mikey179 authored
This page is out of date. Refresh to see the latest.
View
2  src/main/php/net/stubbles/input/ParamErrors.php
@@ -101,7 +101,7 @@ public function get()
* returns a list of errors for given param
*
* @param string $paramName
- * @return FilterError[]
+ * @return ParamError[]
*/
public function getFor($paramName)
{
View
41 src/main/php/net/stubbles/input/filter/AcceptFilter.php
@@ -0,0 +1,41 @@
+<?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\BaseObject;
+use net\stubbles\peer\http\AcceptHeader;
+/**
+ * Filters accept headers.
+ *
+ * @since 2.0.0
+ */
+class AcceptFilter extends BaseObject implements Filter
+{
+ /**
+ * apply filter on given param
+ *
+ * @param Param $param
+ * @return AcceptHeader
+ */
+ public function apply(Param $param)
+ {
+ if ($param->isNull() || $param->isEmpty()) {
+ return new AcceptHeader();
+ }
+
+ try {
+ return AcceptHeader::parse($param->getValue());
+ } catch (\net\stubbles\lang\exception\IllegalArgumentException $iae) {
+ return new AcceptHeader();
+ }
+ }
+}
+?>
View
64 src/test/php/net/stubbles/input/filter/AcceptFilterTestCase.php
@@ -0,0 +1,64 @@
+<?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\Param;
+/**
+ * Tests for net\stubbles\input\filter\AcceptFilter.
+ *
+ * @since 2.0.0
+ * @group filter
+ */
+class AcceptFilterTestCase extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * apply filter on given value
+ *
+ * @param string $value
+ * @return net\stubbles\peer\http\AcceptHeader
+ */
+ private function apply($value)
+ {
+ $acceptFilter = new AcceptFilter();
+ return $acceptFilter->apply(new Param('Accept', $value));
+ }
+
+ /**
+ * @test
+ */
+ public function returnsEmptyAcceptHeaderWhenParamValueIsNull()
+ {
+ $this->assertEquals(0, $this->apply(null)->count());
+ }
+
+ /**
+ * @test
+ */
+ public function returnsEmptyAcceptHeaderWhenParamValueIsEmpty()
+ {
+ $this->assertEquals(0, $this->apply('')->count());
+ }
+
+ /**
+ * @test
+ */
+ public function returnsEmptyAcceptHeaderWhenParamValueIsInvalid()
+ {
+ $this->assertEquals(0, $this->apply('text/plain;q=5')->count());
+ }
+
+ /**
+ * @test
+ */
+ public function returnsFilledAcceptHeaderWhenParamValueIsValid()
+ {
+ $this->assertEquals(1, $this->apply('text/plain;q=0.5')->count());
+ }
+}
+?>
Something went wrong with that request. Please try again.