Is dit OOP waardig?
Boostrap.php
Codeblok (voorbeeld.php):
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
/* Autoload the classes */
class Bootstrap {
/**
* @param string $className
*/
public static function load($className) {
if(strpos($className, jRouter_) !== FALSE) {
$className = preg_replace(/jRouter_/, , $className);
$className = preg_replace(/_/, /, $className);
if(is_readable(__DIR__ . "/{$className}/{$className}.class.php")) {
require_once(__DIR__ . "/{$className}/{$className}.class.php");
} else {
throw new Exception("The class {$className} could not be loaded!");
}
}
}
public static function register() {
spl_autoload_register(function($className) {
self::load($className);
});
}
}
Bootstrap::register();
Params.class.php
Codeblok (voorbeeld.php):
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
class jRouter_Params {
/**
* @var array $errorList
* This variable stores the errors
*/
public $errorList = array();
/**
* @var boolean $isValid
*/
public $isValid = FALSE;
public function __construct($params = array()) {
if(!empty($params)) {
foreach($params as $method => $key) {
switch(strtoupper($method)) {
case GET:
foreach($key as $param => $options) {
/* Check if the name options is set */
if(isset($options[name])) {
$paramName = $options[name];
} else {
$paramName = $param;
}
/* Check if the param is set */
if(isset($options[required]) && $options[required] === TRUE) {
if(!isset($_GET[$param])) {
$this->errorList[] = "The param {$paramName} was not founded!";
}
}
}
default:
break;
case POST:
break;
}
}
if(empty($errorList)) {
$this->isValid = TRUE;
return;
}
}
$this->isValid = FALSE;
}
/**
* @return array $errorList
*/
public function getErrors() {
return $this->errorList;
}
/**
* @param array $params
* @return boolean
*/
public function isValid() {
return $this->isValid;
}
}
DisplayErrors.class.php
Codeblok (voorbeeld.php):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class jRouter_DisplayErrors {
/**
* @var array $errors
* @return string
*/
public static function asString($errors = array()) {
$str = ;
foreach($errors as $error) {
$str .= $error .
;
}
return $str;
}
}
Enige tips zijn ook welkom!
Boostrap.php
Codeblok (voorbeeld.php):
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
/* Autoload the classes */
class Bootstrap {
/**
* @param string $className
*/
public static function load($className) {
if(strpos($className, jRouter_) !== FALSE) {
$className = preg_replace(/jRouter_/, , $className);
$className = preg_replace(/_/, /, $className);
if(is_readable(__DIR__ . "/{$className}/{$className}.class.php")) {
require_once(__DIR__ . "/{$className}/{$className}.class.php");
} else {
throw new Exception("The class {$className} could not be loaded!");
}
}
}
public static function register() {
spl_autoload_register(function($className) {
self::load($className);
});
}
}
Bootstrap::register();
Params.class.php
Codeblok (voorbeeld.php):
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
class jRouter_Params {
/**
* @var array $errorList
* This variable stores the errors
*/
public $errorList = array();
/**
* @var boolean $isValid
*/
public $isValid = FALSE;
public function __construct($params = array()) {
if(!empty($params)) {
foreach($params as $method => $key) {
switch(strtoupper($method)) {
case GET:
foreach($key as $param => $options) {
/* Check if the name options is set */
if(isset($options[name])) {
$paramName = $options[name];
} else {
$paramName = $param;
}
/* Check if the param is set */
if(isset($options[required]) && $options[required] === TRUE) {
if(!isset($_GET[$param])) {
$this->errorList[] = "The param {$paramName} was not founded!";
}
}
}
default:
break;
case POST:
break;
}
}
if(empty($errorList)) {
$this->isValid = TRUE;
return;
}
}
$this->isValid = FALSE;
}
/**
* @return array $errorList
*/
public function getErrors() {
return $this->errorList;
}
/**
* @param array $params
* @return boolean
*/
public function isValid() {
return $this->isValid;
}
}
DisplayErrors.class.php
Codeblok (voorbeeld.php):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class jRouter_DisplayErrors {
/**
* @var array $errors
* @return string
*/
public static function asString($errors = array()) {
$str = ;
foreach($errors as $error) {
$str .= $error .
;
}
return $str;
}
}
Enige tips zijn ook welkom!
03:
44: