// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.ModelBinding
{
///
/// Result of an operation.
///
///
///
/// can represent a single submitted value or multiple submitted values.
///
///
/// Use to consume only a single value, regardless of whether a single value or
/// multiple values were submitted.
///
///
/// Treat as an to consume all values,
/// regardless of whether a single value or multiple values were submitted.
///
///
public struct ValueProviderResult : IEquatable, IEnumerable
{
private static readonly CultureInfo _invariantCulture = CultureInfo.InvariantCulture;
///
/// A that represents a lack of data.
///
public static ValueProviderResult None = new ValueProviderResult(new string[0]);
///
/// Creates a new using .
///
/// The submitted values.
public ValueProviderResult(StringValues values)
: this(values, _invariantCulture)
{
}
///
/// Creates a new .
///
/// The submitted values.
/// The associated with this value.
public ValueProviderResult(StringValues values, CultureInfo culture)
{
Values = values;
Culture = culture ?? _invariantCulture;
}
///
/// Gets or sets the associated with the values.
///
public CultureInfo Culture { get; private set; }
///
/// Gets or sets the values.
///
public StringValues Values { get; private set; }
///
/// Gets the first value based on the order values were provided in the request. Use
/// to get a single value for processing regardless of whether a single or multiple values were provided
/// in the request.
///
public string FirstValue
{
get
{
if (Values.Count == 0)
{
return null;
}
return Values[0];
}
}
///
/// Gets the number of submitted values.
///
public int Length
{
get
{
return Values.Count;
}
}
///
public override bool Equals(object obj)
{
var other = obj as ValueProviderResult?;
return other.HasValue && Equals(other.Value);
}
///
public bool Equals(ValueProviderResult other)
{
if (Length != other.Length)
{
return false;
}
else
{
var x = Values;
var y = other.Values;
for (var i = 0; i < x.Count; i++)
{
if (!string.Equals(x[i], y[i], StringComparison.Ordinal))
{
return false;
}
}
return true;
}
}
///
public override int GetHashCode()
{
return ToString().GetHashCode();
}
///
public override string ToString()
{
return Values.ToString();
}
///
/// Gets an for this .
///
/// An .
public IEnumerator GetEnumerator()
{
return ((IEnumerable)Values).GetEnumerator();
}
///
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Converts the provided into a comma-separated string containing all
/// submitted values.
///
/// The .
public static explicit operator string(ValueProviderResult result)
{
return result.Values;
}
///
/// Converts the provided into a an array of containing
/// all submitted values.
///
/// The .
public static explicit operator string[](ValueProviderResult result)
{
return result.Values;
}
///
/// Compares two objects for equality.
///
/// A .
/// A .
/// true if the values are equal, otherwise false.
public static bool operator ==(ValueProviderResult x, ValueProviderResult y)
{
return x.Equals(y);
}
///
/// Compares two objects for inequality.
///
/// A .
/// A .
/// false if the values are equal, otherwise true.
public static bool operator !=(ValueProviderResult x, ValueProviderResult y)
{
return !x.Equals(y);
}
}
}