PHPackages                             tii/csv-state-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. tii/csv-state-parser

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

tii/csv-state-parser
====================

Parses CSV files with a simple finite-state machine.

v1.1.0(1y ago)076MITPHPPHP ^8.2

Since Sep 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/TiiFuchs/csv-state-parser)[ Packagist](https://packagist.org/packages/tii/csv-state-parser)[ RSS](/packages/tii-csv-state-parser/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (4)Versions (3)Used By (0)

[![Contributors](https://camo.githubusercontent.com/ba41383be8dac036397291ea3aabfad6a4acf2dbc695c6086205eb3aea0ae392/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f54696946756368732f6373762d73746174652d7061727365722e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/TiiFuchs/csv-state-parser/graphs/contributors)[![Forks](https://camo.githubusercontent.com/c453a5f53b858ee5f3b9605dcd7595bf15f4148ee267b71e08a48c59b7d575ab/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f54696946756368732f6373762d73746174652d7061727365722e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/TiiFuchs/csv-state-parser/network/members)[![Stargazers](https://camo.githubusercontent.com/a750c42fb1e6e43b0a11e306230d59f8c3c1860d1a864117b2a74517fcf834d1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f54696946756368732f6373762d73746174652d7061727365722e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/TiiFuchs/csv-state-parser/stargazers)[![Issues](https://camo.githubusercontent.com/55b7cb5ac47ee327a7153e64b1df907d632661d6b264ddede386a58cfa12a77c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f54696946756368732f6373762d73746174652d7061727365722e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/TiiFuchs/csv-state-parser/issues)[![MIT License](https://camo.githubusercontent.com/5088cafb81e8f1e07d9b4752e7adccf280dfb68f7a6fe2dd624215f07baf4478/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f54696946756368732f6373762d73746174652d7061727365722e7376673f7374796c653d666f722d7468652d6261646765)](https://github.com/TiiFuchs/csv-state-parser/blob/master/LICENSE.txt)

### CSV State Parser

[](#csv-state-parser)

 Parses CSV files with a simple finite-state machine.

 Table of Contents1. [Getting Started](#getting-started)
2. [Installation](#installation)
3. [Contributing](#contributing)
4. [License](#license)
5. [Contact](#contact)

Installation
------------

[](#installation)

Installation is simple.

Just run `composer require tii/csv-state-parser`

([back to top](#readme-top))

Usage
-----

[](#usage)

Create a Parser class that extends `\Tii\CsvStateParser\CsvStateParser` and implement the `stateStart` and `result`methods. See below for a full example.

Every row gets passed into the current actives stateMethod (`stateStart` in the beginning).

You can change the state from the next row on by calling `$this->state($nextState)` and pass a String backed enum or a value that can be cast to string. The next row will get passed to the corresponding stateMethod. I.e. if you call `$this->state('fooBar')` the next row will get passed to `stateFooBar(array $row)`.

If you need to convert **every** field in the CSV file beforehand you can overwrite the `protected function mapValue(string $value): string` method. This is i.e. useful for encoding conversions.

There are a few additional helpers that help you achieve stuff:

methoddescription`$this->done();`Tell the parser to quit early.`$this->skip(int $count)`Tell the parser to skip `$count` lines before calling the stateMethod again.You are responsible for compiling the data that the parser should return at the end yourself. This data should be returned by the `result()` method.

Here is a full example:

```
/**
 * @extends \Tii\CsvStateParser\CsvStateParser
 */
class SumParser extends \Tii\CsvStateParser\CsvStateParser
{

    protected array $sums = [];

    protected function result(): array
    {
        return $this->sums;
    }

    protected function stateStart(array $row): void
    {
        if ($row[0] === 'START') {
            $this->state('list');
        }
    }

    protected function stateList(array $row): void
    {
        if ($row[0] === 'END') {
            $this->done();
            return;
        }

        $this->items[] = array_reduce($row, fn($sum, $number) => $sum + $number, 0);
    }

}
```

You can use your parser by instantiating it, and calling the `parse(string $filename)` method.
If you need to adjust the separator, enclosure and escape char you can pass those to the constructor.

⚠️ **Beware!** In contrast to PHPs fgetcsv function this package uses ';' as the default separator character for CSV files.

```
$parser = new SumParser(separator: ',');
$list = $parser->parse('filename.csv');
```

([back to top](#readme-top))

Contributing
------------

[](#contributing)

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

([back to top](#readme-top))

### Top contributors:

[](#top-contributors)

[ ![contrib.rocks image](https://camo.githubusercontent.com/dab3c75c63650d139ff6fe2a80af0959caa86154b0db773ff49e075c983abb7e/68747470733a2f2f636f6e747269622e726f636b732f696d6167653f7265706f3d54696946756368732f6373762d73746174652d706172736572)](https://github.com/TiiFuchs/csv-state-parser/graphs/contributors)License
-------

[](#license)

Distributed under the MIT License. See `LICENSE` for more information.

([back to top](#readme-top))

Contact
-------

[](#contact)

Tii - [@Tii](https://chaos.social/@Tii) -

Project Link:

([back to top](#readme-top))

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

619d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e7ff8761fd0f3c0ecd77e009b9984ac34a247533221fb8c546421f05300f5769?d=identicon)[Tii](/maintainers/Tii)

---

Top Contributors

[![TiiFuchs](https://avatars.githubusercontent.com/u/1958744?v=4)](https://github.com/TiiFuchs "TiiFuchs (7 commits)")

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/tii-csv-state-parser/health.svg)

```
[![Health](https://phpackages.com/badges/tii-csv-state-parser/health.svg)](https://phpackages.com/packages/tii-csv-state-parser)
```

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M65](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M627](/packages/jms-serializer-bundle)[hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

97513.5M170](/packages/hassankhan-config)[meyfa/php-svg

Read, edit, write, and render SVG files with PHP

54613.9M42](/packages/meyfa-php-svg)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
