-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\ValueObject; | ||
|
||
class Name | ||
{ | ||
public $value = ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Validation; | ||
|
||
use App\ValueObject\Name; | ||
|
||
class UserRules | ||
{ | ||
public function valid_name($name, ?string &$error = null) | ||
{ | ||
if (! $name instanceof Name) { | ||
$error = 'The name should be Name::class.'; | ||
|
||
return false; | ||
} | ||
|
||
if ($name->value === '') { | ||
$error = 'The name should not be empty.'; | ||
|
||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Config; | ||
|
||
use App\Validation\UserRules; | ||
use CodeIgniter\Config\BaseConfig; | ||
|
||
class Validation extends BaseConfig | ||
{ | ||
public array $ruleSets = [ | ||
// ... | ||
UserRules::class, | ||
]; | ||
|
||
// ... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use App\ValueObject\Name; | ||
use DomainException; | ||
|
||
class User | ||
{ | ||
public Name $name; | ||
public string $createdAt; | ||
|
||
public function __construct(array $data = []) | ||
{ | ||
if (! service('validation')->validateData($data, [ | ||
'name' => 'required|valid_name', | ||
'created_at' => 'valid_date[d/m/Y]', | ||
])) { | ||
// The validation failed | ||
throw new DomainException('Invalid data for "User"'); | ||
} | ||
|
||
// Working with allowed data | ||
$this->name = $data['name']; | ||
$this->createdAt = $data['created_at']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Entity\User; | ||
use App\ValueObject\Name; | ||
use DomainException; | ||
|
||
class UserController | ||
{ | ||
public function index(): string | ||
{ | ||
$data = [ | ||
'name' => new Name('Ivan'), | ||
'created_at' => '01/01/2000', | ||
]; | ||
|
||
try { | ||
$user = new User($data); | ||
|
||
// Hi, Ivan | ||
return 'Hi, ' . $user->name->value; | ||
} catch (DomainException $e) { | ||
echo 'Found errors: '; | ||
|
||
print_r(service('validation')->getErrors()); | ||
} | ||
} | ||
} |