AuthorizationException.php
3 weeks ago
InvalidCastException.php
3 weeks ago
InvalidMiddlewareException.php
3 weeks ago
InvalidRoutActionException.php
3 weeks ago
InvalidValidationRuleException.php
3 weeks ago
MassAssignmentException.php
3 weeks ago
ModelNotFoundException.php
3 weeks ago
MultipleRecordsFoundException.php
3 weeks ago
NotFoundException.php
3 weeks ago
QueryException.php
3 weeks ago
RecordNotFoundException.php
3 weeks ago
UniqueConstraintViolationException.php
3 weeks ago
ValidationException.php
3 weeks ago
ModelNotFoundException.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Exception thrown when a model (database record) is not found. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Exceptions |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Exceptions; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Kirki\Framework\Supports\Arr; |
| 14 | use function Kirki\Framework\message; |
| 15 | class ModelNotFoundException extends NotFoundException |
| 16 | { |
| 17 | /** |
| 18 | * The model class name being queried. |
| 19 | * |
| 20 | * @var string |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | protected $model; |
| 25 | /** |
| 26 | * The IDs or route keys that were searched for. |
| 27 | * |
| 28 | * @var mixed |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | protected $ids; |
| 33 | /** |
| 34 | * Create a new ModelNotFoundException instance. |
| 35 | * |
| 36 | * @param string $model The model class name. |
| 37 | * @param mixed $ids The ID(s) or route key(s) searched for. |
| 38 | * |
| 39 | * @return void |
| 40 | * |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public function __construct($model, $ids = []) |
| 44 | { |
| 45 | $this->model = $model; |
| 46 | $this->ids = $ids; |
| 47 | $this->prepare_message(); |
| 48 | } |
| 49 | /** |
| 50 | * Set the model class name for the exception. |
| 51 | * |
| 52 | * @param string $model The model class name. |
| 53 | * |
| 54 | * @return void |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public function set_model($model) |
| 59 | { |
| 60 | $this->model = $model; |
| 61 | $this->prepare_message(); |
| 62 | } |
| 63 | /** |
| 64 | * Set the IDs or route keys that were searched for. |
| 65 | * |
| 66 | * @param mixed $ids The ID(s) or route key(s). |
| 67 | * |
| 68 | * @return void |
| 69 | * |
| 70 | * @since 1.0.0 |
| 71 | */ |
| 72 | public function set_ids($ids) |
| 73 | { |
| 74 | $this->ids = $ids; |
| 75 | $this->prepare_message(); |
| 76 | } |
| 77 | /** |
| 78 | * Get the model class name. |
| 79 | * |
| 80 | * @return string |
| 81 | * |
| 82 | * @since 1.0.0 |
| 83 | */ |
| 84 | public function get_model() |
| 85 | { |
| 86 | return $this->model; |
| 87 | } |
| 88 | /** |
| 89 | * Get the ID(s) or route key(s). |
| 90 | * |
| 91 | * @return mixed |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public function get_ids() |
| 96 | { |
| 97 | return $this->ids; |
| 98 | } |
| 99 | /** |
| 100 | * Prepare the exception message using the model and ids. |
| 101 | * |
| 102 | * @return void |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | protected function prepare_message() |
| 107 | { |
| 108 | $ids = Arr::wrap($this->ids); |
| 109 | $this->message = message('model.not_found', $this->model, \implode(', ', $ids)); |
| 110 | } |
| 111 | } |
| 112 |