PageRenderTime 4067ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/OData Producer for PHP/library/ODataProducer/UriProcessor/ResourcePathProcessor/ResourcePathProcessor.php

#
PHP | 162 lines | 108 code | 8 blank | 46 comment | 24 complexity | f4d8c7685c519fd539ab598d38937103 MD5 | raw file
  1. <?php
  2. /**
  3. * Processor to process the resource path part of the request uri.
  4. *
  5. * PHP version 5.3
  6. *
  7. * @category ODataProducer
  8. * @package ODataProducer_UriProcessor_ResourcePathProcessor
  9. * @author Anu T Chandy <odataphpproducer_alias@microsoft.com>
  10. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  11. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  12. * @version SVN: 1.0
  13. * @link http://odataphpproducer.codeplex.com
  14. *
  15. */
  16. namespace ODataProducer\UriProcessor\ResourcePathProcessor;
  17. use ODataProducer\UriProcessor\ResourcePathProcessor\SegmentParser\SegmentParser;
  18. use ODataProducer\UriProcessor\ResourcePathProcessor\SegmentParser\RequestTargetKind;
  19. use ODataProducer\UriProcessor\RequestDescription;
  20. use ODataProducer\UriProcessor\RequestCountOption;
  21. use ODataProducer\DataService;
  22. use ODataProducer\Common\Url;
  23. use ODataProducer\Common\ODataConstants;
  24. use ODataProducer\Common\Messages;
  25. use ODataProducer\Common\ODataException;
  26. /**
  27. * Processor for resource path part of request uri.
  28. *
  29. * @category ODataProducer
  30. * @package ODataProducer_UriProcessor_ResourcePathProcessor
  31. * @author Anu T Chandy <odataphpproducer_alias@microsoft.com>
  32. * @copyright 2011 Microsoft Corp. (http://www.microsoft.com)
  33. * @license New BSD license, (http://www.opensource.org/licenses/bsd-license.php)
  34. * @version Release: 1.0
  35. * @link http://odataphpproducer.codeplex.com
  36. */
  37. class ResourcePathProcessor
  38. {
  39. /**
  40. * Process the given request Uri and creates an instance of
  41. * RequestDescription from the processed uri.
  42. *
  43. * @param Url &$absoluteRequestUri The absolute request uri.
  44. * @param DataService &$dataService Reference to the data service
  45. * instance.
  46. *
  47. * @return RequestDescription
  48. *
  49. * @throws ODataException If any exception occurs while processing the segments
  50. * or incase of any version incompatibility.
  51. */
  52. public static function process(Url &$absoluteRequestUri,
  53. DataService &$dataService
  54. ) {
  55. $absoluteRequestUri = $dataService->getHost()->getAbsoluteRequestUri();
  56. $absoluteServiceUri = $dataService->getHost()->getAbsoluteServiceUri();
  57. $requestUriSegments = array_slice(
  58. $absoluteRequestUri->getSegments(),
  59. $absoluteServiceUri->getSegmentCount()
  60. );
  61. $segmentDescriptors = null;
  62. try {
  63. $segmentDescriptors = SegmentParser::parseRequestUriSegements(
  64. $requestUriSegments,
  65. $dataService->getMetadataQueryProviderWrapper(),
  66. true
  67. );
  68. } catch (ODataException $odataException) {
  69. throw $odataException;
  70. }
  71. $requestDescription = new RequestDescription(
  72. $segmentDescriptors,
  73. $absoluteRequestUri
  74. );
  75. $requestTargetKind = $requestDescription->getTargetKind();
  76. if ($requestTargetKind != RequestTargetKind::METADATA
  77. && $requestTargetKind != RequestTargetKind::BATCH
  78. && $requestTargetKind != RequestTargetKind::SERVICE_DIRECTORY
  79. ) {
  80. if ($requestTargetKind != RequestTargetKind::PRIMITIVE_VALUE
  81. && $requestTargetKind != RequestTargetKind::MEDIA_RESOURCE
  82. ) {
  83. $requestDescription->setContainerName($requestDescription->getIdentifier());
  84. } else {
  85. // http://odata/NW.svc/Orders/$count
  86. // http://odata/NW.svc/Orders(123)/Customer/CustomerID/$value
  87. // http://odata/NW.svc/Employees(1)/$value
  88. // http://odata/NW.svc/Employees(1)/ThumbNail_48X48/$value
  89. $requestDescription->setContainerName(
  90. $segmentDescriptors[count($segmentDescriptors) - 2]->getIdentifier()
  91. );
  92. }
  93. if ($requestDescription->getIdentifier() === ODataConstants::URI_COUNT_SEGMENT
  94. ) {
  95. if (!$dataService->getServiceConfiguration()->getAcceptCountRequests()
  96. ) {
  97. ODataException::createBadRequestError(
  98. Messages::dataServiceConfigurationCountNotAccepted()
  99. );
  100. }
  101. $requestDescription->setRequestCountOption(
  102. RequestCountOption::VALUE_ONLY
  103. );
  104. // use of $count requires request DataServiceVersion
  105. // and MaxDataServiceVersion
  106. // greater than or equal to 2.0
  107. $requestDescription->raiseResponseVersion(
  108. 2,
  109. 0,
  110. $dataService
  111. );
  112. $requestDescription->raiseMinimumVersionRequirement(
  113. 2,
  114. 0,
  115. $dataService
  116. );
  117. } else if ($requestDescription->isNamedStream()) {
  118. $requestDescription->raiseMinimumVersionRequirement(
  119. 3,
  120. 0,
  121. $dataService
  122. );
  123. } else if ($requestDescription->getTargetKind() == RequestTargetKind::RESOURCE
  124. ) {
  125. if (!$requestDescription->isLinkUri()) {
  126. $resourceSetWrapper = $requestDescription
  127. ->getTargetResourceSetWrapper();
  128. //assert($resourceSetWrapper != null)
  129. $hasNamedStream = $resourceSetWrapper->hasNamedStreams(
  130. $dataService->getMetadataQueryProviderWrapper()
  131. );
  132. $hasBagProperty = $resourceSetWrapper->hasBagProperty(
  133. $dataService->getMetadataQueryProviderWrapper()
  134. );
  135. if ($hasNamedStream || $hasBagProperty) {
  136. $requestDescription->raiseResponseVersion(
  137. 3,
  138. 0,
  139. $dataService
  140. );
  141. }
  142. }
  143. } else if ($requestDescription->getTargetKind() == RequestTargetKind::BAG
  144. ) {
  145. $requestDescription->raiseResponseVersion(
  146. 3,
  147. 0,
  148. $dataService
  149. );
  150. }
  151. }
  152. return $requestDescription;
  153. }
  154. }