@@ -44,6 +44,9 @@ discard block |
||
| 44 | 44 | protected $baseUri; |
| 45 | 45 | |
| 46 | 46 | |
| 47 | + /** |
|
| 48 | + * @param string $absoluteServiceUri |
|
| 49 | + */ |
|
| 47 | 50 | public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri) |
| 48 | 51 | { |
| 49 | 52 | if(strlen($absoluteServiceUri) == 0) |
@@ -150,6 +153,9 @@ discard block |
||
| 150 | 153 | |
| 151 | 154 | |
| 152 | 155 | |
| 156 | + /** |
|
| 157 | + * @param string $fragment |
|
| 158 | + */ |
|
| 153 | 159 | protected function writeTopLevelMeta($fragment) |
| 154 | 160 | { |
| 155 | 161 | if($this->metadataLevel == JsonLightMetadataLevel::NONE()) |
@@ -362,7 +368,7 @@ discard block |
||
| 362 | 368 | |
| 363 | 369 | /** |
| 364 | 370 | * @param ProvidersWrapper $providers |
| 365 | - * @return IODataWriter |
|
| 371 | + * @return JsonLightODataWriter |
|
| 366 | 372 | */ |
| 367 | 373 | public function writeServiceDocument(ProvidersWrapper $providers){ |
| 368 | 374 | $writer = $this->_writer; |
@@ -10,16 +10,11 @@ |
||
| 10 | 10 | use POData\ObjectModel\ODataPropertyContent; |
| 11 | 11 | use POData\ObjectModel\ODataBagContent; |
| 12 | 12 | use POData\ObjectModel\ODataProperty; |
| 13 | -use POData\ObjectModel\ODataMediaLink; |
|
| 14 | 13 | use POData\Writers\Json\JsonWriter; |
| 15 | 14 | use POData\Common\Version; |
| 16 | 15 | use POData\Common\ODataConstants; |
| 17 | 16 | use POData\Common\MimeTypes; |
| 18 | -use POData\Common\Messages; |
|
| 19 | -use POData\Common\ODataException; |
|
| 20 | -use POData\Common\InvalidOperationException; |
|
| 21 | 17 | use POData\Providers\ProvidersWrapper; |
| 22 | - |
|
| 23 | 18 | use POData\Writers\Json\JsonLightMetadataLevel; |
| 24 | 19 | |
| 25 | 20 | |
@@ -30,256 +30,256 @@ discard block |
||
| 30 | 30 | class JsonLightODataWriter extends JsonODataV2Writer |
| 31 | 31 | { |
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @var JsonLightMetadataLevel |
|
| 35 | - */ |
|
| 36 | - protected $metadataLevel; |
|
| 37 | - |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * |
|
| 41 | - * The service base uri |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - protected $baseUri; |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri) |
|
| 48 | - { |
|
| 49 | - if(strlen($absoluteServiceUri) == 0) |
|
| 50 | - { |
|
| 51 | - throw new \Exception("absoluteServiceUri must not be empty or null"); |
|
| 52 | - } |
|
| 53 | - $this->baseUri = $absoluteServiceUri; |
|
| 54 | - |
|
| 55 | - $this->_writer = new JsonWriter(''); |
|
| 56 | - $this->urlKey = ODataConstants::JSON_URL_STRING; |
|
| 57 | - $this->dataArrayName = ODataConstants::JSON_LIGHT_VALUE_NAME; |
|
| 58 | - $this->rowCountName = ODataConstants::JSON_LIGHT_ROWCOUNT_STRING; |
|
| 59 | - $this->metadataLevel = $metadataLevel; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Determines if the given writer is capable of writing the response or not |
|
| 65 | - * @param Version $responseVersion the OData version of the response |
|
| 66 | - * @param string $contentType the Content Type of the response |
|
| 67 | - * @return boolean true if the writer can handle the response, false otherwise |
|
| 68 | - */ |
|
| 69 | - public function canHandle(Version $responseVersion, $contentType) |
|
| 70 | - { |
|
| 71 | - if($responseVersion != Version::v3()){ |
|
| 72 | - return false; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $parts = explode(";", $contentType); |
|
| 76 | - |
|
| 77 | - //It must be app/json and have the right odata= piece |
|
| 78 | - $metadata = array_filter($parts, function ($item) { return strpos($item, 'odata') !== false; }); |
|
| 79 | - return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && (empty($metadata) || in_array($this->metadataLevel->getValue(), $metadata)); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Write the given OData model in a specific response format |
|
| 86 | - * |
|
| 87 | - * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content. |
|
| 88 | - * |
|
| 89 | - * @return JsonLightODataWriter |
|
| 90 | - */ |
|
| 91 | - public function write($model){ |
|
| 92 | - $this->_writer->startObjectScope(); |
|
| 93 | - |
|
| 94 | - if ($model instanceof ODataURL) { |
|
| 95 | - $this->writeTopLevelMeta("url"); |
|
| 96 | - $this->writeURL($model); |
|
| 97 | - } elseif ($model instanceof ODataURLCollection) { |
|
| 98 | - $this->writeTopLevelMeta("urlCollection"); |
|
| 99 | - $this->writeURLCollection($model); |
|
| 100 | - } elseif ($model instanceof ODataPropertyContent) { |
|
| 101 | - $this->writeTopLevelMeta( $model->properties[0]->typeName ); |
|
| 102 | - $this->writeTopLevelProperty($model->properties[0]); |
|
| 103 | - } elseif ($model instanceof ODataFeed) { |
|
| 104 | - $this->writeTopLevelMeta($model->title); |
|
| 105 | - $this->writeRowCount($model->rowCount); |
|
| 106 | - $this->writeNextPageLink($model->nextPageLink); |
|
| 107 | - $this->_writer |
|
| 108 | - ->writeName($this->dataArrayName) |
|
| 109 | - ->startArrayScope(); |
|
| 110 | - $this->writeFeed($model); |
|
| 111 | - $this->_writer->endScope(); |
|
| 112 | - }elseif ($model instanceof ODataEntry) { |
|
| 113 | - $this->writeTopLevelMeta($model->resourceSetName . "/@Element"); |
|
| 114 | - $this->writeEntry($model); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->_writer->endScope(); |
|
| 118 | - |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * |
|
| 125 | - * @param ODataProperty $property |
|
| 126 | - * |
|
| 127 | - * @return JsonLightODataWriter |
|
| 128 | - */ |
|
| 129 | - protected function writeTopLevelProperty(ODataProperty $property) |
|
| 130 | - { |
|
| 131 | - $this->writePropertyMeta($property); |
|
| 132 | - if ($property->value == null) { |
|
| 133 | - $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 134 | - $this->_writer->writeValue("null"); |
|
| 135 | - } elseif ($property->value instanceof ODataPropertyContent) { |
|
| 136 | - //In the case of complex properties at the top level we don't write the name of the property, |
|
| 137 | - //just the sub properties. |
|
| 138 | - $this->writeComplexPropertyMeta($property) |
|
| 139 | - ->writeProperties($property->value); |
|
| 140 | - } elseif ($property->value instanceof ODataBagContent) { |
|
| 141 | - $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 142 | - $this->writeBagContent($property->value); |
|
| 143 | - } else { |
|
| 144 | - $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 145 | - $this->_writer->writeValue($property->value, $property->typeName); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return $this; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - |
|
| 153 | - protected function writeTopLevelMeta($fragment) |
|
| 154 | - { |
|
| 155 | - if($this->metadataLevel == JsonLightMetadataLevel::NONE()) |
|
| 156 | - { |
|
| 157 | - return; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - $this->_writer |
|
| 161 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_STRING) |
|
| 162 | - ->writeValue($this->baseUri . '/' . ODataConstants::URI_METADATA_SEGMENT . '#' . $fragment); |
|
| 163 | - |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - |
|
| 167 | - protected function writePropertyMeta(ODataProperty $property) |
|
| 168 | - { |
|
| 169 | - if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 170 | - { |
|
| 171 | - //Only full meta level outputs this info |
|
| 172 | - return $this; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if(is_null($property->value)) |
|
| 176 | - { |
|
| 177 | - //it appears full metadata doesn't output types for nulls... |
|
| 178 | - return $this; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - |
|
| 182 | - switch($property->typeName) |
|
| 183 | - { |
|
| 184 | - //The type metadata is only included on certain types of properties |
|
| 185 | - //Note this also excludes Complex types |
|
| 186 | - |
|
| 187 | - case "Edm.Decimal": |
|
| 188 | - case "Edm.DateTime": |
|
| 189 | - $this->_writer |
|
| 190 | - ->writeName($property->name . ODataConstants::JSON_LIGHT_METADATA_PROPERTY_TYPE_SUFFIX_STRING) |
|
| 191 | - ->writeValue($property->typeName); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - |
|
| 195 | - return $this; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - /** |
|
| 199 | - * |
|
| 200 | - * @param ODataEntry $entry Entry to write metadata for. |
|
| 201 | - * |
|
| 202 | - * @return JsonLightODataWriter |
|
| 203 | - */ |
|
| 204 | - protected function writeEntryMetadata(ODataEntry $entry){ |
|
| 205 | - |
|
| 206 | - if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 207 | - { |
|
| 208 | - //Only full meta level outputs this info |
|
| 209 | - return $this; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - $this->_writer |
|
| 213 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING) |
|
| 214 | - ->writeValue($entry->type) |
|
| 215 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_ID_STRING) |
|
| 216 | - ->writeValue($entry->id) |
|
| 217 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_ETAG_STRING) |
|
| 218 | - ->writeValue($entry->eTag) |
|
| 219 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_EDIT_LINK_STRING) |
|
| 220 | - ->writeValue($entry->editLink) |
|
| 221 | - ; |
|
| 222 | - |
|
| 223 | - return $this; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param ODataLink $link Link to write. |
|
| 229 | - * |
|
| 230 | - * @return JsonLightODataWriter |
|
| 231 | - */ |
|
| 232 | - protected function writeLink(ODataLink $link){ |
|
| 233 | - |
|
| 234 | - if($this->metadataLevel == JsonLightMetadataLevel::FULL()) |
|
| 235 | - { |
|
| 236 | - //Interestingly the fullmetadata outputs this metadata..even if the thing is expanded |
|
| 237 | - $this->_writer |
|
| 238 | - ->writeName($link->title . ODataConstants::JSON_LIGHT_METADATA_LINK_NAVIGATION_SUFFIX_STRING) |
|
| 239 | - ->writeValue($link->url);; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - |
|
| 243 | - if($link->isExpanded) |
|
| 244 | - { |
|
| 245 | - $this->_writer->writeName($link->title); |
|
| 246 | - |
|
| 247 | - if(is_null($link->expandedResult)) { |
|
| 248 | - $this->_writer->writeValue("null"); |
|
| 249 | - } else { |
|
| 250 | - $this->writeExpandedLink($link); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $this; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - protected function writeExpandedLink(ODataLink $link) |
|
| 258 | - { |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - if ($link->isCollection) { |
|
| 262 | - $this->_writer->startArrayScope(); |
|
| 263 | - $this->writeFeed($link->expandedResult); |
|
| 264 | - } else { |
|
| 265 | - $this->_writer->startObjectScope(); |
|
| 266 | - $this->writeEntry($link->expandedResult); |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - |
|
| 270 | - $this->_writer->endScope(); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - /** |
|
| 274 | - * Writes the next page link. |
|
| 275 | - * |
|
| 276 | - * @param ODataLink $nextPageLinkUri Uri for next page link. |
|
| 277 | - * |
|
| 278 | - * @return JsonLightODataWriter |
|
| 279 | - */ |
|
| 280 | - protected function writeNextPageLink(ODataLink $nextPageLinkUri = null) |
|
| 281 | - { |
|
| 282 | - /* |
|
| 33 | + /** |
|
| 34 | + * @var JsonLightMetadataLevel |
|
| 35 | + */ |
|
| 36 | + protected $metadataLevel; |
|
| 37 | + |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * |
|
| 41 | + * The service base uri |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + protected $baseUri; |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri) |
|
| 48 | + { |
|
| 49 | + if(strlen($absoluteServiceUri) == 0) |
|
| 50 | + { |
|
| 51 | + throw new \Exception("absoluteServiceUri must not be empty or null"); |
|
| 52 | + } |
|
| 53 | + $this->baseUri = $absoluteServiceUri; |
|
| 54 | + |
|
| 55 | + $this->_writer = new JsonWriter(''); |
|
| 56 | + $this->urlKey = ODataConstants::JSON_URL_STRING; |
|
| 57 | + $this->dataArrayName = ODataConstants::JSON_LIGHT_VALUE_NAME; |
|
| 58 | + $this->rowCountName = ODataConstants::JSON_LIGHT_ROWCOUNT_STRING; |
|
| 59 | + $this->metadataLevel = $metadataLevel; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Determines if the given writer is capable of writing the response or not |
|
| 65 | + * @param Version $responseVersion the OData version of the response |
|
| 66 | + * @param string $contentType the Content Type of the response |
|
| 67 | + * @return boolean true if the writer can handle the response, false otherwise |
|
| 68 | + */ |
|
| 69 | + public function canHandle(Version $responseVersion, $contentType) |
|
| 70 | + { |
|
| 71 | + if($responseVersion != Version::v3()){ |
|
| 72 | + return false; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $parts = explode(";", $contentType); |
|
| 76 | + |
|
| 77 | + //It must be app/json and have the right odata= piece |
|
| 78 | + $metadata = array_filter($parts, function ($item) { return strpos($item, 'odata') !== false; }); |
|
| 79 | + return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && (empty($metadata) || in_array($this->metadataLevel->getValue(), $metadata)); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Write the given OData model in a specific response format |
|
| 86 | + * |
|
| 87 | + * @param ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content. |
|
| 88 | + * |
|
| 89 | + * @return JsonLightODataWriter |
|
| 90 | + */ |
|
| 91 | + public function write($model){ |
|
| 92 | + $this->_writer->startObjectScope(); |
|
| 93 | + |
|
| 94 | + if ($model instanceof ODataURL) { |
|
| 95 | + $this->writeTopLevelMeta("url"); |
|
| 96 | + $this->writeURL($model); |
|
| 97 | + } elseif ($model instanceof ODataURLCollection) { |
|
| 98 | + $this->writeTopLevelMeta("urlCollection"); |
|
| 99 | + $this->writeURLCollection($model); |
|
| 100 | + } elseif ($model instanceof ODataPropertyContent) { |
|
| 101 | + $this->writeTopLevelMeta( $model->properties[0]->typeName ); |
|
| 102 | + $this->writeTopLevelProperty($model->properties[0]); |
|
| 103 | + } elseif ($model instanceof ODataFeed) { |
|
| 104 | + $this->writeTopLevelMeta($model->title); |
|
| 105 | + $this->writeRowCount($model->rowCount); |
|
| 106 | + $this->writeNextPageLink($model->nextPageLink); |
|
| 107 | + $this->_writer |
|
| 108 | + ->writeName($this->dataArrayName) |
|
| 109 | + ->startArrayScope(); |
|
| 110 | + $this->writeFeed($model); |
|
| 111 | + $this->_writer->endScope(); |
|
| 112 | + }elseif ($model instanceof ODataEntry) { |
|
| 113 | + $this->writeTopLevelMeta($model->resourceSetName . "/@Element"); |
|
| 114 | + $this->writeEntry($model); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->_writer->endScope(); |
|
| 118 | + |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * |
|
| 125 | + * @param ODataProperty $property |
|
| 126 | + * |
|
| 127 | + * @return JsonLightODataWriter |
|
| 128 | + */ |
|
| 129 | + protected function writeTopLevelProperty(ODataProperty $property) |
|
| 130 | + { |
|
| 131 | + $this->writePropertyMeta($property); |
|
| 132 | + if ($property->value == null) { |
|
| 133 | + $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 134 | + $this->_writer->writeValue("null"); |
|
| 135 | + } elseif ($property->value instanceof ODataPropertyContent) { |
|
| 136 | + //In the case of complex properties at the top level we don't write the name of the property, |
|
| 137 | + //just the sub properties. |
|
| 138 | + $this->writeComplexPropertyMeta($property) |
|
| 139 | + ->writeProperties($property->value); |
|
| 140 | + } elseif ($property->value instanceof ODataBagContent) { |
|
| 141 | + $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 142 | + $this->writeBagContent($property->value); |
|
| 143 | + } else { |
|
| 144 | + $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME); |
|
| 145 | + $this->_writer->writeValue($property->value, $property->typeName); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return $this; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + |
|
| 153 | + protected function writeTopLevelMeta($fragment) |
|
| 154 | + { |
|
| 155 | + if($this->metadataLevel == JsonLightMetadataLevel::NONE()) |
|
| 156 | + { |
|
| 157 | + return; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + $this->_writer |
|
| 161 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_STRING) |
|
| 162 | + ->writeValue($this->baseUri . '/' . ODataConstants::URI_METADATA_SEGMENT . '#' . $fragment); |
|
| 163 | + |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + |
|
| 167 | + protected function writePropertyMeta(ODataProperty $property) |
|
| 168 | + { |
|
| 169 | + if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 170 | + { |
|
| 171 | + //Only full meta level outputs this info |
|
| 172 | + return $this; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if(is_null($property->value)) |
|
| 176 | + { |
|
| 177 | + //it appears full metadata doesn't output types for nulls... |
|
| 178 | + return $this; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + |
|
| 182 | + switch($property->typeName) |
|
| 183 | + { |
|
| 184 | + //The type metadata is only included on certain types of properties |
|
| 185 | + //Note this also excludes Complex types |
|
| 186 | + |
|
| 187 | + case "Edm.Decimal": |
|
| 188 | + case "Edm.DateTime": |
|
| 189 | + $this->_writer |
|
| 190 | + ->writeName($property->name . ODataConstants::JSON_LIGHT_METADATA_PROPERTY_TYPE_SUFFIX_STRING) |
|
| 191 | + ->writeValue($property->typeName); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + |
|
| 195 | + return $this; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + /** |
|
| 199 | + * |
|
| 200 | + * @param ODataEntry $entry Entry to write metadata for. |
|
| 201 | + * |
|
| 202 | + * @return JsonLightODataWriter |
|
| 203 | + */ |
|
| 204 | + protected function writeEntryMetadata(ODataEntry $entry){ |
|
| 205 | + |
|
| 206 | + if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 207 | + { |
|
| 208 | + //Only full meta level outputs this info |
|
| 209 | + return $this; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + $this->_writer |
|
| 213 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING) |
|
| 214 | + ->writeValue($entry->type) |
|
| 215 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_ID_STRING) |
|
| 216 | + ->writeValue($entry->id) |
|
| 217 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_ETAG_STRING) |
|
| 218 | + ->writeValue($entry->eTag) |
|
| 219 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_EDIT_LINK_STRING) |
|
| 220 | + ->writeValue($entry->editLink) |
|
| 221 | + ; |
|
| 222 | + |
|
| 223 | + return $this; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param ODataLink $link Link to write. |
|
| 229 | + * |
|
| 230 | + * @return JsonLightODataWriter |
|
| 231 | + */ |
|
| 232 | + protected function writeLink(ODataLink $link){ |
|
| 233 | + |
|
| 234 | + if($this->metadataLevel == JsonLightMetadataLevel::FULL()) |
|
| 235 | + { |
|
| 236 | + //Interestingly the fullmetadata outputs this metadata..even if the thing is expanded |
|
| 237 | + $this->_writer |
|
| 238 | + ->writeName($link->title . ODataConstants::JSON_LIGHT_METADATA_LINK_NAVIGATION_SUFFIX_STRING) |
|
| 239 | + ->writeValue($link->url);; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + |
|
| 243 | + if($link->isExpanded) |
|
| 244 | + { |
|
| 245 | + $this->_writer->writeName($link->title); |
|
| 246 | + |
|
| 247 | + if(is_null($link->expandedResult)) { |
|
| 248 | + $this->_writer->writeValue("null"); |
|
| 249 | + } else { |
|
| 250 | + $this->writeExpandedLink($link); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $this; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + protected function writeExpandedLink(ODataLink $link) |
|
| 258 | + { |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + if ($link->isCollection) { |
|
| 262 | + $this->_writer->startArrayScope(); |
|
| 263 | + $this->writeFeed($link->expandedResult); |
|
| 264 | + } else { |
|
| 265 | + $this->_writer->startObjectScope(); |
|
| 266 | + $this->writeEntry($link->expandedResult); |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + |
|
| 270 | + $this->_writer->endScope(); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + /** |
|
| 274 | + * Writes the next page link. |
|
| 275 | + * |
|
| 276 | + * @param ODataLink $nextPageLinkUri Uri for next page link. |
|
| 277 | + * |
|
| 278 | + * @return JsonLightODataWriter |
|
| 279 | + */ |
|
| 280 | + protected function writeNextPageLink(ODataLink $nextPageLinkUri = null) |
|
| 281 | + { |
|
| 282 | + /* |
|
| 283 | 283 | // "__next" : uri |
| 284 | 284 | if ($nextPageLinkUri != null) { |
| 285 | 285 | $this->_writer |
@@ -289,48 +289,48 @@ discard block |
||
| 289 | 289 | |
| 290 | 290 | return $this; |
| 291 | 291 | */ |
| 292 | - } |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | 294 | |
| 295 | - /** |
|
| 296 | - * Begin write complex property. |
|
| 297 | - * |
|
| 298 | - * @param ODataProperty $property property to write. |
|
| 299 | - * |
|
| 300 | - * @return JsonLightODataWriter |
|
| 301 | - */ |
|
| 302 | - protected function writeComplexProperty(ODataProperty $property) |
|
| 303 | - { |
|
| 295 | + /** |
|
| 296 | + * Begin write complex property. |
|
| 297 | + * |
|
| 298 | + * @param ODataProperty $property property to write. |
|
| 299 | + * |
|
| 300 | + * @return JsonLightODataWriter |
|
| 301 | + */ |
|
| 302 | + protected function writeComplexProperty(ODataProperty $property) |
|
| 303 | + { |
|
| 304 | 304 | |
| 305 | - // { |
|
| 306 | - $this->_writer->startObjectScope(); |
|
| 305 | + // { |
|
| 306 | + $this->_writer->startObjectScope(); |
|
| 307 | 307 | |
| 308 | - $this->writeComplexPropertyMeta($property) |
|
| 309 | - ->writeProperties($property->value); |
|
| 308 | + $this->writeComplexPropertyMeta($property) |
|
| 309 | + ->writeProperties($property->value); |
|
| 310 | 310 | |
| 311 | - $this->_writer->endScope(); |
|
| 311 | + $this->_writer->endScope(); |
|
| 312 | 312 | |
| 313 | - return $this; |
|
| 314 | - } |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - protected function writeComplexPropertyMeta(ODataProperty $property) |
|
| 317 | - { |
|
| 318 | - if($this->metadataLevel == JsonLightMetadataLevel::FULL()){ |
|
| 319 | - $this->_writer |
|
| 320 | - ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING) |
|
| 321 | - ->writeValue($property->typeName); |
|
| 322 | - } |
|
| 316 | + protected function writeComplexPropertyMeta(ODataProperty $property) |
|
| 317 | + { |
|
| 318 | + if($this->metadataLevel == JsonLightMetadataLevel::FULL()){ |
|
| 319 | + $this->_writer |
|
| 320 | + ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING) |
|
| 321 | + ->writeValue($property->typeName); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - return $this; |
|
| 325 | - } |
|
| 324 | + return $this; |
|
| 325 | + } |
|
| 326 | 326 | |
| 327 | - protected function writeBagContent(ODataBagContent $bag) |
|
| 328 | - { |
|
| 327 | + protected function writeBagContent(ODataBagContent $bag) |
|
| 328 | + { |
|
| 329 | 329 | |
| 330 | - $this->_writer |
|
| 330 | + $this->_writer |
|
| 331 | 331 | |
| 332 | 332 | |
| 333 | - /* |
|
| 333 | + /* |
|
| 334 | 334 | ->writeName(ODataConstants::JSON_METADATA_STRING) //__metadata : { Type : "typename" } |
| 335 | 335 | ->startObjectScope() |
| 336 | 336 | |
@@ -340,60 +340,60 @@ discard block |
||
| 340 | 340 | */ |
| 341 | 341 | |
| 342 | 342 | |
| 343 | - ->startArrayScope(); // [ |
|
| 344 | - |
|
| 345 | - foreach ($bag->propertyContents as $content) { |
|
| 346 | - if ($content instanceof ODataPropertyContent) { |
|
| 347 | - $this->_writer->startObjectScope(); |
|
| 348 | - $this->writeProperties($content); |
|
| 349 | - $this->_writer->endScope(); |
|
| 350 | - } else { |
|
| 351 | - // retrieving the collection datatype in order |
|
| 352 | - //to write in json specific format, with in chords or not |
|
| 353 | - preg_match('#\((.*?)\)#', $bag->type, $type); |
|
| 354 | - $this->_writer->writeValue($content, $type[1]); |
|
| 355 | - } |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - |
|
| 359 | - $this->_writer->endScope(); // ] |
|
| 360 | - return $this; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * @param ProvidersWrapper $providers |
|
| 365 | - * @return IODataWriter |
|
| 366 | - */ |
|
| 367 | - public function writeServiceDocument(ProvidersWrapper $providers){ |
|
| 368 | - $writer = $this->_writer; |
|
| 369 | - $writer |
|
| 370 | - ->startObjectScope() // { |
|
| 371 | - ->writeName("odata.metadata") |
|
| 372 | - ->writeValue("{$this->baseUri}/\$metadata") |
|
| 373 | - ->writeName("value") // "value" : |
|
| 374 | - // ->writeName(ODataConstants::ENTITY_SET) // "EntitySets" |
|
| 375 | - ->startArrayScope() // [ |
|
| 376 | - ; |
|
| 377 | - |
|
| 378 | - foreach ($providers->getResourceSets() as $resourceSetWrapper) { |
|
| 379 | - $name = $resourceSetWrapper->getName(); |
|
| 380 | - $writer |
|
| 381 | - ->startObjectScope() // { |
|
| 382 | - ->writeName("name") // "name" : |
|
| 383 | - ->writeValue($name) |
|
| 384 | - ->writeName("url") // "name" : |
|
| 385 | - ->writeValue($name) |
|
| 386 | - ->endScope() // } |
|
| 387 | - ; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - $writer |
|
| 391 | - ->endScope() // ] |
|
| 392 | - ->endScope() // } |
|
| 393 | - ; |
|
| 394 | - |
|
| 395 | - return $this; |
|
| 396 | - |
|
| 397 | - } |
|
| 343 | + ->startArrayScope(); // [ |
|
| 344 | + |
|
| 345 | + foreach ($bag->propertyContents as $content) { |
|
| 346 | + if ($content instanceof ODataPropertyContent) { |
|
| 347 | + $this->_writer->startObjectScope(); |
|
| 348 | + $this->writeProperties($content); |
|
| 349 | + $this->_writer->endScope(); |
|
| 350 | + } else { |
|
| 351 | + // retrieving the collection datatype in order |
|
| 352 | + //to write in json specific format, with in chords or not |
|
| 353 | + preg_match('#\((.*?)\)#', $bag->type, $type); |
|
| 354 | + $this->_writer->writeValue($content, $type[1]); |
|
| 355 | + } |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + |
|
| 359 | + $this->_writer->endScope(); // ] |
|
| 360 | + return $this; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * @param ProvidersWrapper $providers |
|
| 365 | + * @return IODataWriter |
|
| 366 | + */ |
|
| 367 | + public function writeServiceDocument(ProvidersWrapper $providers){ |
|
| 368 | + $writer = $this->_writer; |
|
| 369 | + $writer |
|
| 370 | + ->startObjectScope() // { |
|
| 371 | + ->writeName("odata.metadata") |
|
| 372 | + ->writeValue("{$this->baseUri}/\$metadata") |
|
| 373 | + ->writeName("value") // "value" : |
|
| 374 | + // ->writeName(ODataConstants::ENTITY_SET) // "EntitySets" |
|
| 375 | + ->startArrayScope() // [ |
|
| 376 | + ; |
|
| 377 | + |
|
| 378 | + foreach ($providers->getResourceSets() as $resourceSetWrapper) { |
|
| 379 | + $name = $resourceSetWrapper->getName(); |
|
| 380 | + $writer |
|
| 381 | + ->startObjectScope() // { |
|
| 382 | + ->writeName("name") // "name" : |
|
| 383 | + ->writeValue($name) |
|
| 384 | + ->writeName("url") // "name" : |
|
| 385 | + ->writeValue($name) |
|
| 386 | + ->endScope() // } |
|
| 387 | + ; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + $writer |
|
| 391 | + ->endScope() // ] |
|
| 392 | + ->endScope() // } |
|
| 393 | + ; |
|
| 394 | + |
|
| 395 | + return $this; |
|
| 396 | + |
|
| 397 | + } |
|
| 398 | 398 | |
| 399 | 399 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | |
| 47 | 47 | public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri) |
| 48 | 48 | { |
| 49 | - if(strlen($absoluteServiceUri) == 0) |
|
| 49 | + if (strlen($absoluteServiceUri) == 0) |
|
| 50 | 50 | { |
| 51 | 51 | throw new \Exception("absoluteServiceUri must not be empty or null"); |
| 52 | 52 | } |
@@ -68,14 +68,14 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function canHandle(Version $responseVersion, $contentType) |
| 70 | 70 | { |
| 71 | - if($responseVersion != Version::v3()){ |
|
| 71 | + if ($responseVersion != Version::v3()) { |
|
| 72 | 72 | return false; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | $parts = explode(";", $contentType); |
| 76 | 76 | |
| 77 | 77 | //It must be app/json and have the right odata= piece |
| 78 | - $metadata = array_filter($parts, function ($item) { return strpos($item, 'odata') !== false; }); |
|
| 78 | + $metadata = array_filter($parts, function($item) { return strpos($item, 'odata') !== false; }); |
|
| 79 | 79 | return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && (empty($metadata) || in_array($this->metadataLevel->getValue(), $metadata)); |
| 80 | 80 | } |
| 81 | 81 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return JsonLightODataWriter |
| 90 | 90 | */ |
| 91 | - public function write($model){ |
|
| 91 | + public function write($model) { |
|
| 92 | 92 | $this->_writer->startObjectScope(); |
| 93 | 93 | |
| 94 | 94 | if ($model instanceof ODataURL) { |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | $this->writeTopLevelMeta("urlCollection"); |
| 99 | 99 | $this->writeURLCollection($model); |
| 100 | 100 | } elseif ($model instanceof ODataPropertyContent) { |
| 101 | - $this->writeTopLevelMeta( $model->properties[0]->typeName ); |
|
| 101 | + $this->writeTopLevelMeta($model->properties[0]->typeName); |
|
| 102 | 102 | $this->writeTopLevelProperty($model->properties[0]); |
| 103 | 103 | } elseif ($model instanceof ODataFeed) { |
| 104 | 104 | $this->writeTopLevelMeta($model->title); |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | |
| 153 | 153 | protected function writeTopLevelMeta($fragment) |
| 154 | 154 | { |
| 155 | - if($this->metadataLevel == JsonLightMetadataLevel::NONE()) |
|
| 155 | + if ($this->metadataLevel == JsonLightMetadataLevel::NONE()) |
|
| 156 | 156 | { |
| 157 | 157 | return; |
| 158 | 158 | } |
@@ -166,20 +166,20 @@ discard block |
||
| 166 | 166 | |
| 167 | 167 | protected function writePropertyMeta(ODataProperty $property) |
| 168 | 168 | { |
| 169 | - if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 169 | + if ($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 170 | 170 | { |
| 171 | 171 | //Only full meta level outputs this info |
| 172 | 172 | return $this; |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - if(is_null($property->value)) |
|
| 175 | + if (is_null($property->value)) |
|
| 176 | 176 | { |
| 177 | 177 | //it appears full metadata doesn't output types for nulls... |
| 178 | 178 | return $this; |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
| 182 | - switch($property->typeName) |
|
| 182 | + switch ($property->typeName) |
|
| 183 | 183 | { |
| 184 | 184 | //The type metadata is only included on certain types of properties |
| 185 | 185 | //Note this also excludes Complex types |
@@ -201,9 +201,9 @@ discard block |
||
| 201 | 201 | * |
| 202 | 202 | * @return JsonLightODataWriter |
| 203 | 203 | */ |
| 204 | - protected function writeEntryMetadata(ODataEntry $entry){ |
|
| 204 | + protected function writeEntryMetadata(ODataEntry $entry) { |
|
| 205 | 205 | |
| 206 | - if($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 206 | + if ($this->metadataLevel != JsonLightMetadataLevel::FULL()) |
|
| 207 | 207 | { |
| 208 | 208 | //Only full meta level outputs this info |
| 209 | 209 | return $this; |
@@ -229,22 +229,22 @@ discard block |
||
| 229 | 229 | * |
| 230 | 230 | * @return JsonLightODataWriter |
| 231 | 231 | */ |
| 232 | - protected function writeLink(ODataLink $link){ |
|
| 232 | + protected function writeLink(ODataLink $link) { |
|
| 233 | 233 | |
| 234 | - if($this->metadataLevel == JsonLightMetadataLevel::FULL()) |
|
| 234 | + if ($this->metadataLevel == JsonLightMetadataLevel::FULL()) |
|
| 235 | 235 | { |
| 236 | 236 | //Interestingly the fullmetadata outputs this metadata..even if the thing is expanded |
| 237 | 237 | $this->_writer |
| 238 | 238 | ->writeName($link->title . ODataConstants::JSON_LIGHT_METADATA_LINK_NAVIGATION_SUFFIX_STRING) |
| 239 | - ->writeValue($link->url);; |
|
| 239 | + ->writeValue($link->url); ; |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | |
| 243 | - if($link->isExpanded) |
|
| 243 | + if ($link->isExpanded) |
|
| 244 | 244 | { |
| 245 | 245 | $this->_writer->writeName($link->title); |
| 246 | 246 | |
| 247 | - if(is_null($link->expandedResult)) { |
|
| 247 | + if (is_null($link->expandedResult)) { |
|
| 248 | 248 | $this->_writer->writeValue("null"); |
| 249 | 249 | } else { |
| 250 | 250 | $this->writeExpandedLink($link); |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | |
| 316 | 316 | protected function writeComplexPropertyMeta(ODataProperty $property) |
| 317 | 317 | { |
| 318 | - if($this->metadataLevel == JsonLightMetadataLevel::FULL()){ |
|
| 318 | + if ($this->metadataLevel == JsonLightMetadataLevel::FULL()) { |
|
| 319 | 319 | $this->_writer |
| 320 | 320 | ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING) |
| 321 | 321 | ->writeValue($property->typeName); |
@@ -356,7 +356,7 @@ discard block |
||
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | |
| 359 | - $this->_writer->endScope(); // ] |
|
| 359 | + $this->_writer->endScope(); // ] |
|
| 360 | 360 | return $this; |
| 361 | 361 | } |
| 362 | 362 | |
@@ -364,7 +364,7 @@ discard block |
||
| 364 | 364 | * @param ProvidersWrapper $providers |
| 365 | 365 | * @return IODataWriter |
| 366 | 366 | */ |
| 367 | - public function writeServiceDocument(ProvidersWrapper $providers){ |
|
| 367 | + public function writeServiceDocument(ProvidersWrapper $providers) { |
|
| 368 | 368 | $writer = $this->_writer; |
| 369 | 369 | $writer |
| 370 | 370 | ->startObjectScope() // { |
@@ -189,7 +189,7 @@ |
||
| 189 | 189 | * |
| 190 | 190 | * @param ResourceType[] $resourceTypes resource types array |
| 191 | 191 | |
| 192 | - * @param array $associationTypesInResourceTypesNamespace collection of |
|
| 192 | + * @param ResourceAssociationType[] $associationTypesInResourceTypesNamespace collection of |
|
| 193 | 193 | * association types for the given resource types |
| 194 | 194 | * array(string, AssociationType) |
| 195 | 195 | * |
@@ -188,7 +188,6 @@ discard block |
||
| 188 | 188 | * Write all resource types (entity and complex types) |
| 189 | 189 | * |
| 190 | 190 | * @param ResourceType[] $resourceTypes resource types array |
| 191 | - |
|
| 192 | 191 | * @param array $associationTypesInResourceTypesNamespace collection of |
| 193 | 192 | * association types for the given resource types |
| 194 | 193 | * array(string, AssociationType) |
@@ -290,10 +289,10 @@ discard block |
||
| 290 | 289 | } else if ($resourceProperty->isKindOf(ResourcePropertyKind::RESOURCE_REFERENCE) |
| 291 | 290 | || $resourceProperty->isKindOf(ResourcePropertyKind::RESOURCESET_REFERENCE) |
| 292 | 291 | ) { |
| 293 | - $this->_writeNavigationProperty($resourceType, $associationTypesInResourceTypeNamespace, $resourceProperty); |
|
| 292 | + $this->_writeNavigationProperty($resourceType, $associationTypesInResourceTypeNamespace, $resourceProperty); |
|
| 294 | 293 | } else { |
| 295 | - //Unexpected ResourceProperty, expected |
|
| 296 | - //Bag/Primitive/Complex/Navigation Property |
|
| 294 | + //Unexpected ResourceProperty, expected |
|
| 295 | + //Bag/Primitive/Complex/Navigation Property |
|
| 297 | 296 | } |
| 298 | 297 | } |
| 299 | 298 | } |
@@ -530,23 +529,23 @@ discard block |
||
| 530 | 529 | private function _getSchemaNamespaceUri($edmSchemaVersion) |
| 531 | 530 | { |
| 532 | 531 | switch ($edmSchemaVersion) { |
| 533 | - case EdmSchemaVersion::VERSION_1_DOT_0: |
|
| 534 | - return ODataConstants::CSDL_VERSION_1_0; |
|
| 532 | + case EdmSchemaVersion::VERSION_1_DOT_0: |
|
| 533 | + return ODataConstants::CSDL_VERSION_1_0; |
|
| 535 | 534 | |
| 536 | - case EdmSchemaVersion::VERSION_1_DOT_1: |
|
| 537 | - return ODataConstants::CSDL_VERSION_1_1; |
|
| 535 | + case EdmSchemaVersion::VERSION_1_DOT_1: |
|
| 536 | + return ODataConstants::CSDL_VERSION_1_1; |
|
| 538 | 537 | |
| 539 | - case EdmSchemaVersion::VERSION_1_DOT_2: |
|
| 540 | - return ODataConstants::CSDL_VERSION_1_2; |
|
| 538 | + case EdmSchemaVersion::VERSION_1_DOT_2: |
|
| 539 | + return ODataConstants::CSDL_VERSION_1_2; |
|
| 541 | 540 | |
| 542 | - case EdmSchemaVersion::VERSION_2_DOT_0: |
|
| 543 | - return ODataConstants::CSDL_VERSION_2_0; |
|
| 541 | + case EdmSchemaVersion::VERSION_2_DOT_0: |
|
| 542 | + return ODataConstants::CSDL_VERSION_2_0; |
|
| 544 | 543 | |
| 545 | - case EdmSchemaVersion::VERSION_2_DOT_2: |
|
| 546 | - return ODataConstants::CSDL_VERSION_2_2; |
|
| 544 | + case EdmSchemaVersion::VERSION_2_DOT_2: |
|
| 545 | + return ODataConstants::CSDL_VERSION_2_2; |
|
| 547 | 546 | |
| 548 | - default: |
|
| 549 | - return ODataConstants::CSDL_VERSION_2_2; |
|
| 547 | + default: |
|
| 548 | + return ODataConstants::CSDL_VERSION_2_2; |
|
| 550 | 549 | } |
| 551 | 550 | } |
| 552 | 551 | } |