Permalink
Browse files

Adding cookie route. Going to open PR for comments.

  • Loading branch information...
Jerry Saravia Jerry Saravia
Jerry Saravia authored and Jerry Saravia committed Oct 16, 2012
1 parent 508ed58 commit 7b0f4131441625b78356023ba67ee0419f37566d
Showing with 249 additions and 0 deletions.
  1. +206 −0 library/Zend/Mvc/Router/Http/Cookie.php
  2. +43 −0 tests/ZendTest/Mvc/Router/Http/CookieTest.php
@@ -0,0 +1,206 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace Zend\Mvc\Router\Http;
+
+use Traversable;
+use Zend\Mvc\Router\Http\RouteInterface,
+ Zend\Mvc\Router\Http\RouteMatch,
+ Zend\Stdlib\ArrayUtils,
+ Zend\Stdlib\RequestInterface as Request,
+ Zend\Mvc\Router\Exception,
+ Zend\Http\Request as HttpRequest;
+
+/**
+ * Cookie route.
+ *
+ * This route will check the request for the
+ * presence of a cookie, it's absence, or a check
+ * it against a regex expression and route the request
+ * accordingly.
+ *
+ *
+ * @package Zend\Mvc\Router\Http
+ * @see http://manuals.rubyonrails.com/read/chapter/65
+ */
+class Cookie
+ implements RouteInterface
+{
+
+ /**
+ * The names of the cookies to look for
+ *
+ * @var array
+ */
+ private $cookies;
+
+ /**
+ * Constraints for parameters.
+ *
+ * @var array
+ */
+ private $constraints;
+
+ /**
+ * Default values.
+ *
+ * @var array
+ */
+ private $defaults;
+
+ /**
+ *
+ * @param string|array $cookies
+ * @param array $constraints
+ * @param array $defaults
+ */
+ public function __construct( $cookies,
+ array $constraints = array( ),
+ array $defaults = array( ) )
+ {
+ $this->cookies = ( array ) $cookies;
+ $this->defaults = $defaults;
+ $this->constraints = $constraints;
+ }
+
+ /**
+ * factory(): defined by RouteInterface interface.
+ *
+ * @see Route::factory()
+ * @param array|\Traversable $options
+ * @throws \Zend\Mvc\Router\Exception\InvalidArgumentException
+ * @return Cookie
+ */
+ public static function factory( $options = array( ) )
+ {
+
+ if( $options instanceof Traversable )
+ {
+ $options = ArrayUtils::iteratorToArray( $options );
+ }
+ elseif( !is_array( $options ) )
+ {
+ throw new Exception\InvalidArgumentException( __METHOD__ . ' expects an array or Traversable set of options' );
+ }
+
+ if( !isset( $options[ 'cookies' ] ) )
+ {
+ throw new Exception\InvalidArgumentException( 'Missing "cookies" in options array' );
+ }
+
+ if( !isset( $options[ 'constraints' ] ) )
+ {
+ $options[ 'constraints' ] = array( );
+ }
+
+ if( !isset( $options[ 'defaults' ] ) )
+ {
+ $options[ 'defaults' ] = array( );
+ }
+
+ return new static( $options[ 'cookies' ], $options[ 'constraints' ], $options[ 'defaults' ] );
+ }
+
+ /**
+ * assemble(): Defined by RouteInterface interface.
+ *
+ * @see Route::assemble()
+ * @param array $params
+ * @param array $options
+ * @return mixed
+ * @throws Exception\InvalidArgumentException
+ */
+ public function assemble( array $params = array( ),
+ array $options = array( ) )
+ {
+ //cookie's don't contribute to the path
+ return '';
+ }
+
+ /**
+ * getAssembledParams(): defined by RouteInterface interface.
+ *
+ * @see Route::getAssembledParams
+ * @return array
+ */
+ public function getAssembledParams()
+ {
+ return array( );
+ }
+
+ /**
+ * match(): defined by RouteInterface interface.
+ *
+ * @see Route::match()
+ * @param Request $request
+ * @return RouteMatch
+ */
+ public function match( Request $request )
+ {
+ /* @var $request HttpRequest */
+ if( !method_exists( $request, 'getCookie' ) )
+ {
+ return null;
+ }
+
+ $results = array( );
+
+ /* @var $cookie \Zend\Http\Header\Cookie */
+ $incomingCookies = $request->getCookie()->getArrayCopy();
+
+ foreach( $this->cookies as $cookie )
+ {
+ if( !array_key_exists( $cookie, $this->constraints ) )
+ {
+ $this->constraints[ $cookie ] = true;
+ }
+
+ if( is_bool( $this->constraints[ $cookie ] ) )
+ {
+ if( $this->constraints[ $cookie ] === true && !array_key_exists( $cookie,
+ $incomingCookies ) )
+ {
+ return null;
+ }
+ elseif( $this->constraints[ $cookie ] === false && array_key_exists( $cookie,
+ $incomingCookies ) )
+ {
+ return null;
+ }
+
+ $results[ $cookie ] = $this->constraints[ $cookie ];
+ continue;
+ }
+ elseif( is_string( $this->constraints[ $cookie ] ) )
+ {
+ if( !array_key_exists( $cookie, $incomingCookies ) )
+ {
+ return null;
+ }
+
+
+ $result = preg_match( $this->constraints[ $cookie ],
+ $incomingCookies[ $cookie ], $matches );
+
+ if( !$result )
+ {
+ return null;
+ }
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ return new RouteMatch( array_merge( $this->defaults, $results ) );
+ }
+
+}
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Mvc
+ */
+
+namespace ZendTest\Mvc\Router\Http;
+
+use PHPUnit_Framework_TestCase as TestCase;
+use Zend\Http\Request as Request;
+use Zend\Stdlib\Request as BaseRequest;
+use Zend\Uri\Http as HttpUri;
+use Zend\Mvc\Router\Http\Cookie;
+use ZendTest\Mvc\Router\FactoryTester;
+
+class CookieTest
+ extends TestCase
+{
+
+ public function testFactory()
+ {
+ $tester = new FactoryTester( $this );
+ $tester->testFactory(
+ 'Zend\Mvc\Router\Http\Cookie',
+ array(
+ 'cookies' => 'Missing "cookies" in options array'
+ ), array(
+ 'cookies' => 'thing'
+ )
+ );
+ }
+
+ public static function routeProvider()
+ {
+ return array(
+ );
+ }
+
+}

0 comments on commit 7b0f413

Please sign in to comment.