@ 2015-11-03T07:43:51Z <?php
class ValueObject implements JsonSerializable {
private $value;
public function __construct($value) {
$this->value = $value;
}
public function jsonSerialize() {
return $this->value;
}
public function equals(ValueObject $value) {
return $this->value === $value;
}
}
class Age extends ValueObject {
public function __construct(int $value) {
if ($value > 150) {
throw new Exception('Age must be less than 150.');
}
parent::__construct($value);
}
}
class Email extends ValueObject {
public function __construct(string $value) {
$email = filter_var($value, FILTER_VALIDATE_EMAIL);
if (false === $email) {
throw new Exception('Email is invalid.');
}
parent::__construct($email);
}
}
function Emails(Email ...$emails) {
class Emails {
private $values;
public function __construct($values) {
$this->values = $values;
}
}
}
function Email(string $email) {
return new Email($email);
}
function Age(int $age) {
return new Age($age);
}
// class User implements JsonSerializable {
// private $email;
// private $age;
// public function __construct(Email $email) {
// $this->email = $email;
// }
// public function setAge(Age $age) { $this->age = $age; }
// public function jsonSerialize() {
// return ['email' => $this->email, 'age' => $this->age];
// }
// public function __destruct()
// {
// // persist
// }
// }
// class UpdateUserCommand {
// private $userId;
// private $email;
// private $age;
// private $location;
// public function __construct(GUID $userId, Email $email, Age $age = null, Point $location = null) {
// $this->userId = $userId;
// $this->email = $email;
// $this->age = $age;
// $this->location = $location;
// }
// public function getUserId() { return $this->userId; }
// public function getEmail() { return $this->email; }
// public function getAge() { return $this->age; }
// public function getLocation() { return $this->location; }
// }
// class UserService {
// private $users;
// public function __construct(Users $users) {
// $this->users = $users;
// }
// public function update(UpdateUserCommand $command) {
// $user = $this->users[$command->getUserId()];
// $user->setEmail($command->getEmail());
// $user->setAge($command->getAge());
// $user->setPoint($command->getPoint());
// }
// }
$emails = Emails(Email('opexus@gmail.com'), Email('irma@wiki.ru'));
$age = Age(23);
// $point = Point(Lattitude(-20), Longitude(170));
// $lattitude = new Lattitude(-20);
// $longitude = new Longitude(170);
// $point = new Point($lattitude, $longitude);
// $user = new User($email);
// $user->setAge($age);
echo json_encode(compact('emails', 'age'));
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for git.master , git.master_jit , rfc.property-hooks Deprecated: Return type of ValueObject::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8Al8a on line 7
{"emails":null,"age":23} This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
47.3 ms | 402 KiB | 8 Q