Skip to content

Commit 21759bc

Browse files
committed
Lesson 12: Class inheritance and protected scope
1 parent c5472da commit 21759bc

File tree

7 files changed

+349
-0
lines changed

7 files changed

+349
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
/**
6+
* This class extends Acme\App\User
7+
*/
8+
class Administrator extends User {
9+
10+
public function __construct() {
11+
$this->isAdmin = true;
12+
}
13+
14+
/**
15+
* Return the result of the login()
16+
*/
17+
public function getLogin ()
18+
{
19+
// The login method in the parent class is accessible from here,
20+
// because it is protected.
21+
return $this->login();
22+
}
23+
24+
/**
25+
* This method is for Administrators only
26+
*/
27+
public function reportForDuty ()
28+
{
29+
// Do stuff ...
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
class User {
6+
7+
/**
8+
* @var string
9+
*/
10+
protected $email;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $password;
16+
17+
/**
18+
* @var boolean
19+
*/
20+
protected $isAdmin = false;
21+
22+
/**
23+
* All properties that can be set directly
24+
* @var array
25+
*/
26+
protected $fillable = array('email', 'password');
27+
28+
/**
29+
* All properties that can be gotten directly
30+
* @var array
31+
*/
32+
protected $accessible = array('email', 'password');
33+
34+
/**
35+
* Class configuration only
36+
* @param array $params
37+
*/
38+
public function __construct(Array $params = array()) {
39+
40+
if (count($params)) {
41+
foreach ($params as $key => $value) {
42+
$this->$key = $value;
43+
}
44+
}
45+
}
46+
47+
/**
48+
* Directly set inaccessible but existing properties, if in $this->fillable array
49+
* @param string $name
50+
* @param mixed $value
51+
* @return void
52+
*/
53+
public function __set ($name, $value) {
54+
55+
// Do not set if not fillable
56+
if (! in_array($name, $this->fillable)) {
57+
return false;
58+
}
59+
60+
if (isset($this->$name)) {
61+
$this->$name = $value;
62+
}
63+
}
64+
65+
/**
66+
* Directly get inaccessible but existing properties, if in $this->accesible array
67+
* @param string $name
68+
* @return mixed
69+
*/
70+
public function __get ($name) {
71+
72+
// Do not return if not accessible
73+
if (! in_array($name, $this->accessible)) {
74+
return NULL;
75+
}
76+
77+
return isset($this->$name) ? $this->$name : NULL;
78+
}
79+
80+
/**
81+
* Return accessible properties as a json object
82+
* @return string
83+
*/
84+
public function __toString () {
85+
86+
$data = array();
87+
88+
// Only add property accessible
89+
foreach ($this->accessible as $key) {
90+
$data[$key] = $this->$key;
91+
}
92+
93+
return json_encode($data);
94+
}
95+
96+
/**
97+
* Log in a user
98+
* @return string
99+
*/
100+
public function login(){
101+
return 'Logging in with a vengeange ...';
102+
}
103+
104+
/**
105+
* Log in a user
106+
* @return string
107+
*/
108+
public function logout(){
109+
return 'Logging out ...';
110+
}
111+
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
class Validator
6+
{
7+
/**
8+
* Validation errors
9+
* @var array
10+
*/
11+
private $errors = array();
12+
13+
/**
14+
* Validate data against a set of rules and set errors in the $this->errors
15+
* array
16+
* @param array $data
17+
* @param array $rules
18+
* @return boolean
19+
*/
20+
public function validate (Array $data, Array $rules)
21+
{
22+
$valid = true;
23+
24+
foreach ($rules as $item => $ruleset) {
25+
// required|email|min:8
26+
$ruleset = explode('|', $ruleset);
27+
28+
foreach ($ruleset as $rule) {
29+
30+
$pos = strpos($rule, ':');
31+
if ($pos !== false) {
32+
$parameter = substr($rule, $pos + 1);
33+
$rule = substr($rule, 0, $pos);
34+
}
35+
else {
36+
$parameter = '';
37+
}
38+
39+
// validateEmail($item, $value, $param)
40+
$methodName = 'validate' . ucfirst($rule);
41+
$value = isset($data[$item]) ? $data[$item] : NULL;
42+
if (method_exists($this, $methodName)) {
43+
$this->$methodName($item, $value, $parameter) OR $valid = false;
44+
}
45+
}
46+
}
47+
48+
49+
return $valid;
50+
}
51+
52+
/**
53+
* Get validation errors
54+
* @return array:
55+
*/
56+
public function getErrors ()
57+
{
58+
return $this->errors;
59+
}
60+
61+
/**
62+
* Validate the $value of $item to see if it is present and not empty
63+
* @param string $item
64+
* @param string $value
65+
* @param string $parameter
66+
* @return boolean
67+
*/
68+
private function validateRequired ($item, $value, $parameter)
69+
{
70+
if ($value === '' || $value === NULL) {
71+
$this->errors[$item][] = 'The ' . $item . ' field is required';
72+
return false;
73+
}
74+
75+
return true;
76+
}
77+
78+
/**
79+
* Validate the $value of $item to see if it is a valid email address
80+
* @param string $item
81+
* @param string $value
82+
* @param string $parameter
83+
* @return boolean
84+
*/
85+
private function validateEmail ($item, $value, $parameter)
86+
{
87+
if (! filter_var($value, FILTER_VALIDATE_EMAIL)) {
88+
$this->errors[$item][] = 'The ' . $item . ' field should be a valid email addres';
89+
return false;
90+
}
91+
92+
return true;
93+
}
94+
95+
/**
96+
* Validate the $value of $item to see if it is fo at least $param
97+
* characters long
98+
* @param string $item
99+
* @param string $value
100+
* @param string $parameter
101+
* @return boolean
102+
*/
103+
private function validateMin ($item, $value, $parameter)
104+
{
105+
if (strlen($value) >= $parameter == false) {
106+
$this->errors[$item][] = 'The ' . $item . ' field should have a minimum length of ' . $parameter;
107+
return false;
108+
}
109+
110+
return true;
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Acme\Library;
4+
5+
class User
6+
{
7+
8+
/**
9+
* A private property or method is only accessible from the class itself
10+
*/
11+
private $foo;
12+
13+
/**
14+
* A protected property or method is accessible only from the class
15+
* itself AND from it's children classes.
16+
*/
17+
protected $bar;
18+
19+
/**
20+
* A public property or method is accessible from anywhere.
21+
*/
22+
public $baz;
23+
24+
/**
25+
* Log in a user
26+
* @return string
27+
*/
28+
protected function login(){
29+
return 'Logging in from User class ...';
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Let's require our helper file with helper functions here
5+
*/
6+
require 'Helper.php';
7+
8+
/**
9+
* PSR-0 autoloader
10+
* @param string $className
11+
*/
12+
function autoload($className)
13+
{
14+
$className = ltrim($className, '\\');
15+
$fileName = '';
16+
$namespace = '';
17+
if ($lastNsPos = strrpos($className, '\\')) {
18+
$namespace = substr($className, 0, $lastNsPos);
19+
$className = substr($className, $lastNsPos + 1);
20+
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
21+
}
22+
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
23+
24+
require $fileName;
25+
}
26+
27+
/**
28+
* Register the PSR-0 autoloader
29+
*/
30+
spl_autoload_register('autoload');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
// Require psr-0 autoloader
4+
require 'autoload.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 Acme\App\Validator();
12+
if ($validator->validate($data, $rules) == true) {
13+
14+
$joost = new Acme\App\Administrator();
15+
echo $joost->login();
16+
}
17+
else {
18+
19+
// Validation failed. Dump validation errors.
20+
var_dump($validator->getErrors());
21+
}

0 commit comments

Comments
 (0)