-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedbackFactory.php
More file actions
103 lines (87 loc) · 2.13 KB
/
Copy pathFeedbackFactory.php
File metadata and controls
103 lines (87 loc) · 2.13 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Main plugin class.
*
* @package wpRigel\Pollify
* @since 1.0.0
*/
declare(strict_types=1);
namespace wpRigel\Pollify;
use WP_Error;
use wpRigel\Pollify\Model\Poll;
/**
* Class FeedbackFactory.
*
* @package wpRigel\Pollify
*/
class FeedbackFactory {
/**
* Feedback object.
*
* @var object
*/
private $feedback;
/**
* Feedbacks class map.
*
* @var array
*/
protected static $class_map = [
'poll' => Poll::class, // Default free feature.
];
/**
* Cconstructor. where we pass the feedback arrat or object
* and depending on the type we create the object.
*
* @param array|object $feedback Feedback array or object.
*
* @throws WP_Error If feedback is not valid.
*
* @return void
*/
public function __construct( $feedback ) {
if ( is_array( $feedback ) ) {
$feedback = (object) $feedback;
}
if ( ! is_object( $feedback ) ) {
throw new WP_Error( 'invalid-feedback', esc_html__( 'Invalid feedback.', 'poll-creator' ), [ 'status' => 400 ] );
}
$this->feedback = $feedback;
}
/**
* Get feedback object.
*
* @return object
*/
public function get(): object {
// Check if feedback type is set or not.
if ( empty( $this->feedback->type ) ) {
return new WP_Error( 'invalid-feedback-type', __( 'Invalid feedback type.', 'poll-creator' ), [ 'status' => 400 ] );
}
/**
* Filter the feedback classes map.
*
* @param array $class_map Feedback classes map.
* @param object $feedback Feedback object.
*
* @return array
*/
self::$class_map = apply_filters( 'pollify_map_feedback_classes', self::$class_map, $this->feedback );
// Check if feedback type is valid or not.
if ( ! array_key_exists( $this->feedback->type, self::$class_map ) ) {
return new WP_Error( 'invalid-feedback-type', __( 'Invalid feedback type.', 'poll-creator' ), [ 'status' => 400 ] );
}
// Get feedback class.
$class = self::$class_map[ $this->feedback->type ];
// Create feedback object.
return new $class( (array) $this->feedback );
}
/**
* Get class map.
*
* @return array
*/
public static function get_class_map(): array {
return self::$class_map;
}
}