Skip to content

Commit 5b4a373

Browse files
committed
Update core
1 parent d4b079e commit 5b4a373

File tree

9 files changed

+46
-31
lines changed

9 files changed

+46
-31
lines changed

Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
interface Builder {
3-
public function setName($name);
4-
public function setAge($age);
5-
public function setLevel($level);
6-
public function setLocation($location);
7-
public function setTodo($role);
8-
public function build();
3+
public function setName($name) : Builder;
4+
public function setAge($age) : Builder;
5+
public function setLevel($level) : Builder;
6+
public function setLocation($location) : Builder;
7+
public function setTodo($role) : Builder;
8+
public function build() : array;
99
}

Framework.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22
abstract class Framework {
33
public $data = array();
4-
public function set($key = '',$data = ''){
4+
public function set($key = '',$data = '') : void{
55
if($key === '') {
66
throw new Exception(PATTERN_NAME . " not found key data");
77
}
88
$this->data[$key] = $data;
99
}
10-
public function get(){
10+
public function get() : string | array | int{
1111
return $this->data;
1212
}
1313
}

Parts/Age.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22
class UserAge {
3-
public static function main($age = 0){
3+
public static function main(int $age = 0) : int | \Exception{
44
if($age > PHP_USER_PATTERN_MAX_VALUE_AGE) {
55
throw new Exception(PATTERN_NAME . " : Age : maximum " . PHP_USER_PATTERN_MAX_VALUE_AGE . " characters ");
66
}
7-
return intval($age);
7+
return $age;
88
}
99
}

Parts/Level.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
class UserLevel {
3-
public static function main($level = ''){
3+
public static function main(string $level = '') : string | \Exception{
44
if(strlen($level) > PHP_USER_PATTERN_MAX_LENGTH_LEVEL) {
55
throw new Exception(PATTERN_NAME . " : Level : maximum " . PHP_USER_PATTERN_MAX_LENGTH_LEVEL . " characters ");
66
}

Parts/Location.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
class UserLocation {
3-
public static function main($location = ''){
3+
public static function main(string $location = '') : string | \Exception{
44
if(strlen($location) > PHP_USER_PATTERN_MAX_LENGTH_LOCATION) {
55
throw new Exception(PATTERN_NAME . " : localtion : maximum " . PHP_USER_PATTERN_MAX_LENGTH_LOCATION . " characters ");
66
}

Parts/Name.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
class UserName {
3-
public static function main($name = ''){
3+
public static function main($name = '') : string | \Exception {
44
/**
55
* Name empty
66
*/

Parts/Todo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
class UserTodo {
3-
public static function main($role = 'admin'){
3+
public static function main($role = 'admin') : string | \Exception{
44
$json = $role === 'admin'
55
? file_get_contents(PHP_USER_PATTERN_JSON_PATH_ADMIN_ROLE)
66
: file_get_contents(PHP_USER_PATTERN_JSON_PATH_USER_ROLE);

UserBuilder.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,34 @@
1010
class UserBuilder implements Builder {
1111
private $model;
1212
public $data;
13-
public function __construct(){
14-
$this->model = new UserModel;
13+
public function __construct($model = null){
14+
if($model === null) {
15+
$this->model = new UserModel;
16+
} else{
17+
$this->model = $model;
18+
}
1519
}
16-
public function setName($name){
20+
public function setName($name) : Builder {
1721
$this->model->set('name',UserName::main($name));
22+
return new UserBuilder($this->model);
1823
}
19-
public function setAge($age){
24+
public function setAge($age) : Builder{
2025
$this->model->set('age',UserAge::main($age));
26+
return new UserBuilder($this->model);
2127
}
22-
public function setLevel($level){
28+
public function setLevel($level) : Builder{
2329
$this->model->set('level',UserLevel::main($level));
30+
return new UserBuilder($this->model);
2431
}
25-
public function setLocation($location){
32+
public function setLocation($location) : Builder{
2633
$this->model->set('location',UserLocation::main($location));
34+
return new UserBuilder($this->model);
2735
}
28-
public function setTodo($role = 'member'){
36+
public function setTodo($role = 'member') : Builder{
2937
$this->model->set('todo',UserTodo::main($role));
38+
return new UserBuilder($this->model);
3039
}
31-
public function build(){
32-
$this->data = $this->model->data;
40+
public function build() : array{
41+
return $this->model->data;
3342
}
3443
}

index.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
<?php
22
try {
33
require_once dirname(__FILE__) . '/UserBuilder.php';
4-
$builder = new UserBuilder;
5-
$builder->setName('Hoang Lee');
6-
$builder->setAge(29);
7-
$builder->setLocation('HCM, Viet Nam');
8-
$builder->setLevel('Technical Leader');
9-
$builder->setTodo('admin');
10-
$builder->build();
11-
print_r($builder->data);
4+
$admin = $user = new UserBuilder;
5+
$admin->setName('Hoang Lee')
6+
->setAge(29)
7+
->setLocation('HCM, Viet Nam')
8+
->setLevel('Technical Leader')
9+
->setTodo('admin');
10+
print_r($admin->build());
11+
print_r('<br/>-------- first build ------- <br/>');
12+
$user->setName('Hoang Lee 2')
13+
->setAge(30)
14+
->setLocation('HCM, Viet Nam')
15+
->setLevel('Technical Leader 2')
16+
->setTodo('member');
17+
print_r($admin->build());
1218
}catch(Exception $e) {
1319
print_r($e);
1420
}

0 commit comments

Comments
 (0)