Completed
Push — master ( 869d3d...24f8ca )
by ignace nyamagana
03:44
created

CannotInsertRecord   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
dl 0
loc 68
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromStream() 0 7 1
A createFromValidator() 0 8 1
A getName() 0 4 1
A getRecord() 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\Exception;
16
17
use League\Csv\Exception;
18
use RuntimeException;
19
20
/**
21
 * Thrown when a data is not added to the Csv Document
22
 *
23
 * @package League.csv
24
 * @since   9.0.0
25
 * @author  Ignace Nyamagana Butera <[email protected]>
26
 *
27
 */
28
class CannotInsertRecord extends RuntimeException implements Exception
29
{
30
    /**
31
     * The record submitted for insertion
32
     *
33
     * @var array
34
     */
35
    protected $record;
36
37
    /**
38
     * Validator which did not validated the data
39
     *
40
     * @var string
41
     */
42
    protected $name = '';
43
44
    /**
45
     * Create an Exception from a record insertion into a stream
46
     *
47
     * @param string[] $record
48
     *
49
     * @return self
50
     */
51 2
    public static function createFromStream(array $record): self
52
    {
53 2
        $exception = new static('Unable to write record to the CSV document');
54 2
        $exception->record = $record;
55
56 2
        return $exception;
57
    }
58
59
    /**
60
     * Create an Exception from a Record Validation
61
     *
62
     * @param string   $name   validator name
63
     * @param string[] $record invalid  data
64
     *
65
     * @return self
66
     */
67 6
    public static function createFromValidator(string $name, array $record): self
68
    {
69 6
        $exception = new static('Record validation failed');
70 6
        $exception->name = $name;
71 6
        $exception->record = $record;
72
73 6
        return $exception;
74
    }
75
76
    /**
77
     * return the validator name
78
     *
79
     * @return string
80
     */
81 2
    public function getName(): string
82
    {
83 2
        return $this->name;
84
    }
85
86
    /**
87
     * return the invalid data submitted
88
     *
89
     * @return array
90
     */
91 4
    public function getRecord(): array
92
    {
93 4
        return $this->record;
94
    }
95
}
96