Facades
3 weeks ago
Traits
3 weeks ago
Arr.php
3 weeks ago
DataCaster.php
3 weeks ago
Flex.php
3 weeks ago
HigherOrderTapProxy.php
3 weeks ago
MediaAttachment.php
3 weeks ago
MessagesBag.php
3 weeks ago
Str.php
3 weeks ago
Url.php
3 weeks ago
Utils.php
3 weeks ago
MessagesBag.php
159 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The messages bag class. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Supports |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Supports; |
| 11 | |
| 12 | use function Kirki\Framework\app; |
| 13 | use function Kirki\Framework\config; |
| 14 | \defined('ABSPATH') || exit; |
| 15 | class MessagesBag |
| 16 | { |
| 17 | /** |
| 18 | * The messages bag where all the app defined user faced messages are stored. |
| 19 | * |
| 20 | * @var array |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | protected $messages = []; |
| 25 | /** |
| 26 | * Create a new messages bag instance. |
| 27 | * |
| 28 | * @return void |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | public function __construct() |
| 33 | { |
| 34 | $this->messages = $this->messages(); |
| 35 | } |
| 36 | /** |
| 37 | * Get the default messages. |
| 38 | * These values can be overridden by the app defined messages |
| 39 | * at config/messages.php file. |
| 40 | * Plugin developers can use translated message text there. |
| 41 | * |
| 42 | * @return array |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | protected function defaults() |
| 47 | { |
| 48 | return [ |
| 49 | 'validator.invalid' => 'The value provided for %s is invalid.', |
| 50 | 'validator.required' => 'The %s field is required.', |
| 51 | 'validator.string' => 'The %s field must be of type string.', |
| 52 | 'validator.array' => 'The %s field must be of type array.', |
| 53 | 'validator.object' => 'The %s field must be of type object.', |
| 54 | 'validator.boolean' => 'The %s field must be of type boolean.', |
| 55 | 'validator.integer' => 'The %s field must be of type integer.', |
| 56 | 'validator.number' => 'The %s field must be of type number.', |
| 57 | 'validator.float' => 'The %s field must be of type float.', |
| 58 | 'validator.email' => 'The %s field must be of type email.', |
| 59 | 'validator.email_unique' => 'The email address %s is already in use.', |
| 60 | 'validator.unique' => 'The value for %s must be unique.', |
| 61 | 'validator.url' => 'The %s field must be of type url.', |
| 62 | 'validator.exists' => 'Resource does not exist.', |
| 63 | 'validator.min.characters' => 'The %s field must be greater than or equal to %s characters.', |
| 64 | 'validator.min.items' => 'The %s field must be greater than or equal to %s items.', |
| 65 | 'validator.min.numeric' => 'The %s field must be greater than or equal %s.', |
| 66 | 'validator.max.characters' => 'The %s field must be less than or equal to %s characters.', |
| 67 | 'validator.max.items' => 'The %s field must be less than or equal to %s items.', |
| 68 | 'validator.max.numeric' => 'The %s field must be less than or equal %s.', |
| 69 | 'validator.in' => 'The %s field must contain a value from: %s.', |
| 70 | 'validator.not_in' => 'The %s field must not contain a value from: %s.', |
| 71 | 'validator.regex' => 'The %s field must match the regex: %s.', |
| 72 | 'validator.sanitize' => 'The %s field must be sanitized.', |
| 73 | 'validator.same_as' => 'The %s field must be same as %s.', |
| 74 | 'validator.date' => 'The %s field must be a valid date time in the format %s.', |
| 75 | 'validator.datetime' => 'The %s field must be a valid date time in the format %s.', |
| 76 | 'validator.date_format' => 'The %s field must be a valid date in the format %s.', |
| 77 | 'validator.is_valid_image_id' => 'The %s field must be a valid media image', |
| 78 | 'validator.required_if' => 'The %s field is required.', |
| 79 | 'validator.required_if_sibling' => 'The %s field is required.', |
| 80 | 'validator.prohibited' => 'The %s field is prohibited.', |
| 81 | 'validator.prohibited_if' => 'The %s field is prohibited.', |
| 82 | 'validator.required_if_exists' => 'The %s field is required.', |
| 83 | 'validator.user_exists' => 'User with id %s does not exist.', |
| 84 | 'validator.after' => 'The %s field must be after %s.', |
| 85 | 'validator.gt' => 'The %s field must be greater than %s.', |
| 86 | 'validator.gte' => 'The %s field must be greater than %s.', |
| 87 | 'validator.lt' => 'The %s field must be greater than %s.', |
| 88 | 'validator.lte' => 'The %s field must be greater than %s.', |
| 89 | 'validator.failed' => 'Validation failed!', |
| 90 | 'validator.expected_array' => 'Expected an array at "%s"', |
| 91 | 'auth.logged_in_required' => 'You have to be logged in.', |
| 92 | 'auth.admin_required' => 'You have to be logged in and have admin privileges.', |
| 93 | 'auth.unauthorized_request' => 'You are not authorized to make this request.', |
| 94 | 'auth.unauthorized_action' => 'You are not authorized to %s this resource.', |
| 95 | // phpcs:ignore Generic.Files.LineLength.TooLong |
| 96 | 'auth.no_policy' => 'No policy found for this resource. Pass the model instance or class name as the second argument.', |
| 97 | 'auth.ability_not_defined' => 'The ability %s is not defined in the policy for this resource.', |
| 98 | 'auth.upload_forbidden' => 'You are not authorized to upload files.', |
| 99 | 'auth.invalid_upload_path' => 'Invalid upload path.', |
| 100 | 'model.not_found' => 'No query results for model [%s] with ids: %s', |
| 101 | 'filesystem.file_not_found' => 'file does not exists at [%s]', |
| 102 | 'upload.directory_unavailable' => 'Upload directory is not available.', |
| 103 | 'upload.move_failed' => 'Could not move the file "%s" to "%s" (%s).', |
| 104 | 'upload.ini_size_exceeded' => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).', |
| 105 | 'upload.form_size_exceeded' => 'The file "%s" exceeds the upload limit defined in your form.', |
| 106 | 'upload.partial' => 'The file "%s" was only partially uploaded.', |
| 107 | 'upload.no_file' => 'No file was uploaded.', |
| 108 | 'upload.cant_write' => 'The file "%s" could not be written on disk.', |
| 109 | 'upload.no_tmp_dir' => 'File could not be uploaded: missing temporary directory.', |
| 110 | 'upload.stopped_by_extension' => 'File upload was stopped by a PHP extension.', |
| 111 | 'upload.unknown_error' => 'The file "%s" was not uploaded due to an unknown error.', |
| 112 | ]; |
| 113 | } |
| 114 | /** |
| 115 | * Get the app defined messages. |
| 116 | * |
| 117 | * @return array |
| 118 | * |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | protected function app_defined_messages() |
| 122 | { |
| 123 | $app = app(); |
| 124 | if (!\method_exists($app, 'config_path')) { |
| 125 | return []; |
| 126 | } |
| 127 | return config('messages', []); |
| 128 | } |
| 129 | /** |
| 130 | * Get the messages. |
| 131 | * |
| 132 | * @return array |
| 133 | * |
| 134 | * @since 1.0.0 |
| 135 | */ |
| 136 | protected function messages() |
| 137 | { |
| 138 | return \array_merge($this->defaults(), $this->app_defined_messages()); |
| 139 | } |
| 140 | /** |
| 141 | * Get a message by key. |
| 142 | * |
| 143 | * @param string $key The key. |
| 144 | * @param mixed $args The arguments. |
| 145 | * |
| 146 | * @return string|null |
| 147 | * |
| 148 | * @since 1.0.0 |
| 149 | */ |
| 150 | public function get(string $key, ...$args) |
| 151 | { |
| 152 | if (!\array_key_exists($key, $this->messages)) { |
| 153 | return null; |
| 154 | } |
| 155 | $args = !empty($args) ? Arr::flatten($args) : []; |
| 156 | return \vsprintf(esc_html($this->messages[$key]), $args); |
| 157 | } |
| 158 | } |
| 159 |