Completed
Pull Request — staging (#840)
by
unknown
18:30
created

InvalidField   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A from_field() 0 11 2
A invalid_id() 0 9 1
A field_required() 0 10 1
A value_invalid() 0 10 1
1
<?php
2
/**
3
 * YIKES Inc. Easy Mailchimp Forms Plugin.
4
 *
5
 * @package   YIKES\EasyForms
6
 * @author    Freddie Mixell
7
 * @license   GPL2
8
 */
9
10
namespace YIKES\EasyForms\Exception;
11
12
/**
13
 * Class InvalidField
14
 *
15
 * @since   %VERSION%
16
 * @package YIKES\EasyForms
17
 */
18
class InvalidField extends \InvalidArgumentException implements Exception {
19
20
	/**
21
	 * Create a new instance of the exception for a field class name that is
22
	 * not recognized.
23
	 *
24
	 * @since %VERSION%
25
	 *
26
	 * @param string $field Class name of the service that was not recognized.
27
	 *
28
	 * @return static
29
	 */
30
	public static function from_field( $field ) {
31
		$message = sprintf(
32
			/* translators: %s represents the class name */
33
			esc_html__( 'The field class "%s" is not recognized and cannot be used.', 'yikes-inc-easy-mailchimp-extender' ),
34
			is_object( $field )
35
				? get_class( $field )
36
				: (string) $field
37
		);
38
39
		return new static( $message );
40
	}
41
42
	/**
43
	 * Create a new instance of the exception for a field ID that is invalid.
44
	 *
45
	 * @since %VERSION%
46
	 *
47
	 * @param string $id The invalid field ID.
48
	 *
49
	 * @return InvalidField
50
	 */
51
	public static function invalid_id( $id ) {
52
		$message = sprintf(
53
			/* translators: %s represents the field ID */
54
			esc_html__( 'The ID "%s" is invalid. The ID must be a simple string, or a single depth array', 'yikes-inc-easy-mailchimp-extender' ),
55
			$id
56
		);
57
58
		return new static( $message );
59
	}
60
61
	/**
62
	 * Create a new instance of the exception when a form field is required.
63
	 *
64
	 * @since %VERSION%
65
	 *
66
	 * @param string $field   The field name.
67
	 * @param string $context Optional additional context about the field.
68
	 *
69
	 * @return static
70
	 */
71
	public static function field_required( $field, $context = '' ) {
72
		$message = sprintf(
73
			/* translators: %1$s is the field label, %2$s is the optional additional context */
74
			esc_html__( 'The field %1$s is required. %2$s', 'yikes-inc-easy-mailchimp-extender' ),
75
			$field,
76
			$context
77
		);
78
79
		return new static( $message );
80
	}
81
82
	/**
83
	 * Create a new instance of the exception when the form field value is invalid.
84
	 *
85
	 * @since %VERSION%
86
	 *
87
	 * @param string $field   The field label.
88
	 * @param string $context Optional additional context about the field.
89
	 *
90
	 * @return InvalidField
91
	 */
92
	public static function value_invalid( $field, $context = '' ) {
93
		$message = sprintf(
94
			/* translators: %1$s is the field label, %2$s is the optional additional context */
95
			esc_html__( 'The value submitted for the field %1$s is invalid. %2$s', 'yikes-inc-easy-mailchimp-extender' ),
96
			$field,
97
			$context
98
		);
99
100
		return new static( $message );
101
	}
102
}
103