tsmsogn /
attribute
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Attribute; |
||||
| 4 | |||||
| 5 | use Attribute\Exception\MissingRequiredAttributeException; |
||||
| 6 | |||||
| 7 | trait Attributable |
||||
| 8 | { |
||||
| 9 | /** |
||||
| 10 | * @param $attribute |
||||
| 11 | * @return bool |
||||
| 12 | */ |
||||
| 13 | public function isRequiredAttribute($attribute) |
||||
| 14 | { |
||||
| 15 | return in_array($attribute, $this->getRequiredAttributes(), true); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 16 | } |
||||
| 17 | |||||
| 18 | /** |
||||
| 19 | * @param $attribute |
||||
| 20 | * @return bool |
||||
| 21 | */ |
||||
| 22 | public function isOptionalAttribute($attribute) |
||||
| 23 | { |
||||
| 24 | return in_array($attribute, $this->getOptionalAttributes(), true); |
||||
|
0 ignored issues
–
show
It seems like
getOptionalAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 25 | } |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * @return bool |
||||
| 29 | * @throws \Attribute\Exception\MissingRequiredAttributeException |
||||
| 30 | */ |
||||
| 31 | public function attributeMissing() |
||||
| 32 | { |
||||
| 33 | $requiredAttributes = $this->getRequiredAttributes(); |
||||
| 34 | |||||
| 35 | foreach ($requiredAttributes as $requiredAttribute) { |
||||
| 36 | if ($this->$requiredAttribute === null) { |
||||
| 37 | throw new MissingRequiredAttributeException("The {$requiredAttribute} is missing"); |
||||
| 38 | } |
||||
| 39 | } |
||||
| 40 | |||||
| 41 | return true; |
||||
| 42 | } |
||||
| 43 | } |
||||
| 44 |