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

InsertionException   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 68
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromCsv() 0 7 1
A createFromValidator() 0 8 1
A getName() 0 4 1
A getData() 0 4 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
 *
25
 */
26
class InsertionException extends RuntimeException
27
{
28
    /**
29
     * The record submitted for insertion
30
     *
31
     * @var array
32
     */
33
    protected $data;
34
35
    /**
36
     * Validator which did not validated the data
37
     *
38
     * @var string
39
     */
40
    protected $name = '';
41
42
    /**
43
     * Create an Exception from a Record row
44
     *
45
     * @param string[] $record
46
     *
47
     * @return self
48
     */
49 2
    public static function createFromCsv(array $record)
50
    {
51 2
        $exception = new static('Unable to write data to the CSV document');
52 2
        $exception->data = $record;
53
54 2
        return $exception;
55
    }
56
57
    /**
58
     * Create an Exception from a Record row
59
     *
60
     * @param string   $name validator name
61
     * @param string[] $data invalid  data
62
     *
63
     * @return self
64
     */
65 8
    public static function createFromValidator(string $name, array $data)
66
    {
67 8
        $exception = new static('row validation failed');
68 8
        $exception->name = $name;
69 8
        $exception->data = $data;
70
71 8
        return $exception;
72
    }
73
74
    /**
75
     * return the validator name
76
     *
77
     * @return string
78
     */
79 2
    public function getName(): string
80
    {
81 2
        return $this->name;
82
    }
83
84
    /**
85
     * return the invalid data submitted
86
     *
87
     * @return array
88
     */
89 4
    public function getData(): array
90
    {
91 4
        return $this->data;
92
    }
93
}
94