Giter Site home page Giter Site logo

csv-parser's Introduction

Latest Stable Version CI Status Type Coverage codecov

sebastian/csv-parser

Library for type-safe parsing of CSV files.

Installation

You can add this library as a local, per-project dependency to your project using Composer:

composer require sebastian/csv-parser

If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:

composer require --dev sebastian/csv-parser

Usage

example.csv

1,2,3,1,0,2023-03-24
<?php declare(strict_types=1);
use SebastianBergmann\CsvParser\Parser;
use SebastianBergmann\CsvParser\Schema;
use SebastianBergmann\CsvParser\FieldDefinition;
use SebastianBergmann\CsvParser\Type;
use SebastianBergmann\CsvParser\ObjectMapper;

$schema = Schema::from(
    FieldDefinition::from(1, 'a', Type::integer()),
    FieldDefinition::from(2, 'b', Type::float()),
    FieldDefinition::from(3, 'c', Type::string()),
    FieldDefinition::from(4, 'd', Type::boolean()),
    FieldDefinition::from(5, 'e', Type::boolean()),
    FieldDefinition::from(6, 'f', Type::object(
        new class implements ObjectMapper
        {
            public function map(string $value): DateTimeImmutable
            {
                return new DateTimeImmutable($value);
            }
        }
    )),
);

$parser = new Parser;

foreach ($parser->parse('example.csv', $schema) as $record) {
    var_dump($record);
}

The example above shows how to use a Schema object to configure how a string from a line of comma-separated values is parsed into an array element:

  • 1 refers to the position of the string in the line of comma-separated values
  • 'a' specifies the key for the value in the associative array that is generated for each record
  • Type::integer() specifies the type that should be used to store the value in the associative array that is generated for each record

The following types are available:

  • boolean (Type::boolean(); uses (bool) type cast)
  • integer (Type::integer(); uses (int) type cast)
  • float (Type::float(); uses (float) type cast)
  • string (Type::string())
  • object (Type::object($mapper); $mapper is an object that implements the SebastianBergmann\CsvParser\ObjectMapper interface)

The Parser::parse() method requires two arguments:

  • The first argument, $filename, is the path to the CSV file that should be parsed
  • The second argument, $schema, is the Schema object we discussed above

Running the example shown above prints the output shown below:

array(3) {
  ["a"]=>
  int(1)
  ["b"]=>
  float(2)
  ["c"]=>
  string(1) "3"
  ["d"]=>
  bool(true)
  ["e"]=>
  bool(false)
  ["f"]=>
  object(DateTimeImmutable)#1 (3) {
    ["date"]=>
    string(26) "2023-03-24 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(13) "Europe/Berlin"
  }
}

The $parser->ignoreFirstLine() method can be used to configure the parser to ignore the first line of the CSV file.

The $parser->setSeparator() method can be used to configure the parser to use a separator different from the default ,.

The $parser->setEnclosure() method can be used to configure the parser to use an enclosure different from the default ".

The $parser->setEscape() method can be used to configure the parser to use an escape different from the default ".

csv-parser's People

Contributors

chemaclass avatar localheinz avatar sebastianbergmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

csv-parser's Issues

Infection reports escaped mutant in CI but not locally

$ git show --oneline -s
ab1d88b (HEAD -> main, origin/main) Refactor

Local environment (--only-covered)

$ ./tools/infection --only-covered              

    ____      ____          __  _
   /  _/___  / __/__  _____/ /_(_)___  ____
   / // __ \/ /_/ _ \/ ___/ __/ / __ \/ __ \
 _/ // / / / __/  __/ /__/ /_/ / /_/ / / / /
/___/_/ /_/_/  \___/\___/\__/_/\____/_/ /_/

#StandWithUkraine

Infection - PHP Mutation Testing Framework version 0.26.19

[notice] You are running Infection with PCOV enabled.

Running initial test suite...

PHPUnit version: 10.0.18

   16 [============================] < 1 sec

Generate mutants...

Processing source code files: 11/11
.: killed, M: escaped, U: uncovered, E: fatal error, X: syntax error, T: timed out, S: skipped, I: ignored

.......................................              (39 / 39)

39 mutations were generated:
      39 mutants were killed
       0 mutants were configured to be ignored
       0 mutants were not covered by tests
       0 covered mutants were not detected
       0 errors were encountered
       0 syntax errors were encountered
       0 time outs were encountered
       0 mutants required more time than configured

Metrics:
         Mutation Score Indicator (MSI): 100%
         Mutation Code Coverage: 100%
         Covered Code MSI: 100%

Note: to see escaped mutants run Infection with "--show-mutations" or configure file loggers.

Please note that some mutants will inevitably be harmless (i.e. false positives).

Time: 10s. Memory: 10.00MB

Local environment (--show-mutations)

./tools/infection --show-mutations               

    ____      ____          __  _
   /  _/___  / __/__  _____/ /_(_)___  ____
   / // __ \/ /_/ _ \/ ___/ __/ / __ \/ __ \
 _/ // / / / __/  __/ /__/ /_/ / /_/ / / / /
/___/_/ /_/_/  \___/\___/\__/_/\____/_/ /_/

#StandWithUkraine

Infection - PHP Mutation Testing Framework version 0.26.19

[notice] You are running Infection with PCOV enabled.

Running initial test suite...

PHPUnit version: 10.0.18

   16 [============================] < 1 sec

Generate mutants...

Processing source code files: 11/11
.: killed, M: escaped, U: uncovered, E: fatal error, X: syntax error, T: timed out, S: skipped, I: ignored

U.......................................             (40 / 40)

40 mutations were generated:
      39 mutants were killed
       0 mutants were configured to be ignored
       1 mutants were not covered by tests
       0 covered mutants were not detected
       0 errors were encountered
       0 syntax errors were encountered
       0 time outs were encountered
       0 mutants required more time than configured

Metrics:
         Mutation Score Indicator (MSI): 97%
         Mutation Code Coverage: 97%
         Covered Code MSI: 100%

Please note that some mutants will inevitably be harmless (i.e. false positives).

Time: 10s. Memory: 10.00MB

[ERROR] The minimum required MSI percentage should be 100%, but actual is 97.5%. Improve your tests!                   

GitHub Actions

See https://github.com/sebastianbergmann/csv-parser/actions/runs/4498004606/jobs/7914157732

Improve type coverage (and fix issues identified by Psalm that are currently baselined)

$ ./tools/psalm --config=.psalm/config.xml --show-info=true --stats --no-cache --monochrome
.
.
.
INFO: ArgumentTypeCoercion - src/Parser.php:59:34 -
      Argument 1 of SebastianBergmann\CsvParser\Schema::apply expects list<string>,
      but parent type array<array-key, mixed> provided (see https://psalm.dev/193)
      yield $schema->apply($line);


INFO: MixedAssignment - src/schema/ColumnDefinition.php:68:9 -
      Unable to determine the type of this assignment (see https://psalm.dev/032)
      $output[$this->name] = $this->type->apply($value);
.
.
.
Psalm was able to infer types for 98.6111% of the codebase
.
.
.
100.000% src/Parser.php (0 mixed)
96.296% src/schema/ColumnDefinition.php (1 mixed)
100.000% src/schema/Schema.php (0 mixed)
100.000% src/schema/type/BooleanType.php (0 mixed)
100.000% src/schema/type/FloatType.php (0 mixed)
100.000% src/schema/type/IntegerType.php (0 mixed)
100.000% src/schema/type/StringType.php (0 mixed)
100.000% src/schema/type/Type.php (0 mixed)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.