-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvalidDataSetException.php
66 lines (54 loc) · 1.6 KB
/
InvalidDataSetException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase\Exception;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Upmind\ProvisionBase\Exception\Contract\ProvisionException;
/**
* The given data failed to pass the validation rules for this type of dataset
*/
class InvalidDataSetException extends InvalidArgumentException implements ProvisionException
{
/**
* Optional error key prefix.
*
* @var string|null
*/
protected $errorPrefix;
/**
* @param ValidationException $validationException
*/
protected $validationException;
public function __construct(ValidationException $validationException, ?string $message = null)
{
$this->validationException = $validationException;
parent::__construct($message ?: $validationException->getMessage(), 0, $validationException);
}
/**
* @return static
*/
public function setErrorPrefix(?string $prefix)
{
$this->errorPrefix = $prefix ?: null;
return $this;
}
/**
* @return string[]
*/
public function errors(): array
{
$prefix = isset($this->errorPrefix)
? rtrim($this->errorPrefix, '.')
: null;
if (empty($prefix)) {
return $this->validationException->errors();
}
return collect($this->validationException->errors())
->mapWithKeys(function ($error, $key) use ($prefix) {
return [
sprintf('%s.%s', $prefix, ltrim($key, '.')) => $error
];
})
->all();
}
}