项目作者: spekkionu

项目描述 :
Trait that automatically calls getters and setters for property access.
高级语言: PHP
项目地址: git://github.com/spekkionu/property-access.git
创建时间: 2015-04-02T01:15:10Z
项目社区:https://github.com/spekkionu/property-access

开源协议:MIT License

下载


Property Access Trait

Build Status
Code Coverage
Scrutinizer Code Quality
SensioLabsInsight

Trait that automatically calls getters and setters for property access.

  1. use Spekkionu\PropertyAccess\PropertyAccessTrait;
  2. class ExampleClass
  3. {
  4. use PropertyAccessTrait;
  5. private $name;
  6. private $email;
  7. }
  1. $example = new ExampleClass();
  2. $example->name = 'Bob';
  3. $example->email = 'bob@example.com';
  4. echo $example->name; // Bob
  5. $example->fill(array(
  6. 'name' => 'Steve',
  7. 'email' => 'steve@example.com'
  8. ));
  9. echo $example->email; // steve@example.com

Getters and Setters will be called

You can even use Value Objects

  1. use Spekkionu\PropertyAccess\PropertyAccessTrait;
  2. class ExampleClass
  3. {
  4. use PropertyAccessTrait;
  5. private $name;
  6. private $email;
  7. public function setEmail(EmailAddress $email){
  8. $this->email = $email;
  9. }
  10. }
  11. // Value Object
  12. class EmailAddress
  13. {
  14. private $email;
  15. public function __construct($email)
  16. {
  17. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  18. throw new InvalidArgumentException('Not a valid email address.');
  19. }
  20. $this->email = $email;
  21. }
  22. public function getValue()
  23. {
  24. return $this->email;
  25. }
  26. public function __toString()
  27. {
  28. return $this->getValue();
  29. }
  30. }
  31. // Usage
  32. $example = new ExampleClass();
  33. $example->email = new EmailAddress('bob@example.com');