[

PHP Properties implementations based on getters or setters method and used doc-block information.
composer require serafim/properties
How your classes with getters looks like? Maybe like this?
/**
* Class MyClass.
* @property-read int $a
* @property-read string $b
*/
class MyClass
{
/**
* Property $a must be a protected for intercepts of __get method
* @var int
*/
protected $a = 23;
/**
* ...Same
* @var int
*/
protected $b = 'string';
/**
* Getter for read-only $a and $b properties
* @param string $field
* @return int|null
*/
public function __get($field)
{
switch (true) {
case 'a': return $this->a;
case 'b': return $this->b;
}
return null;
}
}It looks ugly! ...especially when such a code is a lot.
How about to cut like that? Enjoy!
use Serafim\Properties\Properties;
/**
* Class MyClass.
* @property-read int $a
* @property-read string $b
*/
class MyClass
{
use Properties;
/**
* @var int
*/
protected $a = 23;
/**
* @var int
*/
protected $b = 'string';
}And it declaration will be works for readonly properties:
$dto = new MyClass;
$dto->a; // => 23
$dto->b; // => 'string'Try to write a new value:
$dto = new MyClass;
$dto->a = 42; // Error: "AccessDeniedException: Can not set value to read-only property MyClass::$a"All declarations (@property, @property-read and @property-write) works in IDE:
For getter or setter override just declare
get[Property] (is[Property] for booleans will be works too) or set[Property] methods.
/**
* @property-read int $a
*/
class MyClass
{
use Properties;
/**
* @var int
*/
protected $a = 23;
/**
* @return int
*/
protected function getA()
{
return $this->a + 19;
}
}
echo (new MyClass)->a; // 42 (because 23 + 19 = 42)Setter:
// ...
class Some
{
// ...
protected $anotherProperty = 'some';
/**
* @param string $newVal
*/
public function setAnotherProperty($newVal)
{
// Just example
if (mb_strlen($newVal) > 4) {
throw new InvalidArgumentException('...');
}
$this->anotherProperty = $newVal;
}
}All properties with writeable behavior will be "type checkable".
/**
* @property int|null $a
*/
class Some
{
use Properties;
protected $a;
}
//
$some = new Some;
$some->a = 23; // Ok
$some->a = null; // Ok
$some->a = 'string'; // Error: "TypeError: Value for property Some::$a must be of the type int|null, string given"Type declarations are case insensitive
Definitions are compatible with PSR-5 standard and follows ABNF definitions:
type-expression = type *("|" type)
type = class-name / keyword / array
array = (type / array-expression) "[]" / generic
array-expression = "(" type-expression ")"
generic = collection-type "<" [type-expression "," *SP] type-expression ">"
collection-type = class-name / "array"
class-name = ["\"] label *("\" label)
label = (ALPHA / %x7F-FF) *(ALPHA / DIGIT / %x7F-FF)
keyword = "array" / "bool" / "callable" / "false" / "float" / "int" / "mixed" / "null" / "object" /
keyword = "resource" / "self" / "static" / "string" / "true" / "void" / "$this"
Primitive types (@property [primitive] $a):
int(orinteger) - property value are integerbool(orboolean) - value are booleanfloat(ordouble) - value are floatstring- value are string or object with__toStringmethodnull(orvoid) - value are nullableresource- value are resourceobject- value can be any objectmixed- no type checkingcallable(orclosure) - value can be string, instance of \Closure, array with 2 args or object with__invokemethod
closurealias will be deprecated in future
scalar- value cannot be an object
Does not available yet: it will be supports in future
self- value can be object of self class or string with name of self class
Does not available yet: it will be supports in future
static- value can be instance of self class or string whos are sublass of self
Does not available yet: it will be supports in future
$this- value can be only object instance of self class
Does not available yet: it will be supports in future
Arrays and collections
array- value is type of arrayClass[]- value is type of array or instance of \Traversablescalar[]- value is type of array or instance of \TraversableCollection<>- value is type of array or instance of "Collection" and \TraversableCollection<T>- value is type of array or instance of "Collection" and \TraversableCollection<T,V>- value is type of array or instance of "Collection" and \Traversable
See more: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#appendix-a-types
Trait Properties declare __get and __set methods.
If you try access to some property - it:
- Search for
getSomemethod - Search docblock -
@property-reador@propertyand check type - If type is
boolorboolean- searchisSomemethod declaration - Or return
$somefield (if docblock info declares this property). It must beprotectedorprivate
If you write new value inside some property - it:
-
Search for
setSomemethod -
Search docblock -
@property-writeor@property -
Read
@propertytypehint declaration based onPSR-5type declarations: https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md -
Sets a new value in
$someproperty (if docblock info declares this property). It must beprotectedorprivate
