Skip to content

Commit 77c3afb

Browse files
authored
Update AbstractRepository.php
1 parent c40e96b commit 77c3afb

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

src/Repositories/AbstractRepository.php

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ abstract class AbstractRepository implements RepositoryContract
3636
protected $modelInstance;
3737

3838
/**
39-
* The errors message bag instance
40-
*
4139
* @var \Illuminate\Support\MessageBag
4240
*/
43-
protected $errors;
41+
protected MessageBag $message_bag;
4442

4543
/**
4644
* @var \Illuminate\Database\Eloquent\Builder
@@ -681,41 +679,77 @@ protected function applyScope()
681679
return $this;
682680
}
683681

682+
684683
/**
685-
* Add a message to the repository's error messages.
686-
*
687-
* @param string $message
688-
* @param string $key
689-
*
690-
* @return static
684+
* {@inheritDoc}
691685
*/
692-
public function addError(string $message, string $key = 'message')
686+
public function getMessageBag(): MessageBag
693687
{
694-
$this->getErrors()->add($key, $message);
688+
if ($this->message_bag === null) {
689+
$this->message_bag = new MessageBag;
690+
}
691+
692+
return $this->message_bag;
693+
}
694+
695+
/**
696+
* {@inheritDoc}
697+
*/
698+
public function addMessage(string $message, string $key = 'message'): static
699+
{
700+
$this->getMessageBag()->add($key, $message);
695701

696702
return $this;
697703
}
698704

699705
/**
700-
* Get the repository's error messages.
701-
*
702-
* @return \Illuminate\Support\MessageBag
706+
* {@inheritDoc}
703707
*/
704-
public function getErrors()
708+
public function hasMessage(string $key = 'message'): bool
705709
{
706-
if ($this->errors === null) {
707-
$this->errors = new MessageBag;
708-
}
710+
return $this->getMessageBag()->has($key);
711+
}
712+
713+
/**
714+
* {@inheritDoc}
715+
*/
716+
public function getMessage(string $key = null, string $format = null, string $default = ''): string
717+
{
718+
return $this->getMessageBag()->first($key, $format) ?: $default;
719+
}
720+
721+
/**
722+
* {@inheritDoc}
723+
*/
724+
public function addError(string $message)
725+
{
726+
$this->addMessage($message, 'error');
727+
728+
return $this;
729+
}
709730

710-
return $this->errors;
731+
/**
732+
* {@inheritDoc}
733+
*/
734+
public function hasErrors(): bool
735+
{
736+
return $this->getMessageBag()->has('error');
737+
}
738+
739+
/**
740+
* {@inheritDoc}
741+
*/
742+
public function getErrors(string $format = null): array
743+
{
744+
return $this->getMessageBag()->get('error', $format);
711745
}
712746

713747
/**
714748
* {@inheritDoc}
715749
*/
716750
public function getErrorMessage(string $default = ''): string
717751
{
718-
return $this->getErrors()->first('message') ?: $default;
752+
return $this->getMessage('error') ?: $default;
719753
}
720754

721755
/**

0 commit comments

Comments
 (0)