Last active
August 29, 2015 14:23
-
-
Save MacDada/49c74e9555ea217662c0 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Model szkieletor: | |
* struktura danych co nie ma żadnej logiki, żadnej enkapsulacji, | |
* korzystający może z tym obiektem zrobić co tylko zechce. | |
*/ | |
class BadEntity | |
{ | |
private $username; | |
private $height; | |
public function setUsername($username) | |
{ | |
$this->username = $username; | |
} | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
public function setHeight($height) | |
{ | |
$this->height = $height; | |
} | |
public function getHeight() | |
{ | |
return $this->height; | |
} | |
} |
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
<?php | |
class ChangeUserAgeDTO | |
{ | |
public $age; | |
/** | |
* age jest polem publicznym i moglibyśmy to robić z kontrolera, | |
* ale znów… metoda pomocnicza | |
*/ | |
public function __construct(User $user) | |
{ | |
$this->age = $user->getAge(); | |
} | |
public function applyOnUser(User $user) | |
{ | |
$user->setAge($this->Age); | |
} | |
} |
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
<?php | |
/** | |
* Model solidny: | |
* posiada wewnętrzną logikę, chroni dostęp do danych, | |
* nie pozwala, żeby wykorzystać go w sposób niepoprawny. | |
*/ | |
class GoodEntity | |
{ | |
/** | |
* Wymaganie jest, żeby każdy użytkownik posiadał pseudonim i wzrost? | |
* Wymuszamy to w konstruktorze! | |
* Tym samym nie da się stworzyć nieprawidłowego obiektu | |
* (np nie da się zapomnieć ustanowić wiek użytkownika – będzie błąd) | |
*/ | |
public function __construct($username, $height) | |
{ | |
$this->setUsername($username); | |
$this->setHeight($height); | |
} | |
/** | |
* Setter dla pseudonimu jest prywatny – nie dajemy możliwości jego zmiany. | |
* Raz ustanowiony, musi taki zostać, bo takie mamy wymagania w aplikacji. | |
*/ | |
private function setUsername($username) | |
{ | |
$username = (string) $username; | |
if (empty($username)) { | |
// można też dać jakieś bardziej skomplikowane reguły, regexpy, etc. | |
throw new \InvalidArgumentException('Pseudonim nie może być pusty'); | |
} | |
$this->username = $username; | |
} | |
public function getUsername() | |
{ | |
return $this->username; | |
} | |
/** | |
* Setter dla wzrostu jest publiczny | |
* – chcemy dać możliwość zmiany wzrostu użytkownika | |
* (bo ludzie mogą rosnąć ;-)) | |
*/ | |
public function setHeight($height) | |
{ | |
$height = (int) $height; | |
if ($height <= 0) { | |
// pilnujemy, żeby do modelu nie dostała się jakaś bzdurna wartość | |
// oczywiście ten wyjątek „nigdy nie powinien wystąpić” | |
// – jeśli wystąpił to programista popełnił gdzieś błąd bo np zapomniał zrobić walidację formularza | |
throw new \InvalidArgumentException('Wzrost musi być liczbą dodatnią'); | |
} | |
if ($height > 300) { | |
throw new \InvalidArgumentException('Nie istnieją ludzie o wzroście ponad 3 metry…'); | |
} | |
$this->height = $height; | |
} | |
public function getHeight() | |
{ | |
return $this->height; | |
} | |
} |
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
<?php | |
class NewUserDTO | |
{ | |
/** | |
* Pola są publiczne – nie ma co się bawić w jakieś gettery/settery, | |
* to i tak służy jako obiekt pośredniczący między formularzami a modelem. | |
* | |
* Formularze pilnują swojej walidacji (na tym obiekcie), model pilnuje swojej. | |
* | |
* Tu walę sobie annotacje do walidacji, etc. | |
*/ | |
public $username; | |
public $age; | |
/** | |
* Metoda pomocnicza | |
*/ | |
public function createUser() | |
{ | |
return new User($this->username, $this->age); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment