Skip to content

Commit a9a6c50

Browse files
committed
Document IPhpObject and fix #23.
1 parent 65e5683 commit a9a6c50

File tree

7 files changed

+44
-6
lines changed

7 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
**Serialization:**
2424
- Added support for serializing `PhpDynamicObject` and `ExpandoObject`.
25+
- Always serialize implementations of `IPhpObject` using object notation.
26+
**This is technically a breaking change**, but it was always intended to work that way.
2527

2628
# 0.9.0:
2729

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
This Source Code Form is subject to the terms of the Mozilla Public
3+
License, v. 2.0. If a copy of the MPL was not distributed with this
4+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
**/
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
namespace PhpSerializerNET.Test {
9+
[TestClass]
10+
public class IPhpObjectSerializationTest {
11+
public class MyPhpObject : IPhpObject {
12+
public string GetClassName() => "MyPhpObject";
13+
public void SetClassName(string className) {}
14+
public string Foo {get;set;}
15+
}
16+
17+
[TestMethod]
18+
public void SerializeIPhpObject() {
19+
Assert.AreEqual( // strings:
20+
"O:11:\"MyPhpObject\":1:{s:3:\"Foo\";s:0:\"\";}",
21+
PhpSerialization.Serialize( new MyPhpObject() {Foo =""})
22+
);
23+
}
24+
}
25+
}

PhpSerializerNET.Test/SerializeObjects.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void ExplicitToClassWithMappingAttributes() {
2525
);
2626
}
2727

28+
2829
[TestMethod]
2930
public void SerializeList() {
3031
Assert.AreEqual( // strings:

PhpSerializerNET/PhpSerializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ private string SerializeComplex(object input) {
162162
default: {
163163
var inputType = input.GetType();
164164

165-
if (inputType.GetCustomAttribute<PhpClass>() != null) // TODO: add option.
166-
{
165+
if (typeof(IPhpObject).IsAssignableFrom(inputType) || inputType.GetCustomAttribute<PhpClass>() != null) {
167166
return this.SerializeToObject(input);
168167
}
169168

PhpSerializerNET/PhpTokenizer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ This Source Code Form is subject to the terms of the Mozilla Public
44
file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
**/
66

7-
using System.Linq;
87
using System.Text;
98

109
namespace PhpSerializerNET {

docs/Types/IPhpObject.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,24 @@
44

55
# Interface IPhpObject
66

7+
The interface is for manipulating the class name of an object in regards to the serialized data. I.E. the `stdClass` in `O:8:\"stdClass":0:{}`.
8+
9+
Please note that for serializatin, any types that implement IPhpObject will always be serialized using object notation.
10+
711
## Methods
812

913
### SetClassName
1014

11-
**TODO.**
15+
Set the class name of the object for serialization. Is also used internally when deserializing an object that implements this interface.
16+
17+
**Parameters:**
18+
- `string` - The class name to set.
19+
**Returns:** *void*
1220

1321
### GetClassName
1422

15-
**TODO.**
23+
Get the class name that was specified in the serialization data of the object.
24+
25+
**Parameters:** *None*.
26+
**Returns:** `string` - The class name.
27+

docs/Types/PhpDateTime.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This method has no effect, because PhpDateTime always has a fixed classname.
2929
Implementation of [`IPhpObject.GetClassName`](./IPhpObject.md#GetClassName).
3030
This method always returns "DateTime".
3131

32-
**Parameters:** None.
32+
**Parameters:** *None*.
3333

3434
**Returns:** `string` - Always "DateTime"
3535

0 commit comments

Comments
 (0)