Skip to content

Commit b111b9a

Browse files
Allow defaults (limosa-io#83)
1 parent b813b5e commit b111b9a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/Attribute/Attribute.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Attribute
3838
protected $mutability = 'readWrite';
3939
protected $type = 'string';
4040
protected $description = null;
41+
protected $defaultValue = null;
4142

4243
public $dirty = false;
4344

@@ -101,6 +102,12 @@ public function ensure(...$validations)
101102
return $this;
102103
}
103104

105+
public function default($value){
106+
$this->defaultValue = $value;
107+
108+
return $this;
109+
}
110+
104111
public function isRequired()
105112
{
106113
return in_array('required', $this->validations);
@@ -143,7 +150,7 @@ public function read(&$object, array $attributes = []): ?AttributeValue
143150
return null;
144151
}
145152

146-
return new AttributeValue($this->doRead($object, $attributes));
153+
return new AttributeValue($this->doRead($object, $attributes) ?? $this->defaultValue);
147154
}
148155

149156
protected function doRead(&$object, $attributes = [])

0 commit comments

Comments
 (0)