Completed
Pull Request — master (#210)
by ignace nyamagana
02:39
created

InsertionException::createFromValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
/**
3
* This file is part of the League.csv library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/thephpleague/csv/
7
* @version 9.0.0
8
* @package League.csv
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
declare(strict_types=1);
14
15
namespace League\Csv;
16
17
use RuntimeException;
18
19
/**
20
 *  Thrown when a data is not added to the Csv Document
21
 *
22
 * @package League.csv
23
 * @since   9.0.0
24
 * @author  Ignace Nyamagana Butera <[email protected]>
25
 *
26
 */
27
class InsertionException extends RuntimeException implements Exception
28
{
29
    /**
30
     * The record submitted for insertion
31
     *
32
     * @var array
33
     */
34
    protected $data;
35
36
    /**
37
     * Validator which did not validated the data
38
     *
39
     * @var string
40
     */
41
    protected $name = '';
42
43
    /**
44
     * Create an Exception from a Record row
45
     *
46
     * @param string[] $record
47
     *
48
     * @return self
49
     */
50 2
    public static function createFromCsv(array $record): self
51
    {
52 2
        $exception = new static('Unable to write data to the CSV document');
53 2
        $exception->data = $record;
54
55 2
        return $exception;
56
    }
57
58
    /**
59
     * Create an Exception from a Record row
60
     *
61
     * @param string   $name validator name
62
     * @param string[] $data invalid  data
63
     *
64
     * @return self
65
     */
66 8
    public static function createFromValidator(string $name, array $data): self
67
    {
68 8
        $exception = new static('row validation failed');
69 8
        $exception->name = $name;
70 8
        $exception->data = $data;
71
72 8
        return $exception;
73
    }
74
75
    /**
76
     * return the validator name
77
     *
78
     * @return string
79
     */
80 2
    public function getName(): string
81
    {
82 2
        return $this->name;
83
    }
84
85
    /**
86
     * return the invalid data submitted
87
     *
88
     * @return array
89
     */
90 4
    public function getData(): array
91
    {
92 4
        return $this->data;
93
    }
94
}
95