PluginProbe
Tutor LMS – eLearning and online course solution / 3.9.2
Tutor LMS – eLearning and online course solution v3.9.2
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / BaseController.php

BaseController.php in Tutor LMS – eLearning and online course solution 3.9.2, at classes/BaseController.php

73 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base Controller
4 *
5 * @package Tutor\BaseController
6 * @author Themeum <support@themeum.com>
7 * @link https://themeum.com
8 * @since 3.0.0
9 */
10
11 namespace TUTOR;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Base Controller
19 */
20 abstract class BaseController {
21
22 /**
23 * Get model object
24 *
25 * @since 3.0.0
26 * @return object
27 */
28 abstract public function get_model();
29
30 /**
31 * Get allowed field and optionally set empty values
32 * for the required fields
33 *
34 * @since 3.0.0
35 *
36 * @param array $params Request params.
37 * @param boolean $set_required To set required fields empty values.
38 *
39 * @return array
40 */
41 public function get_allowed_fields( array $params, $set_required = true ) {
42 $fillable = $this->get_model()->get_fillable_fields();
43 $required_fields = $this->get_model()->get_required_fields();
44
45 $allowed_params = array_intersect_key( $params, array_flip( $fillable ) );
46
47 // Set empty value if set_required is true.
48 if ( $set_required ) {
49 $this->setup_required_fields( $allowed_params, $required_fields );
50 }
51
52 return $allowed_params;
53 }
54
55 /**
56 * Setup required fields on the request params
57 *
58 * @since 3.0.0
59 *
60 * @param array $params Reference request params.
61 * @param array $required_fields Required fields.
62 *
63 * @return void
64 */
65 public function setup_required_fields( array &$params, $required_fields ) {
66 foreach ( $required_fields as $field ) {
67 if ( ! isset( $params[ $field ] ) ) {
68 $params[ $field ] = '';
69 }
70 }
71 }
72 }
73