Last active
August 29, 2015 13:57
-
-
Save dadamssg/9807690 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface RequestSpecification | |
{ | |
/** | |
* @param Request $request | |
* @return bool | |
*/ | |
public function isSatisfiedBy(Request $request); | |
} | |
class QueryParamSpecificationDatum | |
{ | |
public $name; | |
public $possibleValues = array(); | |
public $required = true; | |
public function getPossibleValues() | |
{ | |
return $this->possibleValues; | |
} | |
public function getName() | |
{ | |
return $this->name; | |
} | |
public function isRequired() | |
{ | |
return $this->required; | |
} | |
} | |
class QueryParamSpecification implements RequestSpecification | |
{ | |
/** | |
* @var QueryParamSpecificationDatum | |
*/ | |
protected $datum; | |
public function setDatum(QueryParamSpecificationDatum $datum) | |
{ | |
$this->datum = $datum; | |
return $this; | |
} | |
public function onMatchRequestDatum(RequestMatchEvent $event) | |
{ | |
$queryParamDatums = $event->getRequestDatum()->getQueryParamSpecificationDatums(); | |
foreach ($queryParamDatums as $queryParamDatum) { | |
$this->setDatum($queryParamDatum); | |
if (!$this->isSatisfiedBy($event->getRequest())) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public function isSatisfiedBy(Request $request) | |
{ | |
if (!$this->datum) { | |
throw new \Exception('No datum provided!'); | |
} | |
if (!$this->datum->isRequired()) { | |
return true; | |
} | |
$query = $request->query; | |
$datum = $this->datum; | |
foreach ($datum->getPossibleValues() as $value) { | |
if ($query->has($datum->getName())) { | |
if ($value === $query->get($datum->getName())) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
} | |
class RequestDatum | |
{ | |
protected $queryParamSpecificationDatums; | |
public function __construct() | |
{ | |
$this->queryParamSpecificationDatums = new ArrayCollection(); | |
} | |
public function getQueryParamSpecificationDatums() | |
{ | |
return $this->queryParamSpecificationDatums; | |
} | |
} | |
class RequestMatchEvent extends Event | |
{ | |
protected $request; | |
protected $requestDatum; | |
protected $matches = true; | |
public function __construct(Request $request, RequestDatum $requestDatum) | |
{ | |
$this->request = $request; | |
$this->requestDatum = $requestDatum; | |
} | |
public function setMatches($matches) | |
{ | |
return $this->matches = (bool) $matches; | |
} | |
public function matches() | |
{ | |
return $this->matches; | |
} | |
public function getRequest() | |
{ | |
return $this->request; | |
} | |
public function getRequestDatum() | |
{ | |
return $this->requestDatum; | |
} | |
} | |
class RequestMatcher implements RequestSpecification | |
{ | |
const MATCHING_REQUEST = 'matching.request'; | |
/** | |
* @var RequestDatum | |
*/ | |
protected $datum; | |
protected $dispatcher; | |
protected $request; | |
public function __construct(Request $request, EventDispatcherInterface $dispatcher) | |
{ | |
$this->request = $request; | |
$this->dispatcher = $dispatcher; | |
} | |
public function setRequestData(RequestDatum $datum) | |
{ | |
$this->datum = $datum; | |
} | |
/** | |
* @param Request $request | |
* @return bool | |
*/ | |
public function isSatisfiedBy(Request $request) | |
{ | |
$event = new RequestMatchEvent($this->request, $this->datum); | |
$this->dispatcher->dispatch(self::MATCHING_REQUEST, $event); | |
return $event->matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment