2012-03-11 08:42:13 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Builder;
|
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
class Property extends PhpParser\BuilderAbstract
|
2012-03-11 08:42:13 +01:00
|
|
|
{
|
|
|
|
protected $name;
|
|
|
|
|
2014-12-19 17:21:46 +01:00
|
|
|
protected $type = 0;
|
|
|
|
protected $default = null;
|
|
|
|
protected $attributes = array();
|
2012-03-11 08:42:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a property builder.
|
|
|
|
*
|
|
|
|
* @param string $name Name of the property
|
|
|
|
*/
|
|
|
|
public function __construct($name) {
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the property public.
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function makePublic() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PUBLIC);
|
2012-03-11 08:42:13 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the property protected.
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function makeProtected() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PROTECTED);
|
2012-03-11 08:42:13 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the property private.
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function makePrivate() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_PRIVATE);
|
2012-03-11 08:42:13 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes the property static.
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function makeStatic() {
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->setModifier(Stmt\Class_::MODIFIER_STATIC);
|
2012-03-11 08:42:13 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets default value for the property.
|
|
|
|
*
|
|
|
|
* @param mixed $value Default value to use
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function setDefault($value) {
|
|
|
|
$this->default = $this->normalizeValue($value);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-12-13 13:44:40 +01:00
|
|
|
/**
|
|
|
|
* Sets doc comment for the property.
|
|
|
|
*
|
|
|
|
* @param PhpParser\Comment\Doc|string $docComment Doc comment to set
|
|
|
|
*
|
2014-12-19 17:21:46 +01:00
|
|
|
* @return $this The builder instance (for fluid interface)
|
2014-12-13 13:44:40 +01:00
|
|
|
*/
|
|
|
|
public function setDocComment($docComment) {
|
|
|
|
$this->attributes = array(
|
|
|
|
'comments' => array($this->normalizeDocComment($docComment))
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-11 08:42:13 +01:00
|
|
|
/**
|
|
|
|
* Returns the built class node.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @return Stmt\Property The built property node
|
2012-03-11 08:42:13 +01:00
|
|
|
*/
|
|
|
|
public function getNode() {
|
2014-02-06 14:44:16 +01:00
|
|
|
return new Stmt\Property(
|
|
|
|
$this->type !== 0 ? $this->type : Stmt\Class_::MODIFIER_PUBLIC,
|
2012-03-11 08:42:13 +01:00
|
|
|
array(
|
2014-02-06 14:44:16 +01:00
|
|
|
new Stmt\PropertyProperty($this->name, $this->default)
|
2014-12-13 13:44:40 +01:00
|
|
|
),
|
|
|
|
$this->attributes
|
2012-03-11 08:42:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|