Completed
Branch 2.0.0 (814c19)
by Jimmy
03:05
created

Helper_Class   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 0
1
<?php
2
/**
3
 * Classe helper pour les modèles.
4
 *
5
 * @author Eoxia <[email protected]>
6
 * @since 1.0.0
7
 * @version 1.0.0
8
 * @copyright 2015-2018
9
 * @package EO_Framework\EO_Model\Class
10
 */
11
12
namespace eoxia;
13
14
if ( ! defined( 'ABSPATH' ) ) { exit; }
15
16
if ( ! class_exists( '\eoxia\Helper_Class' ) ) {
17
18
	/**
19
	 * Classe helper pour les modèles.
20
	 */
21
	class Helper_Class implements \ArrayAccess {
22
23
		/**
24
		 * Récupères le modèle.
25
		 *
26
		 * @since 1.0.0
27
		 * @version 1.0.0
28
		 *
29
		 * @return Object le modèle.
30
		 */
31
		public function get_model() {
32
			return $this->schema;
33
		}
34
35
		/**
36
		 * Récupères le nom de la classe selon le model.
37
		 *
38
		 * @since 1.0.0
39
		 * @version 1.0.0
40
		 *
41
		 * @return string Le nom de la classe avec le namespace si existant.
42
		 */
43
		 public function get_class() {
44
			 $class_name = get_class( $this );
45
			 $class_name = str_replace( 'Model', 'Class', $class_name );
46
			 $class_name = str_replace( 'model', 'Class', $class_name );
47
			 $class_name = str_replace( '\\', '/', $class_name );
48
49
			 return $class_name;
50
		 }
51
52
		/**
53
		 * Permet de faire echo sur un objet et supprimes la définition du modèle avant l'affichage.
54
		 *
55
		 * @return string void
56
		 */
57
		public function __toString() {
58
			$this->delete_model_for_print( $this );
59
			echo '<pre>'; print_r( $this ); echo '</pre>';
60
			return '';
61
		}
62
63
		/**
64
		 * Supprime le modèle.
65
		 *
66
		 * @since 1.0.0
67
		 * @version 1.0.0
68
		 *
69
		 * @param  object $current L'objet complet.
70
		 */
71
		private function delete_model_for_print( $current ) {
72
			if ( ! empty( $this->model ) ) {
73
				unset( $this->model );
74
			}
75
76
			foreach ( $current as &$content ) {
77
				if ( is_array( $content ) ) {
78
					foreach ( $content as &$model ) {
79
						if ( ! empty( $model->model ) ) {
80
							unset( $model->model );
81
							$this->delete_model_for_print( $model );
82
						}
83
					}
84
				}
85
			}
86
		}
87
88
		/**
89
		 * Checks if a parameter is set.
90
		 *
91
		 * @since 1.0.0
92
		 *
93
		 * @param string $offset Parameter name.
94
		 * @return bool Whether the parameter is set.
95
		 */
96
		public function offsetExists( $offset ) {
97
			return isset( $this->$offset );
98
		}
99
100
		/**
101
		 * Retrieves a parameter from the request.
102
		 *
103
		 * @since 1.0.0
104
		 *
105
		 * @param string $offset Parameter name.
106
		 * @return mixed|null Value if set, null otherwise.
107
		 */
108
		public function offsetGet( $offset ) {
109
			return isset( $this->$offset ) ? $this->$offset : null;
110
		}
111
112
		/**
113
		 * Sets a parameter on the request.
114
		 *
115
		 * @since 1.0.0
116
		 *
117
		 * @param string $offset Parameter name.
118
		 * @param mixed  $value  Parameter value.
119
		 */
120
		public function offsetSet( $offset, $value ) {
121
			$this->$offset = $value;
122
		}
123
124
		/**
125
		 * Removes a parameter from the request.
126
		 *
127
		 * @since 1.0.0
128
		 *
129
		 * @param string $offset Parameter name.
130
		 */
131
		public function offsetUnset( $offset ) {
132
			unset( $this->$offset );
133
		}
134
135
	}
136
137
} // End if().
138