summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OpenId/OpenId/Extensions/SimpleRegistration/Gender.cs
blob: 05cbfbbb261abb922e568efcdc1bee3c40efd37a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//-----------------------------------------------------------------------
// <copyright file="Gender.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OpenId.Extensions.SimpleRegistration {
	using System;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Messaging.Reflection;

	/// <summary>
	/// Indicates the gender of a user.
	/// </summary>
	public enum Gender {
		/// <summary>
		/// The user is male.
		/// </summary>
		Male,

		/// <summary>
		/// The user is female.
		/// </summary>
		Female,
	}

	/// <summary>
	/// Encodes/decodes the Simple Registration Gender type to its string representation.
	/// </summary>
	internal class GenderEncoder : IMessagePartEncoder {
		#region IMessagePartEncoder Members

		/// <summary>
		/// Encodes the specified value.
		/// </summary>
		/// <param name="value">The value.  Guaranteed to never be null.</param>
		/// <returns>
		/// The <paramref name="value"/> in string form, ready for message transport.
		/// </returns>
		public string Encode(object value) {
			var gender = (Gender?)value;
			if (gender.HasValue) {
				switch (gender.Value) {
					case Gender.Male: return Constants.Genders.Male;
					case Gender.Female: return Constants.Genders.Female;
				}
			}

			return null;
		}

		/// <summary>
		/// Decodes the specified value.
		/// </summary>
		/// <param name="value">The string value carried by the transport.  Guaranteed to never be null, although it may be empty.</param>
		/// <returns>
		/// The deserialized form of the given string.
		/// </returns>
		/// <exception cref="FormatException">Thrown when the string value given cannot be decoded into the required object type.</exception>
		public object Decode(string value) {
			switch (value) {
				case Constants.Genders.Male: return SimpleRegistration.Gender.Male;
				case Constants.Genders.Female: return SimpleRegistration.Gender.Female;
				default: throw new FormatException();
			}
		}

		#endregion
	}
}