• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

courtney-miles / slurp / 7351864250

28 Dec 2023 10:55PM UTC coverage: 91.188% (-0.09%) from 91.282%
7351864250

push

github

web-flow
Merge pull request #59 from stefgas/task/zbdev-15688/throw-ExtractionException-when-primary-key-not-found

zbdev-15688 throw ExtractionException when primary key not found

6 of 8 new or added lines in 2 files covered. (75.0%)

714 of 783 relevant lines covered (91.19%)

9.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/src/Extract/CsvFileExtractor/EnforcePrimaryKeyIterator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MilesAsylum\Slurp\Extract\CsvFileExtractor;
6

7
use MilesAsylum\Slurp\Extract\Exception\DuplicatePrimaryKeyValueException;
8
use MilesAsylum\Slurp\Extract\Exception\MissingPrimaryKeyException;
9

10
class EnforcePrimaryKeyIterator extends \IteratorIterator
11
{
12
    private $primaryKeyFieldsValues = [];
13

14
    private $primaryKeyFields = [];
15

16
    public function __construct(\Traversable $iterator, array $primaryKeyFields)
17
    {
18
        parent::__construct($iterator);
9✔
19
        $this->primaryKeyFields = $primaryKeyFields;
9✔
20
    }
21

22
    /**
23
     * @throws DuplicatePrimaryKeyValueException
24
     */
25
    #[\ReturnTypeWillChange]
26
    public function current()
27
    {
28
        $currentRecord = parent::current();
9✔
29
        $pkValues = [];
9✔
30

31
        foreach ($this->primaryKeyFields as $pkField) {
9✔
32
            if (!array_key_exists($pkField, $currentRecord)) {
6✔
NEW
33
                throw new MissingPrimaryKeyException('The supplied record does not contain the primary key field ' . $pkField . '.');
×
34
            }
35

36
            $pkValues[$pkField] = $currentRecord[$pkField];
6✔
37
        }
38

39
        $pkValues = array_values($pkValues);
9✔
40
        $normPkValues = self::normalisePrimaryKeysValue($pkValues);
9✔
41

42
        if (isset($this->primaryKeyFieldsValues[$normPkValues])) {
9✔
43
            throw DuplicatePrimaryKeyValueException::create($this->primaryKeyFields, $pkValues, $this->key());
3✔
44
        }
45

46
        $this->primaryKeyFieldsValues[$normPkValues] = true;
9✔
47

48
        return parent::current();
9✔
49
    }
50

51
    private static function normalisePrimaryKeysValue(array $primaryKeyValues): string
52
    {
53
        return implode(
9✔
54
            ':',
9✔
55
            array_map(
9✔
56
                static function ($value) {
9✔
57
                    return addcslashes((string) $value, '\:');
6✔
58
                },
9✔
59
                $primaryKeyValues
9✔
60
            )
9✔
61
        );
9✔
62
    }
63
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc