17 private $eventManager =
null;
18 private $persistenceFacade =
null;
24 $this->eventManager = $eventManager;
26 $this->persistenceFacade = $persistenceFacade;
43 if ($request !=
null) {
50 if ($request !=
null && $response !=
null) {
58 if ($request->hasHeader(
'Range')) {
59 if (preg_match(
'/^items=([\-]?[0-9]+)-([\-]?[0-9]+)$/', $request->getHeader(
'Range'), $matches)) {
60 $offset = intval($matches[1]);
61 $limit = intval($matches[2])-$offset+1;
62 $request->setValue(
'offset', $offset);
63 $request->setValue(
'limit', $limit);
68 if ($request->hasHeader(
'Position')) {
69 $position = $request->getHeader(
'Position');
70 if ($position ==
'last') {
71 $referenceOid =
'ORDER_BOTTOM';
74 list($ignore, $orderReferenceIdStr) = preg_split(
'/ /', $position);
75 if ($request->hasValue(
'relation') && $request->hasValue(
'sourceOid')) {
78 $relatedType = $this->
getRelatedType($sourceOid, $request->getValue(
'relation'));
79 $referenceOid =
new ObjectId($relatedType, $orderReferenceIdStr);
83 $referenceOid =
new ObjectId($request->getValue(
'className'), $orderReferenceIdStr);
86 $request->setValue(
'referenceOid', $referenceOid);
90 foreach ($request->getValues() as $key => $value) {
92 if (preg_match(
'/^sort\(([^\)]+)\)$|sortBy=([.]+)$/', $key, $matches)) {
93 $sortDefs = preg_split(
'/,/', $matches[1]);
95 $sortDef = $sortDefs[0];
96 $sortFieldName = substr($sortDef, 1);
97 $sortDirection = preg_match(
'/^-/', $sortDef) ?
'desc' :
'asc';
98 $request->setValue(
'sortFieldName', $sortFieldName);
99 $request->setValue(
'sortDirection', $sortDirection);
102 if (preg_match(
'/^limit\(([^\)]+)\)$/', $key, $matches)) {
103 $rangeDefs = preg_split(
'/,/', $matches[1]);
104 $limit = intval($rangeDefs[0]);
105 $offset =
sizeof($rangeDefs) > 1 ? intval($rangeDefs[1]) : 0;
106 $request->setValue(
'offset', $offset);
107 $request->setValue(
'limit', $limit);
114 if ($response->hasValue(
'list')) {
115 $objects = $response->getValue(
'list');
116 $size =
sizeof($objects);
119 $offset = $request->getValue(
'offset');
120 $limit = $size == 0 ? $offset : $offset+$size-1;
121 $total = $response->getValue(
'totalCount');
122 $response->setHeader(
'Content-Range',
'items '.$offset.
'-'.$limit.
'/'.$total);
134 $sourceMapper = $this->persistenceFacade->getMapper($sourceOid->
getType());
135 $relation = $sourceMapper->getRelation($role);
136 return $relation->getOtherType();
getResponse()
Get the response.
const AFTER_EXECUTE_CONTROLLER
A AFTER_EXECUTE_CONTROLLER event occurs after the current controller is executed.
EventManager is responsible for dispatching events to registered listeners.
transformRequest($request)
getStage()
Get the stage at which the event occurred.
getRequest()
Get the request.
const BEFORE_ROUTE_ACTION
A BEFORE_ROUTE_ACTION event occurs before the request is mapped to an action key.
__construct(EventManager $eventManager, PersistenceFacade $persistenceFacade)
Constructor.
ObjectId is the unique identifier of an object.
listen(ApplicationEvent $event)
Listen to ApplicationEvent.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
PersistenceFacade defines the interface for PersistenceFacade implementations.
transformResponse($request, $response)
getType()
Get the type (including namespace)
ApplicationEvent instances are fired at different stages of the program flow.
addListener($eventName, $callback)
Register a listener for a given event.
getRelatedType(ObjectId $sourceOid, $role)
Get the type that is used in the given role related to the given source object.
DefaultRequestListener normalizes various search, sort and paging parameters into those understood by...