PHPackages                             frictionlessdata/tableschema - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. frictionlessdata/tableschema

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

frictionlessdata/tableschema
============================

A utility library for working with Table Schema

v1.2.3(9mo ago)12194.4k—0%12[3 issues](https://github.com/frictionlessdata/tableschema-php/issues)[2 PRs](https://github.com/frictionlessdata/tableschema-php/pulls)6MITPHPPHP &gt;=7.1.0CI passing

Since Apr 13Pushed 9mo ago7 watchersCompare

[ Source](https://github.com/frictionlessdata/tableschema-php)[ Packagist](https://packagist.org/packages/frictionlessdata/tableschema)[ RSS](/packages/frictionlessdata-tableschema/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (19)Used By (6)

tableschema-php
===============

[](#tableschema-php)

[![Tests](https://github.com/frictionlessdata/tableschema-php/actions/workflows/tests.yml/badge.svg)](https://github.com/frictionlessdata/tableschema-php/actions/workflows/tests.yml)[![Coveralls](https://camo.githubusercontent.com/26b60f0a36d0e284150c6fdb5d1ef4b4bdedc797803b4a265dd460629f1efeb3/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6672696374696f6e6c657373646174612f7461626c65736368656d612d7068702e7376673f6272616e63683d6d6173746572)](https://coveralls.io/r/frictionlessdata/tableschema-php?branch=master)[![Scrutinizer-ci](https://camo.githubusercontent.com/b8b857f8e86f569cac8dd0c12529082f30d754dee0584ed07d04d1aff6e58831/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4f7269486f63682f7461626c65736368656d612d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/OriHoch/tableschema-php/)[![Packagist](https://camo.githubusercontent.com/879e536c634dcefa9e410f9ff47d80944d0c73d27c44367957db5560ec0e0d0c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6672696374696f6e6c657373646174612f7461626c65736368656d612e737667)](https://packagist.org/packages/frictionlessdata/tableschema)[![SemVer](https://camo.githubusercontent.com/45f93e2041a390dd2ac2b65c9d8a30d49c5bcb1daba2a4cb9b761ce1b57da332/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f76657273696f6e732d53656d5665722d627269676874677265656e2e737667)](http://semver.org/)[![Codebase](https://camo.githubusercontent.com/78247530a35f3bb28ca0dfe54db1e072372948cea17e1dc02b8e8a8219afe4bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f6465626173652d6769746875622d627269676874677265656e)](https://github.com/frictionlessdata/tableschema-php)[![Support](https://camo.githubusercontent.com/4daae5075cff697eaeeee7ece1f29ad900e1f015452da0316a897466c3788a35/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737570706f72742d646973636f72642d627269676874677265656e)](https://discordapp.com/invite/Sewv6av)

A utility library for working with [Table Schema](https://specs.frictionlessdata.io/table-schema/) in PHP.

Features summary and Usage guide
--------------------------------

[](#features-summary-and-usage-guide)

### Installation

[](#installation)

```
$ composer require frictionlessdata/tableschema
```

### Table

[](#table)

Table class allows to iterate over data conforming to a table schema

Instantiate a Table object based on a data source and a table schema.

```
use frictionlessdata\tableschema\Table;

$table = new Table("tests/fixtures/data.csv", ["fields" => [
    ["name" => "first_name"],
    ["name" => "last_name"],
    ["name" => "order"]
]]);
```

Schema can be any parameter valid for the Schema object (See below), so you can use a url or filename which contains the schema

```
$table = new Table("tests/fixtures/data.csv", "tests/fixtures/data.json");
```

iterate over the data, all the values are cast and validated according to the schema

```
foreach ($table as $row) {
    print($row["order"]." ".$row["first_name"]." ".$row["last_name"]."\n");
};
```

validate function will validate the schema and get some sample of the data itself to validate it as well

```
Table::validate(new CsvDataSource("http://invalid.data.source/"), $schema);
```

You can instantiate a table object without schema, in this case the schema will be inferred automatically based on the data

```
$table = new Table("tests/fixtures/data.csv");
$table->schema()->fields();  // ["first_name" => StringField, "last_name" => StringField, "order" => IntegerField]
```

Optionally, specify a [CSV Dialect](https://frictionlessdata.io/specs/csv-dialect/):

```
$table = new Table("tests/fixtures/data.csv", null, ["delimiter" => ";"]);
```

Table::read method allows to get all data as an array, it also supports options to modify reader behavior

```
$table->read()  // returns all the data as an array
```

read accepts an options parameter, for example:

```
$table->read(["cast" => false, "limit": 5])
```

The following options are available (the values are the default values):

```
$table->read([
    "keyed" => true,  // flag to emit keyed rows
    "extended" => false,  // flag to emit extended rows
    "cast" => true,  //flag to disable data casting if false
    "limit" => null,  // integer limit of rows to return
]);
```

Additional methods and functionality

```
$table->headers()  // ["first_name", "last_name", "order"]
$table->save("output.csv")  // iterate over all the rows and save the to a csv file
$table->schema()  // get the Schema object
$table->read()  // returns all the data as an array
```

### Schema

[](#schema)

Schema class provides helpful methods for working with a table schema and related data.

`use frictionlessdata\tableschema\Schema;`

Schema objects can be constructed using any of the following:

- php array (or object)

```
$schema = new Schema([
    'fields' => [
        [
            'name' => 'id', 'title' => 'Identifier', 'type' => 'integer',
            'constraints' => [
                "required" => true,
                "minimum" => 1,
                "maximum" => 500
            ]
        ],
        ['name' => 'name', 'title' => 'Name', 'type' => 'string'],
    ],
    'primaryKey' => 'id'
]);
```

- string containing json

```
$schema = new Schema("{
    \"fields\": [
        {\"name\": \"id\"},
        {\"name\": \"height\", \"type\": \"integer\"}
    ]
}");
```

- string containg value supported by [file\_get\_contents](http://php.net/manual/en/function.file-get-contents.php)

```
$schema = new Schema("https://raw.githubusercontent.com/frictionlessdata/testsuite-extended/ecf1b2504332852cca1351657279901eca6fdbb5/datasets/synthetic/schema.json");
```

The schema is loaded, parsed and validated and will raise exceptions in case of any problems.

access the schema data, which is ensured to conform to the specs.

```
$schema->missingValues(); // [""]
$schema->primaryKey();  // ["id"]
$schema->foreignKeys();  // []
$schema->fields(); // ["id" => IntegerField, "name" => StringField]
$field = $schema->field("id");  // Field object (See Field reference below)
```

validate function accepts the same arguemnts as the Schema constructor but returns a list of errors instead of raising exceptions

```
// validate functions accepts the same arguments as the Schema constructor
$validationErrors = Schema::validate("http://invalid.schema.json");
foreach ($validationErrors as $validationError) {
    print(validationError->getMessage();
};
```

validate and cast a row of data according to the schema

```
$row = $schema->castRow(["id" => "1", "name" => "First Name"]);
```

will raise exception if row fails validation

it returns the row with all native values

```
$row  // ["id" => 1, "name" => "First Name"];
```

validate the row to get a list of errors

```
$schema->validateRow(["id" => "foobar"]);  // ["id is not numeric", "name is required" .. ]
```

Infer schema based on source data:

```
$schema = Schema::infer("tests/fixtures/data.csv");
$table->schema()->fields();  // ["first_name" => StringField, "last_name" => StringField, "order" => IntegerField]
```

You can also create a new empty schema for editing

```
$schema = new Schema();
```

set fields

```
$schema->fields([
    "id" => (object)["type" => "integer"],
    "name" => (object)["type" => "string"],
]);
```

appropriate Field object is created according to the given descriptor (see below for Field class reference)

```
$schema->field("id");  // IntegerField object
```

add / update or remove fields

```
$schema->field("email", ["type" => "string", "format" => "email"]);
$schema->field("name", ["type" => "string"]);
$schema->removeField("name");
```

set or update other table schema attributes

```
$schema->primaryKey(["id"]);
```

after every change - schema is validated and will raise Exception in case of validation errors

Finally, you can get the full validated descriptor

```
$schema->fullDescriptor();
```

And, save it to a json file

```
$schema->save("my-schema.json");
```

### Field

[](#field)

Field class represents a single table schema field descriptor

Create a field from a descriptor

```
use frictionlessdata\tableschema\Fields\FieldsFactory;
$field = FieldsFactory::field([
    "name" => "id", "type" => "integer",
    "constraints" => ["required" => true, "minimum" => 5]
]);
```

Cast and validate values using the field

```
$field->castValue("3");  // exception: value is below minimum
$field->castValue("7");  // 7
```

Additional method to access field data

```
$field("id")->format();  // "default"
$field("id")->name();  // "id"
$field("id")->type(); // "integer"
$field("id")->constraints();  // (object)["required"=>true, "minimum"=>1, "maximum"=>500]
$field("id")->enum();  // []
$field("id")->required();  // true
$field("id")->unique();  // false
$field("id")->title();  // "Id" (or null if not provided in descriptor)
$field("id")->description();  // "The ID" (or null if not provided in descriptor)
$field("id")->rdfType();  // "http://schema.org/Thing" (or null if not provided in descriptor)
```

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

[](#contributing)

Please read the contribution guidelines: [How to Contribute](CONTRIBUTING.md)

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance51

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~178 days

Recently: every ~321 days

Total

18

Last Release

295d ago

Major Versions

v0.2.0 → v1.0.02021-08-18

PHP version history (4 changes)v0.1.0PHP &gt;=7

v0.1.2PHP &gt;=5.4

v0.1.8PHP &gt;=5.6

v0.2.0PHP &gt;=7.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/33b6bb5c2a2ef2cf758cb86705891b08d72d74e083a593fd057de4345d17a312?d=identicon)[OriHoch](/maintainers/OriHoch)

![](https://www.gravatar.com/avatar/a32730de9fb7d601730c3149bd533322afe34bbcd1aa53be6985cdece21ae65e?d=identicon)[courtney-miles](/maintainers/courtney-miles)

---

Top Contributors

[![OriHoch](https://avatars.githubusercontent.com/u/1198854?v=4)](https://github.com/OriHoch "OriHoch (47 commits)")[![courtney-miles](https://avatars.githubusercontent.com/u/2454544?v=4)](https://github.com/courtney-miles "courtney-miles (36 commits)")[![roll](https://avatars.githubusercontent.com/u/557395?v=4)](https://github.com/roll "roll (10 commits)")[![pwalsh](https://avatars.githubusercontent.com/u/791678?v=4)](https://github.com/pwalsh "pwalsh (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/frictionlessdata-tableschema/health.svg)

```
[![Health](https://phpackages.com/badges/frictionlessdata-tableschema/health.svg)](https://phpackages.com/packages/frictionlessdata-tableschema)
```

###  Alternatives

[ashallendesign/short-url

A Laravel package for creating shortened URLs for your web apps.

1.4k1.9M4](/packages/ashallendesign-short-url)[getdkan/dkan

DKAN Open Data Catalog

385135.4k2](/packages/getdkan-dkan)[intervention/zodiac

Zodiac Sign Calculator

58191.7k](/packages/intervention-zodiac)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40129.3k2](/packages/erlandmuchasaj-laravel-gzip)[solspace/craft-calendar

The most powerful event management and calendaring plugin!

1830.8k1](/packages/solspace-craft-calendar)

PHPackages © 2026

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