Skip to content

Commit 3cd885d

Browse files
committed
Lesson: 09_autoloading_through_spl
1 parent 7697931 commit 3cd885d

File tree

4 files changed

+270
-0
lines changed

4 files changed

+270
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Create a hash
5+
* @param string $string
6+
* @return string
7+
*/
8+
function getHash($string){
9+
return hash('sha256', $string);
10+
}
11+
12+
/**
13+
* Autoloader, registered by spl_autoload_register()
14+
* @param string $className
15+
*/
16+
function autoload ($className) {
17+
18+
$file = dirname(__FILE__) . '/' . $className . '.php';
19+
if (file_exists($file)) {
20+
require $file;
21+
}
22+
}
23+
24+
spl_autoload_register('autoload');
+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
class User {
3+
4+
/**
5+
* @var string
6+
*/
7+
private $email;
8+
9+
/**
10+
* @var string
11+
*/
12+
private $password;
13+
14+
/**
15+
* All properties that can be set directly
16+
* @var array
17+
*/
18+
private $fillable = array('email', 'password');
19+
20+
/**
21+
* All properties that can be gotten directly
22+
* @var array
23+
*/
24+
private $accessible = array('email', 'password');
25+
26+
/**
27+
* Class configuration only
28+
* @param array $params
29+
*/
30+
public function __construct(Array $params = array()) {
31+
32+
if (count($params)) {
33+
foreach ($params as $key => $value) {
34+
$this->$key = $value;
35+
}
36+
}
37+
}
38+
39+
/**
40+
* Directly set inaccessible but existing properties, if in $this->fillable array
41+
* @param string $name
42+
* @param mixed $value
43+
* @return void
44+
*/
45+
public function __set ($name, $value) {
46+
47+
// Do not set if not fillable
48+
if (! in_array($name, $this->fillable)) {
49+
return false;
50+
}
51+
52+
if (isset($this->$name)) {
53+
$this->$name = $value;
54+
}
55+
}
56+
57+
/**
58+
* Directly get inaccessible but existing properties, if in $this->accesible array
59+
* @param string $name
60+
* @return mixed
61+
*/
62+
public function __get ($name) {
63+
64+
// Do not return if not accessible
65+
if (! in_array($name, $this->accessible)) {
66+
return NULL;
67+
}
68+
69+
return isset($this->$name) ? $this->$name : NULL;
70+
}
71+
72+
/**
73+
* Return accessible properties as a json object
74+
* @return string
75+
*/
76+
public function __toString () {
77+
78+
$data = array();
79+
80+
// Only add property accessible
81+
foreach ($this->accessible as $key) {
82+
$data[$key] = $this->$key;
83+
}
84+
85+
return json_encode($data);
86+
}
87+
88+
/**
89+
* Log in a user
90+
* @return string
91+
*/
92+
public function login(){
93+
return 'Logging in with a vengeange ...';
94+
}
95+
96+
/**
97+
* Log in a user
98+
* @return string
99+
*/
100+
public function logout(){
101+
return 'Logging out ...';
102+
}
103+
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
class Validator
4+
{
5+
/**
6+
* Validation errors
7+
* @var array
8+
*/
9+
private $errors = array();
10+
11+
/**
12+
* Validate data against a set of rules and set errors in the $this->errors
13+
* array
14+
* @param array $data
15+
* @param array $rules
16+
* @return boolean
17+
*/
18+
public function validate (Array $data, Array $rules)
19+
{
20+
$valid = true;
21+
22+
foreach ($rules as $item => $ruleset) {
23+
// required|email|min:8
24+
$ruleset = explode('|', $ruleset);
25+
26+
foreach ($ruleset as $rule) {
27+
28+
$pos = strpos($rule, ':');
29+
if ($pos !== false) {
30+
$parameter = substr($rule, $pos + 1);
31+
$rule = substr($rule, 0, $pos);
32+
}
33+
else {
34+
$parameter = '';
35+
}
36+
37+
// validateEmail($item, $value, $param)
38+
$methodName = 'validate' . ucfirst($rule);
39+
$value = isset($data[$item]) ? $data[$item] : NULL;
40+
if (method_exists($this, $methodName)) {
41+
$this->$methodName($item, $value, $parameter) OR $valid = false;
42+
}
43+
}
44+
}
45+
46+
47+
return $valid;
48+
}
49+
50+
/**
51+
* Get validation errors
52+
* @return array:
53+
*/
54+
public function getErrors ()
55+
{
56+
return $this->errors;
57+
}
58+
59+
/**
60+
* Validate the $value of $item to see if it is present and not empty
61+
* @param string $item
62+
* @param string $value
63+
* @param string $parameter
64+
* @return boolean
65+
*/
66+
private function validateRequired ($item, $value, $parameter)
67+
{
68+
if ($value === '' || $value === NULL) {
69+
$this->errors[$item][] = 'The ' . $item . ' field is required';
70+
return false;
71+
}
72+
73+
return true;
74+
}
75+
76+
/**
77+
* Validate the $value of $item to see if it is a valid email address
78+
* @param string $item
79+
* @param string $value
80+
* @param string $parameter
81+
* @return boolean
82+
*/
83+
private function validateEmail ($item, $value, $parameter)
84+
{
85+
if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
86+
$this->errors[$item][] = 'The ' . $item . ' field should be a valid email addres';
87+
return false;
88+
}
89+
90+
return true;
91+
}
92+
93+
/**
94+
* Validate the $value of $item to see if it is fo at least $param
95+
* characters long
96+
* @param string $item
97+
* @param string $value
98+
* @param string $parameter
99+
* @return boolean
100+
*/
101+
private function validateMin ($item, $value, $parameter)
102+
{
103+
if (strlen($value) >= $parameter == false) {
104+
$this->errors[$item][] = 'The ' . $item . ' field should have a minimum length of ' . $parameter;
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
}

09_autoloading_through_spl/index.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
// Require app files
4+
require 'app/Helper.php';
5+
6+
// Set data and validation rules
7+
$rules = array('email' => 'required|email', 'password' => 'required|min:8');
8+
$data = array('email' => 'joost@tutsplus.com', 'password' => '12346789', 'foo' => 'bar');
9+
10+
// Run validation
11+
$validator = new Validator();
12+
if ($validator->validate($data, $rules) == true) {
13+
14+
// Validation passed. Set user values.
15+
$joost = new User($data);
16+
17+
$joost->email = 'someotheremail@tutsplus.com';
18+
$joost->password = 'sadfsadfsad';
19+
20+
// var_dump($joost->email);
21+
// var_dump($joost->password);
22+
23+
// Dump user
24+
// var_dump($joost);
25+
26+
echo $joost;
27+
}
28+
else {
29+
30+
// Validation failed. Dump validation errors.
31+
var_dump($validator->getErrors());
32+
}

0 commit comments

Comments
 (0)