Completed
Push — master ( 6f5720...a297dc )
by Nikolay
07:30
created
src/POData/Writers/Json/JsonLightMetadataLevel.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -15,11 +15,11 @@
 block discarded – undo
15 15
  * @method static \POData\Writers\Json\JsonLightMetadataLevel FULL()
16 16
  */
17 17
 class JsonLightMetadataLevel extends Enum {
18
-	//using these const because it makes them easy to use in writer canHandle..but maybe not such a good idea
19
-	const NONE = "odata=nometadata";
18
+    //using these const because it makes them easy to use in writer canHandle..but maybe not such a good idea
19
+    const NONE = "odata=nometadata";
20 20
 
21
-	const MINIMAL = "odata=minimalmetadata";
21
+    const MINIMAL = "odata=minimalmetadata";
22 22
 
23
-	const FULL = "odata=fullmetadata";
23
+    const FULL = "odata=fullmetadata";
24 24
 
25 25
 }
26 26
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/Json/JsonLightODataWriter.php 1 patch
Indentation   +295 added lines, -295 removed lines patch added patch discarded remove patch
@@ -29,255 +29,255 @@  discard block
 block discarded – undo
29 29
 class JsonLightODataWriter extends JsonODataV2Writer
30 30
 {
31 31
 
32
-	/**
33
-	 * @var JsonLightMetadataLevel
34
-	 */
35
-	protected $metadataLevel;
36
-
37
-
38
-	/**
39
-	 *
40
-	 * The service base uri
41
-	 * @var string
42
-	 */
43
-	protected $baseUri;
44
-
45
-
46
-	public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri)
47
-	{
48
-		if(strlen($absoluteServiceUri) == 0)
49
-		{
50
-			throw new \Exception("absoluteServiceUri must not be empty or null");
51
-		}
52
-		$this->baseUri = $absoluteServiceUri;
53
-
54
-		$this->_writer = new JsonWriter('');
55
-		$this->urlKey = ODataConstants::JSON_URL_STRING;
56
-		$this->dataArrayName = ODataConstants::JSON_LIGHT_VALUE_NAME;
57
-		$this->rowCountName = ODataConstants::JSON_LIGHT_ROWCOUNT_STRING;
58
-		$this->metadataLevel = $metadataLevel;
59
-	}
60
-
61
-
62
-	/**
63
-	 * Determines if the given writer is capable of writing the response or not
64
-	 * @param Version $responseVersion the OData version of the response
65
-	 * @param string $contentType the Content Type of the response
66
-	 * @return boolean true if the writer can handle the response, false otherwise
67
-	 */
68
-	public function canHandle(Version $responseVersion, $contentType)
69
-	{
70
-		if($responseVersion != Version::v3()){
71
-			return false;
72
-		}
73
-
74
-		$parts = explode(";", $contentType);
75
-
76
-		//It must be app/json and have the right odata= piece
77
-		return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && in_array($this->metadataLevel->getValue(), $parts);
78
-	}
79
-
80
-
81
-
82
-	/**
83
-	 * Write the given OData model in a specific response format
84
-	 *
85
-	 * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
86
-	 *
87
-	 * @return JsonLightODataWriter
88
-	 */
89
-	public function write($model){
90
-		$this->_writer->startObjectScope();
91
-
92
-		if ($model instanceof ODataURL) {
93
-			$this->writeTopLevelMeta("url");
94
-			$this->writeURL($model);
95
-		} elseif ($model instanceof ODataURLCollection) {
96
-			$this->writeTopLevelMeta("urlCollection");
97
-			$this->writeURLCollection($model);
98
-		} elseif ($model instanceof ODataPropertyContent) {
99
-			$this->writeTopLevelMeta( $model->properties[0]->typeName );
100
-			$this->writeTopLevelProperty($model->properties[0]);
101
-		} elseif ($model instanceof ODataFeed) {
102
-			$this->writeTopLevelMeta($model->title);
103
-			$this->writeRowCount($model->rowCount);
104
-			$this->writeNextPageLink($model->nextPageLink);
105
-			$this->_writer
106
-				->writeName($this->dataArrayName)
107
-				->startArrayScope();
108
-			$this->writeFeed($model);
109
-			$this->_writer->endScope();
110
-		}elseif ($model instanceof ODataEntry) {
111
-			$this->writeTopLevelMeta($model->resourceSetName . "/@Element");
112
-			$this->writeEntry($model);
113
-		}
114
-
115
-		$this->_writer->endScope();
116
-
117
-		return $this;
118
-	}
119
-
120
-
121
-	/**
122
-	 *
123
-	 * @param ODataProperty $property
124
-	 *
125
-	 * @return JsonLightODataWriter
126
-	 */
127
-	protected function writeTopLevelProperty(ODataProperty $property)
128
-	{
129
-		$this->writePropertyMeta($property);
130
-		if ($property->value == null) {
131
-			$this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
132
-			$this->_writer->writeValue("null");
133
-		} elseif ($property->value instanceof ODataPropertyContent) {
134
-			//In the case of complex properties at the top level we don't write the name of the property,
135
-			//just the sub properties.
136
-			$this->writeComplexPropertyMeta($property)
137
-				->writeProperties($property->value);
138
-		} elseif ($property->value instanceof ODataBagContent) {
139
-			$this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
140
-			$this->writeBagContent($property->value);
141
-		} else {
142
-			$this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
143
-			$this->_writer->writeValue($property->value, $property->typeName);
144
-		}
145
-
146
-		return $this;
147
-	}
148
-
149
-
150
-
151
-	protected function writeTopLevelMeta($fragment)
152
-	{
153
-		if($this->metadataLevel == JsonLightMetadataLevel::NONE())
154
-		{
155
-			return;
156
-		}
157
-
158
-		$this->_writer
159
-			->writeName(ODataConstants::JSON_LIGHT_METADATA_STRING)
160
-			->writeValue($this->baseUri . '/' . ODataConstants::URI_METADATA_SEGMENT . '#' . $fragment);
161
-
162
-	}
163
-
164
-
165
-	protected function writePropertyMeta(ODataProperty $property)
166
-	{
167
-		if($this->metadataLevel != JsonLightMetadataLevel::FULL())
168
-		{
169
-			//Only full meta level outputs this info
170
-			return $this;
171
-		}
172
-
173
-		if(is_null($property->value))
174
-		{
175
-			//it appears full metadata doesn't output types for nulls...
176
-			return $this;
177
-		}
178
-
179
-
180
-		switch($property->typeName)
181
-		{
182
-			//The type metadata is only included on certain types of properties
183
-			//Note this also excludes Complex types
184
-
185
-			case "Edm.Decimal":
186
-			case "Edm.DateTime":
187
-				$this->_writer
188
-					->writeName($property->name . ODataConstants::JSON_LIGHT_METADATA_PROPERTY_TYPE_SUFFIX_STRING)
189
-					->writeValue($property->typeName);
190
-		}
191
-
192
-
193
-		return $this;
194
-	}
195
-
196
-	/**
197
-	 *
198
-	 * @param ODataEntry $entry Entry to write metadata for.
199
-	 *
200
-	 * @return JsonLightODataWriter
201
-	 */
202
-	protected function writeEntryMetadata(ODataEntry $entry){
203
-
204
-		if($this->metadataLevel != JsonLightMetadataLevel::FULL())
205
-		{
206
-			//Only full meta level outputs this info
207
-			return $this;
208
-		}
209
-
210
-		$this->_writer
211
-			->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING)
212
-			->writeValue($entry->type)
213
-			->writeName(ODataConstants::JSON_LIGHT_METADATA_ID_STRING)
214
-			->writeValue($entry->id)
215
-			->writeName(ODataConstants::JSON_LIGHT_METADATA_ETAG_STRING)
216
-			->writeValue($entry->eTag)
217
-			->writeName(ODataConstants::JSON_LIGHT_METADATA_EDIT_LINK_STRING)
218
-			->writeValue($entry->editLink)
219
-		;
220
-
221
-		return $this;
222
-	}
223
-
224
-
225
-	/**
226
-	 * @param ODataLink $link Link to write.
227
-	 *
228
-	 * @return JsonLightODataWriter
229
-	 */
230
-	protected function writeLink(ODataLink $link){
231
-
232
-		if($this->metadataLevel == JsonLightMetadataLevel::FULL())
233
-		{
234
-			//Interestingly the fullmetadata outputs this metadata..even if the thing is expanded
235
-			$this->_writer
236
-				->writeName($link->title . ODataConstants::JSON_LIGHT_METADATA_LINK_NAVIGATION_SUFFIX_STRING)
237
-				->writeValue($link->url);;
238
-		}
239
-
240
-
241
-		if($link->isExpanded)
242
-		{
243
-			$this->_writer->writeName($link->title);
244
-
245
-			if(is_null($link->expandedResult)) {
246
-				$this->_writer->writeValue("null");
247
-			} else {
248
-				$this->writeExpandedLink($link);
249
-			}
250
-		}
251
-
252
-		return $this;
253
-	}
254
-
255
-	protected function writeExpandedLink(ODataLink $link)
256
-	{
257
-
258
-
259
-		if ($link->isCollection) {
260
-			$this->_writer->startArrayScope();
261
-			$this->writeFeed($link->expandedResult);
262
-		} else {
263
-			$this->_writer->startObjectScope();
264
-			$this->writeEntry($link->expandedResult);
265
-		}
266
-
267
-
268
-		$this->_writer->endScope();
269
-	}
270
-
271
-	/**
272
-	 * Writes the next page link.
273
-	 *
274
-	 * @param ODataLink $nextPageLinkUri Uri for next page link.
275
-	 *
276
-	 * @return JsonLightODataWriter
277
-	 */
278
-	protected function writeNextPageLink(ODataLink $nextPageLinkUri = null)
279
-	{
280
-		/*
32
+    /**
33
+     * @var JsonLightMetadataLevel
34
+     */
35
+    protected $metadataLevel;
36
+
37
+
38
+    /**
39
+     *
40
+     * The service base uri
41
+     * @var string
42
+     */
43
+    protected $baseUri;
44
+
45
+
46
+    public function __construct(JsonLightMetadataLevel $metadataLevel, $absoluteServiceUri)
47
+    {
48
+        if(strlen($absoluteServiceUri) == 0)
49
+        {
50
+            throw new \Exception("absoluteServiceUri must not be empty or null");
51
+        }
52
+        $this->baseUri = $absoluteServiceUri;
53
+
54
+        $this->_writer = new JsonWriter('');
55
+        $this->urlKey = ODataConstants::JSON_URL_STRING;
56
+        $this->dataArrayName = ODataConstants::JSON_LIGHT_VALUE_NAME;
57
+        $this->rowCountName = ODataConstants::JSON_LIGHT_ROWCOUNT_STRING;
58
+        $this->metadataLevel = $metadataLevel;
59
+    }
60
+
61
+
62
+    /**
63
+     * Determines if the given writer is capable of writing the response or not
64
+     * @param Version $responseVersion the OData version of the response
65
+     * @param string $contentType the Content Type of the response
66
+     * @return boolean true if the writer can handle the response, false otherwise
67
+     */
68
+    public function canHandle(Version $responseVersion, $contentType)
69
+    {
70
+        if($responseVersion != Version::v3()){
71
+            return false;
72
+        }
73
+
74
+        $parts = explode(";", $contentType);
75
+
76
+        //It must be app/json and have the right odata= piece
77
+        return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && in_array($this->metadataLevel->getValue(), $parts);
78
+    }
79
+
80
+
81
+
82
+    /**
83
+     * Write the given OData model in a specific response format
84
+     *
85
+     * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
86
+     *
87
+     * @return JsonLightODataWriter
88
+     */
89
+    public function write($model){
90
+        $this->_writer->startObjectScope();
91
+
92
+        if ($model instanceof ODataURL) {
93
+            $this->writeTopLevelMeta("url");
94
+            $this->writeURL($model);
95
+        } elseif ($model instanceof ODataURLCollection) {
96
+            $this->writeTopLevelMeta("urlCollection");
97
+            $this->writeURLCollection($model);
98
+        } elseif ($model instanceof ODataPropertyContent) {
99
+            $this->writeTopLevelMeta( $model->properties[0]->typeName );
100
+            $this->writeTopLevelProperty($model->properties[0]);
101
+        } elseif ($model instanceof ODataFeed) {
102
+            $this->writeTopLevelMeta($model->title);
103
+            $this->writeRowCount($model->rowCount);
104
+            $this->writeNextPageLink($model->nextPageLink);
105
+            $this->_writer
106
+                ->writeName($this->dataArrayName)
107
+                ->startArrayScope();
108
+            $this->writeFeed($model);
109
+            $this->_writer->endScope();
110
+        }elseif ($model instanceof ODataEntry) {
111
+            $this->writeTopLevelMeta($model->resourceSetName . "/@Element");
112
+            $this->writeEntry($model);
113
+        }
114
+
115
+        $this->_writer->endScope();
116
+
117
+        return $this;
118
+    }
119
+
120
+
121
+    /**
122
+     *
123
+     * @param ODataProperty $property
124
+     *
125
+     * @return JsonLightODataWriter
126
+     */
127
+    protected function writeTopLevelProperty(ODataProperty $property)
128
+    {
129
+        $this->writePropertyMeta($property);
130
+        if ($property->value == null) {
131
+            $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
132
+            $this->_writer->writeValue("null");
133
+        } elseif ($property->value instanceof ODataPropertyContent) {
134
+            //In the case of complex properties at the top level we don't write the name of the property,
135
+            //just the sub properties.
136
+            $this->writeComplexPropertyMeta($property)
137
+                ->writeProperties($property->value);
138
+        } elseif ($property->value instanceof ODataBagContent) {
139
+            $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
140
+            $this->writeBagContent($property->value);
141
+        } else {
142
+            $this->_writer->writeName(ODataConstants::JSON_LIGHT_VALUE_NAME);
143
+            $this->_writer->writeValue($property->value, $property->typeName);
144
+        }
145
+
146
+        return $this;
147
+    }
148
+
149
+
150
+
151
+    protected function writeTopLevelMeta($fragment)
152
+    {
153
+        if($this->metadataLevel == JsonLightMetadataLevel::NONE())
154
+        {
155
+            return;
156
+        }
157
+
158
+        $this->_writer
159
+            ->writeName(ODataConstants::JSON_LIGHT_METADATA_STRING)
160
+            ->writeValue($this->baseUri . '/' . ODataConstants::URI_METADATA_SEGMENT . '#' . $fragment);
161
+
162
+    }
163
+
164
+
165
+    protected function writePropertyMeta(ODataProperty $property)
166
+    {
167
+        if($this->metadataLevel != JsonLightMetadataLevel::FULL())
168
+        {
169
+            //Only full meta level outputs this info
170
+            return $this;
171
+        }
172
+
173
+        if(is_null($property->value))
174
+        {
175
+            //it appears full metadata doesn't output types for nulls...
176
+            return $this;
177
+        }
178
+
179
+
180
+        switch($property->typeName)
181
+        {
182
+            //The type metadata is only included on certain types of properties
183
+            //Note this also excludes Complex types
184
+
185
+            case "Edm.Decimal":
186
+            case "Edm.DateTime":
187
+                $this->_writer
188
+                    ->writeName($property->name . ODataConstants::JSON_LIGHT_METADATA_PROPERTY_TYPE_SUFFIX_STRING)
189
+                    ->writeValue($property->typeName);
190
+        }
191
+
192
+
193
+        return $this;
194
+    }
195
+
196
+    /**
197
+     *
198
+     * @param ODataEntry $entry Entry to write metadata for.
199
+     *
200
+     * @return JsonLightODataWriter
201
+     */
202
+    protected function writeEntryMetadata(ODataEntry $entry){
203
+
204
+        if($this->metadataLevel != JsonLightMetadataLevel::FULL())
205
+        {
206
+            //Only full meta level outputs this info
207
+            return $this;
208
+        }
209
+
210
+        $this->_writer
211
+            ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING)
212
+            ->writeValue($entry->type)
213
+            ->writeName(ODataConstants::JSON_LIGHT_METADATA_ID_STRING)
214
+            ->writeValue($entry->id)
215
+            ->writeName(ODataConstants::JSON_LIGHT_METADATA_ETAG_STRING)
216
+            ->writeValue($entry->eTag)
217
+            ->writeName(ODataConstants::JSON_LIGHT_METADATA_EDIT_LINK_STRING)
218
+            ->writeValue($entry->editLink)
219
+        ;
220
+
221
+        return $this;
222
+    }
223
+
224
+
225
+    /**
226
+     * @param ODataLink $link Link to write.
227
+     *
228
+     * @return JsonLightODataWriter
229
+     */
230
+    protected function writeLink(ODataLink $link){
231
+
232
+        if($this->metadataLevel == JsonLightMetadataLevel::FULL())
233
+        {
234
+            //Interestingly the fullmetadata outputs this metadata..even if the thing is expanded
235
+            $this->_writer
236
+                ->writeName($link->title . ODataConstants::JSON_LIGHT_METADATA_LINK_NAVIGATION_SUFFIX_STRING)
237
+                ->writeValue($link->url);;
238
+        }
239
+
240
+
241
+        if($link->isExpanded)
242
+        {
243
+            $this->_writer->writeName($link->title);
244
+
245
+            if(is_null($link->expandedResult)) {
246
+                $this->_writer->writeValue("null");
247
+            } else {
248
+                $this->writeExpandedLink($link);
249
+            }
250
+        }
251
+
252
+        return $this;
253
+    }
254
+
255
+    protected function writeExpandedLink(ODataLink $link)
256
+    {
257
+
258
+
259
+        if ($link->isCollection) {
260
+            $this->_writer->startArrayScope();
261
+            $this->writeFeed($link->expandedResult);
262
+        } else {
263
+            $this->_writer->startObjectScope();
264
+            $this->writeEntry($link->expandedResult);
265
+        }
266
+
267
+
268
+        $this->_writer->endScope();
269
+    }
270
+
271
+    /**
272
+     * Writes the next page link.
273
+     *
274
+     * @param ODataLink $nextPageLinkUri Uri for next page link.
275
+     *
276
+     * @return JsonLightODataWriter
277
+     */
278
+    protected function writeNextPageLink(ODataLink $nextPageLinkUri = null)
279
+    {
280
+        /*
281 281
 		// "__next" : uri
282 282
 		if ($nextPageLinkUri != null) {
283 283
 			$this->_writer
@@ -287,48 +287,48 @@  discard block
 block discarded – undo
287 287
 
288 288
 		return $this;
289 289
 		*/
290
-	}
290
+    }
291 291
 
292 292
 
293
-	/**
294
-	 * Begin write complex property.
295
-	 *
296
-	 * @param ODataProperty $property property to write.
297
-	 *
298
-	 * @return JsonLightODataWriter
299
-	 */
300
-	protected function writeComplexProperty(ODataProperty $property)
301
-	{
293
+    /**
294
+     * Begin write complex property.
295
+     *
296
+     * @param ODataProperty $property property to write.
297
+     *
298
+     * @return JsonLightODataWriter
299
+     */
300
+    protected function writeComplexProperty(ODataProperty $property)
301
+    {
302 302
 
303
-		// {
304
-		$this->_writer->startObjectScope();
303
+        // {
304
+        $this->_writer->startObjectScope();
305 305
 
306
-		$this->writeComplexPropertyMeta($property)
307
-			->writeProperties($property->value);
306
+        $this->writeComplexPropertyMeta($property)
307
+            ->writeProperties($property->value);
308 308
 
309
-		$this->_writer->endScope();
309
+        $this->_writer->endScope();
310 310
 
311
-		return $this;
312
-	}
311
+        return $this;
312
+    }
313 313
 
314
-	protected function writeComplexPropertyMeta(ODataProperty $property)
315
-	{
316
-		if($this->metadataLevel == JsonLightMetadataLevel::FULL()){
317
-			$this->_writer
318
-				->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING)
319
-				->writeValue($property->typeName);
320
-		}
314
+    protected function writeComplexPropertyMeta(ODataProperty $property)
315
+    {
316
+        if($this->metadataLevel == JsonLightMetadataLevel::FULL()){
317
+            $this->_writer
318
+                ->writeName(ODataConstants::JSON_LIGHT_METADATA_TYPE_STRING)
319
+                ->writeValue($property->typeName);
320
+        }
321 321
 
322
-		return $this;
323
-	}
322
+        return $this;
323
+    }
324 324
 
325
-	protected function writeBagContent(ODataBagContent $bag)
326
-	{
325
+    protected function writeBagContent(ODataBagContent $bag)
326
+    {
327 327
 
328
-		$this->_writer
328
+        $this->_writer
329 329
 
330 330
 
331
-			/*
331
+            /*
332 332
 			->writeName(ODataConstants::JSON_METADATA_STRING) //__metadata : { Type : "typename" }
333 333
 			->startObjectScope()
334 334
 
@@ -338,24 +338,24 @@  discard block
 block discarded – undo
338 338
 			*/
339 339
 
340 340
 
341
-			->startArrayScope(); // [
341
+            ->startArrayScope(); // [
342 342
 
343
-		foreach ($bag->propertyContents as $content) {
344
-			if ($content instanceof ODataPropertyContent) {
345
-				$this->_writer->startObjectScope();
346
-				$this->writeProperties($content);
347
-				$this->_writer->endScope();
348
-			} else {
349
-				// retrieving the collection datatype in order
350
-				//to write in json specific format, with in chords or not
351
-				preg_match('#\((.*?)\)#', $bag->type, $type);
352
-				$this->_writer->writeValue($content, $type[1]);
353
-			}
354
-		}
343
+        foreach ($bag->propertyContents as $content) {
344
+            if ($content instanceof ODataPropertyContent) {
345
+                $this->_writer->startObjectScope();
346
+                $this->writeProperties($content);
347
+                $this->_writer->endScope();
348
+            } else {
349
+                // retrieving the collection datatype in order
350
+                //to write in json specific format, with in chords or not
351
+                preg_match('#\((.*?)\)#', $bag->type, $type);
352
+                $this->_writer->writeValue($content, $type[1]);
353
+            }
354
+        }
355 355
 
356 356
 
357
-		$this->_writer->endScope();  // ]
358
-		return $this;
359
-	}
357
+        $this->_writer->endScope();  // ]
358
+        return $this;
359
+    }
360 360
 
361 361
 }
362 362
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/Json/JsonODataV1Writer.php 1 patch
Indentation   +198 added lines, -198 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
     protected $_writer;
35 35
 
36 36
 
37
-	protected $urlKey = ODataConstants::JSON_URI_STRING;
37
+    protected $urlKey = ODataConstants::JSON_URI_STRING;
38 38
 
39 39
     /**
40 40
      * Constructs and initializes the Json output writer.
@@ -45,60 +45,60 @@  discard block
 block discarded – undo
45 45
         $this->_writer = new JsonWriter('');
46 46
     }
47 47
 
48
-	/**
49
-	 * Determines if the given writer is capable of writing the response or not
50
-	 * @param Version $responseVersion the OData version of the response
51
-	 * @param string $contentType the Content Type of the response
52
-	 * @return boolean true if the writer can handle the response, false otherwise
53
-	 */
54
-	public function canHandle(Version $responseVersion, $contentType)
55
-	{
56
-		if($responseVersion != Version::v1()){
57
-			return false;
58
-		}
59
-
60
-		$parts = explode(";", $contentType);
61
-
62
-		return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts);
63
-	}
64
-
65
-	/**
66
-	 * Write the given OData model in a specific response format
67
-	 *
68
-	 * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
69
-	 *
70
-	 * @return JsonODataV1Writer
71
-	 */
72
-	public function write($model){
73
-		// { "d" :
74
-		$this->_writer
75
-			->startObjectScope()
76
-			->writeName("d");
77
-
78
-
79
-		if ($model instanceof ODataURL) {
80
-			$this->_writer->startObjectScope();
81
-			$this->writeURL($model);
82
-		} elseif ($model instanceof ODataURLCollection) {
83
-			$this->_writer->startArrayScope();
84
-			$this->writeURLCollection($model);
85
-		} elseif ($model instanceof ODataPropertyContent) {
86
-			$this->_writer->startObjectScope();
87
-			$this->writeProperties($model);
88
-		} elseif ($model instanceof ODataFeed) {
89
-			$this->_writer->startArrayScope();
90
-			$this->writeFeed($model);
91
-		}elseif ($model instanceof ODataEntry) {
92
-			$this->_writer->startObjectScope();
93
-			$this->writeEntry($model);
94
-		}
95
-
96
-
97
-		$this->_writer->endScope();
98
-		$this->_writer->endScope();
99
-
100
-		return $this;
101
-	}
48
+    /**
49
+     * Determines if the given writer is capable of writing the response or not
50
+     * @param Version $responseVersion the OData version of the response
51
+     * @param string $contentType the Content Type of the response
52
+     * @return boolean true if the writer can handle the response, false otherwise
53
+     */
54
+    public function canHandle(Version $responseVersion, $contentType)
55
+    {
56
+        if($responseVersion != Version::v1()){
57
+            return false;
58
+        }
59
+
60
+        $parts = explode(";", $contentType);
61
+
62
+        return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts);
63
+    }
64
+
65
+    /**
66
+     * Write the given OData model in a specific response format
67
+     *
68
+     * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
69
+     *
70
+     * @return JsonODataV1Writer
71
+     */
72
+    public function write($model){
73
+        // { "d" :
74
+        $this->_writer
75
+            ->startObjectScope()
76
+            ->writeName("d");
77
+
78
+
79
+        if ($model instanceof ODataURL) {
80
+            $this->_writer->startObjectScope();
81
+            $this->writeURL($model);
82
+        } elseif ($model instanceof ODataURLCollection) {
83
+            $this->_writer->startArrayScope();
84
+            $this->writeURLCollection($model);
85
+        } elseif ($model instanceof ODataPropertyContent) {
86
+            $this->_writer->startObjectScope();
87
+            $this->writeProperties($model);
88
+        } elseif ($model instanceof ODataFeed) {
89
+            $this->_writer->startArrayScope();
90
+            $this->writeFeed($model);
91
+        }elseif ($model instanceof ODataEntry) {
92
+            $this->_writer->startObjectScope();
93
+            $this->writeEntry($model);
94
+        }
95
+
96
+
97
+        $this->_writer->endScope();
98
+        $this->_writer->endScope();
99
+
100
+        return $this;
101
+    }
102 102
 
103 103
 
104 104
     /**
@@ -110,9 +110,9 @@  discard block
 block discarded – undo
110 110
     {
111 111
         $this->_writer
112 112
             ->writeName($this->urlKey)
113
-	        ->writeValue($url->url);
113
+            ->writeValue($url->url);
114 114
 
115
-	    return $this;
115
+        return $this;
116 116
     }
117 117
 
118 118
     /**
@@ -125,11 +125,11 @@  discard block
 block discarded – undo
125 125
     public function writeUrlCollection(ODataURLCollection $urls)
126 126
     {
127 127
         foreach ($urls->urls as $url) {
128
-	        $this->_writer->startObjectScope();
128
+            $this->_writer->startObjectScope();
129 129
             $this->writeUrl($url);
130
-	        $this->_writer->endScope();
130
+            $this->_writer->endScope();
131 131
         }
132
-	    return $this;
132
+        return $this;
133 133
     }
134 134
 
135 135
     /**
@@ -142,14 +142,14 @@  discard block
 block discarded – undo
142 142
     protected function writeFeed(ODataFeed $feed)
143 143
     {
144 144
 
145
-	    foreach ($feed->entries as $entry) {
146
-		    $this->_writer->startObjectScope();
147
-		    $this->writeEntry($entry);
148
-		    $this->_writer->endScope();
149
-	    }
145
+        foreach ($feed->entries as $entry) {
146
+            $this->_writer->startObjectScope();
147
+            $this->writeEntry($entry);
148
+            $this->_writer->endScope();
149
+        }
150 150
 
151 151
 
152
-	    return $this;
152
+        return $this;
153 153
     }
154 154
 
155 155
 
@@ -163,15 +163,15 @@  discard block
 block discarded – undo
163 163
     protected function writeEntry(ODataEntry $entry)
164 164
     {
165 165
 
166
-	    $this->writeEntryMetadata($entry);
167
-	    foreach ($entry->links as $link) {
168
-		    $this->writeLink($link);
169
-	    }
166
+        $this->writeEntryMetadata($entry);
167
+        foreach ($entry->links as $link) {
168
+            $this->writeLink($link);
169
+        }
170 170
 
171
-	    $this->writeProperties($entry->propertyContent);
171
+        $this->writeProperties($entry->propertyContent);
172 172
 
173 173
 
174
-	    return $this;
174
+        return $this;
175 175
     }
176 176
 
177 177
     /**
@@ -187,28 +187,28 @@  discard block
 block discarded – undo
187 187
         if ($entry->id != null || $entry->type != null || $entry->eTag != null) {
188 188
             // "__metadata"
189 189
             $this->_writer
190
-	            ->writeName(ODataConstants::JSON_METADATA_STRING)
190
+                ->writeName(ODataConstants::JSON_METADATA_STRING)
191 191
                 ->startObjectScope();
192 192
 
193 193
             // Write uri value only for entity types
194 194
             if ($entry->id != null) {
195 195
                 $this->_writer
196
-	                ->writeName($this->urlKey)
196
+                    ->writeName($this->urlKey)
197 197
                     ->writeValue($entry->id);
198 198
             }
199 199
 
200 200
             // Write the etag property, if the entry has etag properties.
201 201
             if ($entry->eTag != null) {
202 202
                 $this->_writer
203
-	                ->writeName(ODataConstants::JSON_ETAG_STRING)
203
+                    ->writeName(ODataConstants::JSON_ETAG_STRING)
204 204
                     ->writeValue($entry->eTag);
205 205
             }
206 206
 
207 207
             // Write the type property, if the entry has type properties.
208 208
             if ($entry->type != null) {
209 209
                 $this->_writer
210
-	                ->writeName(ODataConstants::JSON_TYPE_STRING)
211
-	                ->writeValue($entry->type);
210
+                    ->writeName(ODataConstants::JSON_TYPE_STRING)
211
+                    ->writeValue($entry->type);
212 212
             }
213 213
         }
214 214
 
@@ -216,19 +216,19 @@  discard block
 block discarded – undo
216 216
         if ($entry->isMediaLinkEntry) {
217 217
             if ($entry->mediaLink != null) {
218 218
                 $this->_writer
219
-	                ->writeName(ODataConstants::JSON_EDITMEDIA_STRING)
220
-	                ->writeValue($entry->mediaLink->editLink)
219
+                    ->writeName(ODataConstants::JSON_EDITMEDIA_STRING)
220
+                    ->writeValue($entry->mediaLink->editLink)
221 221
 
222
-	                ->writeName(ODataConstants::JSON_MEDIASRC_STRING)
223
-	                ->writeValue($entry->mediaLink->srcLink)
222
+                    ->writeName(ODataConstants::JSON_MEDIASRC_STRING)
223
+                    ->writeValue($entry->mediaLink->srcLink)
224 224
 
225
-	                ->writeName(ODataConstants::JSON_CONTENTTYPE_STRING)
226
-	                ->writeValue($entry->mediaLink->contentType);
225
+                    ->writeName(ODataConstants::JSON_CONTENTTYPE_STRING)
226
+                    ->writeValue($entry->mediaLink->contentType);
227 227
 
228 228
                 if ($entry->mediaLink->eTag != null) {
229 229
                     $this->_writer
230
-	                    ->writeName(ODataConstants::JSON_MEDIAETAG_STRING)
231
-	                    ->writeValue($entry->mediaLink->eTag);
230
+                        ->writeName(ODataConstants::JSON_MEDIAETAG_STRING)
231
+                        ->writeValue($entry->mediaLink->eTag);
232 232
                 }
233 233
 
234 234
                 $this->_writer->endScope();
@@ -237,19 +237,19 @@  discard block
 block discarded – undo
237 237
             // writing named resource streams
238 238
             foreach ($entry->mediaLinks as $mediaLink) {
239 239
                 $this->_writer
240
-	                ->writeName($mediaLink->name)
241
-	                ->startObjectScope()
240
+                    ->writeName($mediaLink->name)
241
+                    ->startObjectScope()
242 242
 
243
-	                ->writeName(ODataConstants::JSON_MEDIASRC_STRING)
244
-	                ->writeValue($mediaLink->srcLink)
243
+                    ->writeName(ODataConstants::JSON_MEDIASRC_STRING)
244
+                    ->writeValue($mediaLink->srcLink)
245 245
 
246
-	                ->writeName(ODataConstants::JSON_CONTENTTYPE_STRING)
247
-	                ->writeValue($mediaLink->contentType);
246
+                    ->writeName(ODataConstants::JSON_CONTENTTYPE_STRING)
247
+                    ->writeValue($mediaLink->contentType);
248 248
 
249 249
                 if ($mediaLink->eTag != null) {
250 250
                     $this->_writer
251
-	                    ->writeName(ODataConstants::JSON_MEDIAETAG_STRING)
252
-	                    ->writeValue($mediaLink->eTag);
251
+                        ->writeName(ODataConstants::JSON_MEDIAETAG_STRING)
252
+                        ->writeValue($mediaLink->eTag);
253 253
                 }
254 254
 
255 255
                 $this->_writer->endScope();
@@ -258,92 +258,92 @@  discard block
 block discarded – undo
258 258
             $this->_writer->endScope();
259 259
         }
260 260
 
261
-	    return $this;
261
+        return $this;
262 262
     }
263 263
 
264 264
 
265
-	/**
266
-	 * @param ODataLink $link Link to write.
267
-	 *
268
-	 * @return JsonODataV1Writer
269
-	 */
270
-	protected function writeLink(ODataLink $link)
265
+    /**
266
+     * @param ODataLink $link Link to write.
267
+     *
268
+     * @return JsonODataV1Writer
269
+     */
270
+    protected function writeLink(ODataLink $link)
271 271
     {
272 272
 
273 273
         // "<linkname>" :
274 274
         $this->_writer->writeName($link->title);
275 275
 
276
-	    if ($link->isExpanded) {
277
-		    if(is_null($link->expandedResult)){
278
-				$this->_writer->writeValue("null");
279
-		    }
280
-		    else{
281
-			    $this->writeExpandedLink($link);
282
-		    }
283
-	    } else {
284
-		    $this->_writer
285
-			    ->startObjectScope()
286
-			    ->writeName(ODataConstants::JSON_DEFERRED_STRING)
287
-			    ->startObjectScope()
288
-			    ->writeName($this->urlKey)
289
-			    ->writeValue($link->url)
290
-			    ->endScope()
291
-			    ->endScope()
292
-		    ;
293
-	    }
294
-
295
-	    return $this;
276
+        if ($link->isExpanded) {
277
+            if(is_null($link->expandedResult)){
278
+                $this->_writer->writeValue("null");
279
+            }
280
+            else{
281
+                $this->writeExpandedLink($link);
282
+            }
283
+        } else {
284
+            $this->_writer
285
+                ->startObjectScope()
286
+                ->writeName(ODataConstants::JSON_DEFERRED_STRING)
287
+                ->startObjectScope()
288
+                ->writeName($this->urlKey)
289
+                ->writeValue($link->url)
290
+                ->endScope()
291
+                ->endScope()
292
+            ;
293
+        }
294
+
295
+        return $this;
296
+    }
297
+
298
+    protected function writeExpandedLink(ODataLink $link)
299
+    {
300
+        if ($link->isCollection) {
301
+            $this->_writer->startArrayScope();
302
+            $this->writeFeed($link->expandedResult);
303
+        } else {
304
+            $this->_writer->startObjectScope();
305
+            $this->writeEntry($link->expandedResult);
306
+        }
307
+
308
+        $this->_writer->endScope();
296 309
     }
297 310
 
298
-	protected function writeExpandedLink(ODataLink $link)
299
-	{
300
-		if ($link->isCollection) {
301
-			$this->_writer->startArrayScope();
302
-			$this->writeFeed($link->expandedResult);
303
-		} else {
304
-			$this->_writer->startObjectScope();
305
-			$this->writeEntry($link->expandedResult);
306
-		}
307
-
308
-		$this->_writer->endScope();
309
-	}
310
-
311
-
312
-	/**
313
-	 * Write the given collection of properties.
314
-	 * (properties of an entity or complex type)
315
-	 *
316
-	 * @param ODataPropertyContent $properties Collection of properties.
317
-	 *
318
-	 * @return JsonODataV1Writer
319
-	 */
320
-	protected function writeProperties(ODataPropertyContent $properties)
321
-	{
322
-		foreach ($properties->properties as $property) {
323
-
324
-			$this->writePropertyMeta($property);
325
-			$this->_writer->writeName($property->name);
326
-
327
-			if ($property->value == null) {
328
-				$this->_writer->writeValue("null");
329
-			} elseif ($property->value instanceof ODataPropertyContent) {
330
-				$this->writeComplexProperty($property);
331
-			} elseif ($property->value instanceof ODataBagContent) {
332
-				$this->writeBagContent($property->value);
333
-			} else {
334
-				$this->_writer->writeValue($property->value, $property->typeName);
335
-			}
336
-
337
-		}
338
-
339
-		return $this;
340
-	}
341
-
342
-
343
-	protected function writePropertyMeta(ODataProperty $property)
344
-	{
345
-		return $this; //does nothing in v1 or v2, json light outputs stuff
346
-	}
311
+
312
+    /**
313
+     * Write the given collection of properties.
314
+     * (properties of an entity or complex type)
315
+     *
316
+     * @param ODataPropertyContent $properties Collection of properties.
317
+     *
318
+     * @return JsonODataV1Writer
319
+     */
320
+    protected function writeProperties(ODataPropertyContent $properties)
321
+    {
322
+        foreach ($properties->properties as $property) {
323
+
324
+            $this->writePropertyMeta($property);
325
+            $this->_writer->writeName($property->name);
326
+
327
+            if ($property->value == null) {
328
+                $this->_writer->writeValue("null");
329
+            } elseif ($property->value instanceof ODataPropertyContent) {
330
+                $this->writeComplexProperty($property);
331
+            } elseif ($property->value instanceof ODataBagContent) {
332
+                $this->writeBagContent($property->value);
333
+            } else {
334
+                $this->_writer->writeValue($property->value, $property->typeName);
335
+            }
336
+
337
+        }
338
+
339
+        return $this;
340
+    }
341
+
342
+
343
+    protected function writePropertyMeta(ODataProperty $property)
344
+    {
345
+        return $this; //does nothing in v1 or v2, json light outputs stuff
346
+    }
347 347
 
348 348
     /**
349 349
      * Begin write complex property.
@@ -356,21 +356,21 @@  discard block
 block discarded – undo
356 356
     {
357 357
 
358 358
         $this->_writer
359
-	        // {
360
-	        ->startObjectScope()
359
+            // {
360
+            ->startObjectScope()
361 361
 
362
-	        // __metadata : { Type : "typename" }
363
-	        ->writeName(ODataConstants::JSON_METADATA_STRING)
364
-	        ->startObjectScope()
365
-	        ->writeName(ODataConstants::JSON_TYPE_STRING)
366
-	        ->writeValue($property->typeName)
367
-	        ->endScope();
362
+            // __metadata : { Type : "typename" }
363
+            ->writeName(ODataConstants::JSON_METADATA_STRING)
364
+            ->startObjectScope()
365
+            ->writeName(ODataConstants::JSON_TYPE_STRING)
366
+            ->writeValue($property->typeName)
367
+            ->endScope();
368 368
 
369
-	    $this->writeProperties($property->value);
369
+        $this->writeProperties($property->value);
370 370
 
371
-		$this->_writer->endScope();
371
+        $this->_writer->endScope();
372 372
 
373
-	    return $this;
373
+        return $this;
374 374
     }
375 375
 
376 376
 
@@ -385,15 +385,15 @@  discard block
 block discarded – undo
385 385
     {
386 386
 
387 387
         $this->_writer
388
-	        ->startObjectScope() // {
389
-	        ->writeName(ODataConstants::JSON_METADATA_STRING) //__metadata : { Type : "typename" }
388
+            ->startObjectScope() // {
389
+            ->writeName(ODataConstants::JSON_METADATA_STRING) //__metadata : { Type : "typename" }
390 390
             ->startObjectScope()
391 391
 
392 392
             ->writeName(ODataConstants::JSON_TYPE_STRING)
393 393
             ->writeValue($bag->type)
394 394
             ->endScope()  // }
395
-	        ->writeName(ODataConstants::JSON_RESULT_NAME) // "__results":
396
-	    	->startArrayScope(); // [
395
+            ->writeName(ODataConstants::JSON_RESULT_NAME) // "__results":
396
+            ->startArrayScope(); // [
397 397
 
398 398
         foreach ($bag->propertyContents as $content) {
399 399
             if ($content instanceof ODataPropertyContent) {
@@ -410,9 +410,9 @@  discard block
 block discarded – undo
410 410
 
411 411
 
412 412
         $this->_writer
413
-	        ->endScope()  // ]
414
-	        ->endScope(); // }
415
-	    return $this;
413
+            ->endScope()  // ]
414
+            ->endScope(); // }
415
+        return $this;
416 416
     }
417 417
 
418 418
 
@@ -433,29 +433,29 @@  discard block
 block discarded – undo
433 433
         $writer = new JsonWriter('');
434 434
         // Wrapper for error.
435 435
         $writer
436
-	        ->startObjectScope()
436
+            ->startObjectScope()
437 437
             ->writeName(ODataConstants::JSON_ERROR) // "error"
438 438
             ->startObjectScope();
439 439
 
440 440
         // "code"
441 441
         if ($exception->getCode() != null) {
442 442
             $writer
443
-	            ->writeName(ODataConstants::JSON_ERROR_CODE)
444
-	            ->writeValue($exception->getCode());
443
+                ->writeName(ODataConstants::JSON_ERROR_CODE)
444
+                ->writeValue($exception->getCode());
445 445
         }
446 446
 
447 447
         // "message"
448 448
         $writer
449
-	        ->writeName(ODataConstants::JSON_ERROR_MESSAGE)
450
-	        ->startObjectScope()
451
-	        ->writeName(ODataConstants::XML_LANG_ATTRIBUTE_NAME) // "lang"
452
-	        ->writeValue('en-US')
449
+            ->writeName(ODataConstants::JSON_ERROR_MESSAGE)
450
+            ->startObjectScope()
451
+            ->writeName(ODataConstants::XML_LANG_ATTRIBUTE_NAME) // "lang"
452
+            ->writeValue('en-US')
453 453
             ->writeName(ODataConstants::JSON_ERROR_VALUE)
454
-	        ->writeValue($exception->getMessage())
454
+            ->writeValue($exception->getMessage())
455 455
 
456
-	        ->endScope()
457
-	        ->endScope()
458
-	        ->endScope();
456
+            ->endScope()
457
+            ->endScope()
458
+            ->endScope();
459 459
 
460 460
         return $writer->getJsonOutput();
461 461
     }
Please login to merge, or discard this patch.
src/POData/Writers/Json/JsonODataV2Writer.php 1 patch
Indentation   +132 added lines, -132 removed lines patch added patch discarded remove patch
@@ -25,87 +25,87 @@  discard block
 block discarded – undo
25 25
  */
26 26
 class JsonODataV2Writer extends JsonODataV1Writer
27 27
 {
28
-	//The key difference between 1 and 2 is that in 2 collection results
29
-	//are wrapped in a "result" array.  this is to allow a place for collection metadata to be placed
30
-	//
31
-	//IE {d : [ item1, item2, item3] }
32
-	//is now { d : { results :[item1, item2, item3], meta1 : x, meta2 : y }
33
-	//So we override the collection methods to shove this stuff in there
28
+    //The key difference between 1 and 2 is that in 2 collection results
29
+    //are wrapped in a "result" array.  this is to allow a place for collection metadata to be placed
30
+    //
31
+    //IE {d : [ item1, item2, item3] }
32
+    //is now { d : { results :[item1, item2, item3], meta1 : x, meta2 : y }
33
+    //So we override the collection methods to shove this stuff in there
34 34
 
35
-	protected $dataArrayName = ODataConstants::JSON_RESULT_NAME;
35
+    protected $dataArrayName = ODataConstants::JSON_RESULT_NAME;
36 36
 
37
-	protected $rowCountName = ODataConstants::JSON_ROWCOUNT_STRING;
37
+    protected $rowCountName = ODataConstants::JSON_ROWCOUNT_STRING;
38 38
 
39
-	protected $nextLinkName = ODataConstants::JSON_NEXT_STRING;
39
+    protected $nextLinkName = ODataConstants::JSON_NEXT_STRING;
40 40
 
41 41
 
42
-	/**
43
-	 * Determines if the given writer is capable of writing the response or not
44
-	 * @param Version $responseVersion the OData version of the response
45
-	 * @param string $contentType the Content Type of the response
46
-	 * @return boolean true if the writer can handle the response, false otherwise
47
-	 */
48
-	public function canHandle(Version $responseVersion, $contentType)
49
-	{
50
-		$parts = explode(";", $contentType);
51
-
52
-		//special case, in v3 verbose is the v2 writer
53
-		if($responseVersion == Version::v3()){
54
-			return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && in_array('odata=verbose', $parts);
55
-		}
56
-
57
-		if($responseVersion != Version::v2()){
58
-			return false;
59
-		}
60
-
61
-		return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts);
62
-
63
-	}
64
-
65
-
66
-	/**
67
-	 * Write the given OData model in a specific response format
68
-	 *
69
-	 * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
70
-	 *
71
-	 * @return JsonODataV1Writer
72
-	 */
73
-	public function write($model){
74
-		// { "d" :
75
-		$this->_writer
76
-			->startObjectScope()
77
-			->writeName("d")
78
-			->startObjectScope();
79
-
42
+    /**
43
+     * Determines if the given writer is capable of writing the response or not
44
+     * @param Version $responseVersion the OData version of the response
45
+     * @param string $contentType the Content Type of the response
46
+     * @return boolean true if the writer can handle the response, false otherwise
47
+     */
48
+    public function canHandle(Version $responseVersion, $contentType)
49
+    {
50
+        $parts = explode(";", $contentType);
80 51
 
81
-		if ($model instanceof ODataURL) {
52
+        //special case, in v3 verbose is the v2 writer
53
+        if($responseVersion == Version::v3()){
54
+            return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts) && in_array('odata=verbose', $parts);
55
+        }
82 56
 
83
-			$this->writeURL($model);
84
-		} elseif ($model instanceof ODataURLCollection) {
85
-			$this->writeURLCollection($model);
86
-		} elseif ($model instanceof ODataPropertyContent) {
87
-			$this->writeProperties($model);
88
-		} elseif ($model instanceof ODataFeed) {
89
-			// Json Format V2:
90
-			// "results":
91
-			$this->writeRowCount($model->rowCount);
92
-			$this->writeNextPageLink($model->nextPageLink);
93
-			$this->_writer
94
-				->writeName($this->dataArrayName)
95
-				->startArrayScope();
96
-			$this->writeFeed($model);
97
-			$this->_writer->endScope();
57
+        if($responseVersion != Version::v2()){
58
+            return false;
59
+        }
98 60
 
99
-		}elseif ($model instanceof ODataEntry) {
100
-			$this->writeEntry($model);
101
-		}
61
+        return in_array(MimeTypes::MIME_APPLICATION_JSON, $parts);
102 62
 
63
+    }
103 64
 
104
-		$this->_writer->endScope();
105
-		$this->_writer->endScope();
106 65
 
107
-		return $this;
108
-	}
66
+    /**
67
+     * Write the given OData model in a specific response format
68
+     *
69
+     * @param  ODataURL|ODataURLCollection|ODataPropertyContent|ODataFeed|ODataEntry $model Object of requested content.
70
+     *
71
+     * @return JsonODataV1Writer
72
+     */
73
+    public function write($model){
74
+        // { "d" :
75
+        $this->_writer
76
+            ->startObjectScope()
77
+            ->writeName("d")
78
+            ->startObjectScope();
79
+
80
+
81
+        if ($model instanceof ODataURL) {
82
+
83
+            $this->writeURL($model);
84
+        } elseif ($model instanceof ODataURLCollection) {
85
+            $this->writeURLCollection($model);
86
+        } elseif ($model instanceof ODataPropertyContent) {
87
+            $this->writeProperties($model);
88
+        } elseif ($model instanceof ODataFeed) {
89
+            // Json Format V2:
90
+            // "results":
91
+            $this->writeRowCount($model->rowCount);
92
+            $this->writeNextPageLink($model->nextPageLink);
93
+            $this->_writer
94
+                ->writeName($this->dataArrayName)
95
+                ->startArrayScope();
96
+            $this->writeFeed($model);
97
+            $this->_writer->endScope();
98
+
99
+        }elseif ($model instanceof ODataEntry) {
100
+            $this->writeEntry($model);
101
+        }
102
+
103
+
104
+        $this->_writer->endScope();
105
+        $this->_writer->endScope();
106
+
107
+        return $this;
108
+    }
109 109
 
110 110
 
111 111
     /** 
@@ -119,76 +119,76 @@  discard block
 block discarded – undo
119 119
     {
120 120
 
121 121
         $this->writeRowCount($urls->count);
122
-	    $this->writeNextPageLink($urls->nextPageLink);
122
+        $this->writeNextPageLink($urls->nextPageLink);
123 123
 
124 124
         // Json Format V2:
125 125
         // "results":
126
-	    $this->_writer
127
-	        ->writeName($this->dataArrayName)
128
-		    ->startArrayScope();
126
+        $this->_writer
127
+            ->writeName($this->dataArrayName)
128
+            ->startArrayScope();
129 129
 
130
-	   	parent::writeUrlCollection($urls);
130
+            parent::writeUrlCollection($urls);
131 131
 
132
-	    $this->_writer->endScope();
132
+        $this->_writer->endScope();
133 133
 
134
-	    return $this;
134
+        return $this;
135 135
     }
136 136
   
137 137
 
138
-	/**
139
-	 * Writes the row count.
140
-	 *
141
-	 * @param int $count Row count value.
142
-	 *
143
-	 * @return JsonODataV2Writer
144
-	 */
145
-	protected function writeRowCount($count)
146
-	{
147
-		if ($count != null) {
148
-			$this->_writer->writeName($this->rowCountName);
149
-			$this->_writer->writeValue($count);
150
-		}
151
-
152
-		return $this;
153
-	}
154
-
155
-
156
-	/**
157
-	 * Writes the next page link.
158
-	 *
159
-	 * @param ODataLink $nextPageLinkUri Uri for next page link.
160
-	 *
161
-	 * @return JsonODataV2Writer
162
-	 */
163
-	protected function writeNextPageLink(ODataLink $nextPageLinkUri = null)
164
-	{
165
-		// "__next" : uri
166
-		if ($nextPageLinkUri != null) {
167
-			$this->_writer
168
-				->writeName($this->nextLinkName)
169
-				->writeValue($nextPageLinkUri->url);
170
-		}
171
-
172
-		return $this;
173
-	}
174
-
175
-	protected function writeExpandedLink(ODataLink $link)
176
-	{
177
-		//Difference from v1 is that expanded collection have a result: wrapper to allow for metadata to exist
178
-		$this->_writer->startObjectScope();
179
-
180
-		if ($link->isCollection) {
181
-			$this->_writer
182
-				->writeName($this->dataArrayName)
183
-				->startArrayScope();
184
-			$this->writeFeed($link->expandedResult);
185
-			$this->_writer->endScope();
186
-		} else {
187
-			$this->writeEntry($link->expandedResult);
188
-		}
189
-
190
-
191
-		$this->_writer->endScope();
192
-	}
138
+    /**
139
+     * Writes the row count.
140
+     *
141
+     * @param int $count Row count value.
142
+     *
143
+     * @return JsonODataV2Writer
144
+     */
145
+    protected function writeRowCount($count)
146
+    {
147
+        if ($count != null) {
148
+            $this->_writer->writeName($this->rowCountName);
149
+            $this->_writer->writeValue($count);
150
+        }
151
+
152
+        return $this;
153
+    }
154
+
155
+
156
+    /**
157
+     * Writes the next page link.
158
+     *
159
+     * @param ODataLink $nextPageLinkUri Uri for next page link.
160
+     *
161
+     * @return JsonODataV2Writer
162
+     */
163
+    protected function writeNextPageLink(ODataLink $nextPageLinkUri = null)
164
+    {
165
+        // "__next" : uri
166
+        if ($nextPageLinkUri != null) {
167
+            $this->_writer
168
+                ->writeName($this->nextLinkName)
169
+                ->writeValue($nextPageLinkUri->url);
170
+        }
171
+
172
+        return $this;
173
+    }
174
+
175
+    protected function writeExpandedLink(ODataLink $link)
176
+    {
177
+        //Difference from v1 is that expanded collection have a result: wrapper to allow for metadata to exist
178
+        $this->_writer->startObjectScope();
179
+
180
+        if ($link->isCollection) {
181
+            $this->_writer
182
+                ->writeName($this->dataArrayName)
183
+                ->startArrayScope();
184
+            $this->writeFeed($link->expandedResult);
185
+            $this->_writer->endScope();
186
+        } else {
187
+            $this->writeEntry($link->expandedResult);
188
+        }
189
+
190
+
191
+        $this->_writer->endScope();
192
+    }
193 193
 
194 194
 }
195 195
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/Json/JsonWriter.php 1 patch
Indentation   +64 added lines, -64 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@  discard block
 block discarded – undo
20 20
     private $_jsonDateTimeFormat = "\/Date(%s)\/";
21 21
 
22 22
 
23
-	/**
23
+    /**
24 24
      * Writer to write text into
25 25
      *
26 26
      */
@@ -57,8 +57,8 @@  discard block
 block discarded – undo
57 57
     public function endScope()
58 58
     {
59 59
         $this->_writer
60
-	        ->writeLine()
61
-	        ->decreaseIndent();
60
+            ->writeLine()
61
+            ->decreaseIndent();
62 62
  
63 63
         if (array_pop($this->_scopes)->type == $this->_scopeType['Array']) {
64 64
             $this->_writer->writeValue("]");
@@ -66,7 +66,7 @@  discard block
 block discarded – undo
66 66
             $this->_writer->writeValue("}");
67 67
         }
68 68
 
69
-	    return $this;
69
+        return $this;
70 70
 
71 71
     }
72 72
 
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
     public function startArrayScope()
79 79
     {
80 80
         $this->_startScope($this->_scopeType['Array']);
81
-	    return $this;
81
+        return $this;
82 82
     }
83 83
 
84 84
 
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
     public function writeDataArrayName()
91 91
     {
92 92
         $this->writeName($this->dataArrayName);
93
-	    return $this;
93
+        return $this;
94 94
     }
95 95
 
96 96
     /**
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     public function startObjectScope()
102 102
     {
103 103
         $this->_startScope($this->_scopeType['Object']);
104
-	    return $this;
104
+        return $this;
105 105
     }
106 106
 
107 107
     /**
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
         $this->_writeCore($name, true /*quotes*/);
126 126
         $this->_writer->writeTrimmed(": ");
127 127
 
128
-	    return $this;
128
+        return $this;
129 129
     }
130 130
 
131 131
     /**
@@ -139,62 +139,62 @@  discard block
 block discarded – undo
139 139
     public function writeValue($value, $type = null)
140 140
     {
141 141
         switch ($type) {
142
-	        case 'Edm.Boolean':
143
-	        case 'Edm.Int16':
144
-	        case 'Edm.Int32':
145
-	        case 'Edm.Byte':
146
-	        case 'Edm.SByte':
147
-	            $this->_writeCore($value, /* quotes */ false);
148
-	            break;
149
-
150
-
151
-	        case 'Edm.Int64':
152
-	        case 'Edm.Guid':
153
-	        case 'Edm.Decimal':
154
-	        case 'Edm.Binary':
155
-	            $this->_writeCore($value, /* quotes */ true);
156
-	            break;
157
-
158
-	        case 'Edm.Single':
159
-	        case 'Edm.Double':
160
-	            if (is_infinite($value) || is_nan($value)) {
161
-	                $this->_writeCore("null", /* quotes */ true);
162
-	            } else {
163
-	                $this->_writeCore($value, /* quotes */ false);
164
-	            }
165
-
166
-	            break;
167
-
168
-
169
-	        case 'Edm.DateTime':
170
-	            $dateTime = new \DateTime($value, new \DateTimeZone('UTC'));
171
-		        $formattedDateTime = $dateTime->format('Y-m-d\TH:i:s');
172
-	            $this->_writeCore($formattedDateTime, /* quotes */ true);
173
-	            break;
174
-
175
-
176
-	        case 'Edm.String':
177
-	            if ($value == null) {
178
-	                $this->_writeCore("null", /* quotes */ false);
179
-	            } else {
180
-	                $jsonEncoded = json_encode($value);
181
-	                //json_encode always escapes a solidus (forward slash, %x2F),
182
-	                //this will be a problem when encoding urls
183
-	                //JSON_UNESCAPED_SLASHES not available in earlier versions of php 5.3
184
-	                //So removing escaping forward slashes manually
185
-	                $jsonEncoded = str_replace('\\/', '/', $jsonEncoded);
186
-	                //since json_encode is already appending chords
187
-	                //there is no need to set it again
188
-	                $this->_writeCore($jsonEncoded, /* quotes */ false);
189
-	            }
190
-	            break;
191
-
192
-
193
-	        default:
194
-	            $this->_writeCore($this->_quoteJScriptString($value), /* quotes */ true);
142
+            case 'Edm.Boolean':
143
+            case 'Edm.Int16':
144
+            case 'Edm.Int32':
145
+            case 'Edm.Byte':
146
+            case 'Edm.SByte':
147
+                $this->_writeCore($value, /* quotes */ false);
148
+                break;
149
+
150
+
151
+            case 'Edm.Int64':
152
+            case 'Edm.Guid':
153
+            case 'Edm.Decimal':
154
+            case 'Edm.Binary':
155
+                $this->_writeCore($value, /* quotes */ true);
156
+                break;
157
+
158
+            case 'Edm.Single':
159
+            case 'Edm.Double':
160
+                if (is_infinite($value) || is_nan($value)) {
161
+                    $this->_writeCore("null", /* quotes */ true);
162
+                } else {
163
+                    $this->_writeCore($value, /* quotes */ false);
164
+                }
165
+
166
+                break;
167
+
168
+
169
+            case 'Edm.DateTime':
170
+                $dateTime = new \DateTime($value, new \DateTimeZone('UTC'));
171
+                $formattedDateTime = $dateTime->format('Y-m-d\TH:i:s');
172
+                $this->_writeCore($formattedDateTime, /* quotes */ true);
173
+                break;
174
+
175
+
176
+            case 'Edm.String':
177
+                if ($value == null) {
178
+                    $this->_writeCore("null", /* quotes */ false);
179
+                } else {
180
+                    $jsonEncoded = json_encode($value);
181
+                    //json_encode always escapes a solidus (forward slash, %x2F),
182
+                    //this will be a problem when encoding urls
183
+                    //JSON_UNESCAPED_SLASHES not available in earlier versions of php 5.3
184
+                    //So removing escaping forward slashes manually
185
+                    $jsonEncoded = str_replace('\\/', '/', $jsonEncoded);
186
+                    //since json_encode is already appending chords
187
+                    //there is no need to set it again
188
+                    $this->_writeCore($jsonEncoded, /* quotes */ false);
189
+                }
190
+                break;
191
+
192
+
193
+            default:
194
+                $this->_writeCore($this->_quoteJScriptString($value), /* quotes */ true);
195 195
         }
196 196
 
197
-	    return $this;
197
+        return $this;
198 198
     }
199 199
 
200 200
     /**
@@ -278,7 +278,7 @@  discard block
 block discarded – undo
278 278
         }
279 279
 
280 280
         $this->_writer
281
-	        ->increaseIndent()
281
+            ->increaseIndent()
282 282
             ->writeLine();
283 283
     }
284 284
 
@@ -321,7 +321,7 @@  discard block
 block discarded – undo
321 321
     public function __construct($type)
322 322
     {
323 323
         $this->type = $type;
324
-	    $this->objectCount = 0;
324
+        $this->objectCount = 0;
325 325
     }
326 326
 
327 327
 }
328 328
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/Metadata/MetadataWriter.php 1 patch
Indentation   +16 added lines, -17 removed lines patch added patch discarded remove patch
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
      */
88 88
     public function writeMetadata()
89 89
     {   
90
-       $this->_metadataManager = MetadataManager::create($this->providersWrapper);
90
+        $this->_metadataManager = MetadataManager::create($this->providersWrapper);
91 91
 
92 92
         $this->_dataServiceVersion = new Version(1, 0);
93 93
         $edmSchemaVersion = $this->providersWrapper->getEdmSchemaVersion();
@@ -184,7 +184,6 @@  discard block
 block discarded – undo
184 184
      * Write all resource types (entity and complex types)
185 185
      * 
186 186
      * @param ResourceType[] $resourceTypes resource types array
187
-
188 187
      * @param array $associationTypesInResourceTypesNamespace collection of 
189 188
      * association types for the given resource types
190 189
      * array(string, AssociationType)
@@ -286,10 +285,10 @@  discard block
 block discarded – undo
286 285
             } else if ($resourceProperty->isKindOf(ResourcePropertyKind::RESOURCE_REFERENCE) 
287 286
                 || $resourceProperty->isKindOf(ResourcePropertyKind::RESOURCESET_REFERENCE)
288 287
             ) {
289
-                 $this->_writeNavigationProperty($resourceType, $associationTypesInResourceTypeNamespace, $resourceProperty);
288
+                    $this->_writeNavigationProperty($resourceType, $associationTypesInResourceTypeNamespace, $resourceProperty);
290 289
             } else {
291
-                 //Unexpected ResourceProperty, expected 
292
-                 //Bag/Primitive/Complex/Navigation Property   
290
+                    //Unexpected ResourceProperty, expected 
291
+                    //Bag/Primitive/Complex/Navigation Property   
293 292
             }            
294 293
         }
295 294
     }
@@ -526,23 +525,23 @@  discard block
 block discarded – undo
526 525
     private function _getSchemaNamespaceUri($edmSchemaVersion)
527 526
     {
528 527
         switch ($edmSchemaVersion) {
529
-	        case EdmSchemaVersion::VERSION_1_DOT_0:
530
-	            return ODataConstants::CSDL_VERSION_1_0;
528
+            case EdmSchemaVersion::VERSION_1_DOT_0:
529
+                return ODataConstants::CSDL_VERSION_1_0;
531 530
 
532
-	        case EdmSchemaVersion::VERSION_1_DOT_1:
533
-	            return ODataConstants::CSDL_VERSION_1_1;
531
+            case EdmSchemaVersion::VERSION_1_DOT_1:
532
+                return ODataConstants::CSDL_VERSION_1_1;
534 533
 
535
-	        case EdmSchemaVersion::VERSION_1_DOT_2:
536
-	            return ODataConstants::CSDL_VERSION_1_2;
534
+            case EdmSchemaVersion::VERSION_1_DOT_2:
535
+                return ODataConstants::CSDL_VERSION_1_2;
537 536
 
538
-	        case EdmSchemaVersion::VERSION_2_DOT_0:
539
-	            return ODataConstants::CSDL_VERSION_2_0;
537
+            case EdmSchemaVersion::VERSION_2_DOT_0:
538
+                return ODataConstants::CSDL_VERSION_2_0;
540 539
 
541
-	        case EdmSchemaVersion::VERSION_2_DOT_2:
542
-	            return ODataConstants::CSDL_VERSION_2_2;
540
+            case EdmSchemaVersion::VERSION_2_DOT_2:
541
+                return ODataConstants::CSDL_VERSION_2_2;
543 542
 
544
-	        default:
545
-	            return ODataConstants::CSDL_VERSION_2_2;
543
+            default:
544
+                return ODataConstants::CSDL_VERSION_2_2;
546 545
         }
547 546
     }
548 547
 }
549 548
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/ODataWriterRegistry.php 1 patch
Indentation   +29 added lines, -29 removed lines patch added patch discarded remove patch
@@ -12,35 +12,35 @@
 block discarded – undo
12 12
 class ODataWriterRegistry
13 13
 {
14 14
 
15
-	/** @var IODataWriter[]  */
16
-	private $writers = array();
17
-
18
-
19
-	public function register(IODataWriter $writer)
20
-	{
21
-		$this->writers[] = $writer;
22
-	}
23
-
24
-	public function reset()
25
-	{
26
-		$this->writers = array();
27
-	}
28
-
29
-	/**
30
-	 * @param Version $responseVersion
31
-	 * @param $contentType
32
-	 *
33
-	 * @return IODataWriter|null the writer that can handle the give criteria, or null
34
-	 */
35
-	public function getWriter(Version $responseVersion, $contentType){
36
-
37
-		foreach($this->writers as $writer)
38
-		{
39
-			if($writer->canHandle($responseVersion, $contentType)) return $writer;
40
-		}
41
-
42
-		return null;
43
-	}
15
+    /** @var IODataWriter[]  */
16
+    private $writers = array();
17
+
18
+
19
+    public function register(IODataWriter $writer)
20
+    {
21
+        $this->writers[] = $writer;
22
+    }
23
+
24
+    public function reset()
25
+    {
26
+        $this->writers = array();
27
+    }
28
+
29
+    /**
30
+     * @param Version $responseVersion
31
+     * @param $contentType
32
+     *
33
+     * @return IODataWriter|null the writer that can handle the give criteria, or null
34
+     */
35
+    public function getWriter(Version $responseVersion, $contentType){
36
+
37
+        foreach($this->writers as $writer)
38
+        {
39
+            if($writer->canHandle($responseVersion, $contentType)) return $writer;
40
+        }
41
+
42
+        return null;
43
+    }
44 44
 
45 45
 
46 46
 }
47 47
\ No newline at end of file
Please login to merge, or discard this patch.
src/POData/Writers/ResponseWriter.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -29,14 +29,14 @@  discard block
 block discarded – undo
29 29
      * 
30 30
      */
31 31
     public static function write(
32
-	    IService $service,
32
+        IService $service,
33 33
         RequestDescription $request,
34 34
         $entityModel,
35 35
         $responseContentType
36 36
     ) {
37 37
         $responseBody = null;
38 38
         $dataServiceVersion = $request->getResponseVersion();
39
-	    $targetKind = $request->getTargetKind();
39
+        $targetKind = $request->getTargetKind();
40 40
 
41 41
         if ($targetKind == TargetKind::METADATA()) {
42 42
             // /$metadata
@@ -44,16 +44,16 @@  discard block
 block discarded – undo
44 44
             $responseBody = $writer->writeMetadata();            
45 45
             $dataServiceVersion = $writer->getDataServiceVersion();
46 46
         } else if ($targetKind == TargetKind::PRIMITIVE_VALUE() && $responseContentType != MimeTypes::MIME_APPLICATION_OCTETSTREAM) {
47
-	        //This second part is to exclude binary properties
47
+            //This second part is to exclude binary properties
48 48
             // /Customer('ALFKI')/CompanyName/$value
49 49
             // /Customers/$count
50 50
             $responseBody = utf8_encode($request->getTargetResult());
51 51
         } else if ($responseContentType == MimeTypes::MIME_APPLICATION_OCTETSTREAM || $targetKind == TargetKind::MEDIA_RESOURCE()) {
52 52
             // Binary property or media resource
53 53
             if ($request->getTargetKind() == TargetKind::MEDIA_RESOURCE()) {
54
-	            $result = $request->getTargetResult();
55
-	            $streamInfo =  $request->getResourceStreamInfo();
56
-	            $provider = $service->getStreamProviderWrapper();
54
+                $result = $request->getTargetResult();
55
+                $streamInfo =  $request->getResourceStreamInfo();
56
+                $provider = $service->getStreamProviderWrapper();
57 57
                 $eTag = $provider->getStreamETag( $result, $streamInfo );
58 58
                 $service->getHost()->setResponseETag($eTag);
59 59
                 $responseBody = $provider->getReadStream( $result, $streamInfo );
@@ -66,8 +66,8 @@  discard block
 block discarded – undo
66 66
             }
67 67
         } else {
68 68
             $writer = $service->getODataWriterRegistry()->getWriter($request->getResponseVersion(), $responseContentType);
69
-	        //TODO: move ot Messages
70
-	        if(is_null($writer)) throw new \Exception("no writer can handle the request");
69
+            //TODO: move ot Messages
70
+            if(is_null($writer)) throw new \Exception("no writer can handle the request");
71 71
 
72 72
             if (is_null($entityModel)) {  //TODO: this seems like a weird way to know that the request is for a service document..i'd think we know this some other way
73 73
                 $responseBody = $writer->writeServiceDocument($service->getProvidersWrapper())->getOutput();
Please login to merge, or discard this patch.