PHPackages                             berlinonline/dat0r - 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. berlinonline/dat0r

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

berlinonline/dat0r
==================

Library for generating data-objects.

0.6(13y ago)87461[6 issues](https://github.com/berlinonline/Dat0r/issues)MITPHPPHP &gt;=5.3

Since Jan 30Pushed 10y ago4 watchersCompare

[ Source](https://github.com/berlinonline/Dat0r)[ Packagist](https://packagist.org/packages/berlinonline/dat0r)[ Docs](https://github.com/berlinonline/Dat0r)[ RSS](/packages/berlinonline-dat0r/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (4)Versions (11)Used By (0)

Dat0r
=====

[](#dat0r)

Unit TestsCoverageDependenciesVersionPHP-FIG[![Build Status](https://camo.githubusercontent.com/d1e65ee13f7b3dad2c361f06dba0d1c70b039c18b1da7bcacac63981dc030748/68747470733a2f2f7472617669732d63692e6f72672f6265726c696e6f6e6c696e652f44617430722e706e67)](https://travis-ci.org/berlinonline/Dat0r)[![Coverage Status](https://camo.githubusercontent.com/366641f7f746103304febda580ff456aedb97a3f63a9b91ae68029a1a9f0ff8c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6265726c696e6f6e6c696e652f44617430722f62616467652e706e67)](https://coveralls.io/r/berlinonline/Dat0r)[![Dependency Status](https://camo.githubusercontent.com/6895b446e7f258141b4eb5be46e30b5f0af87c952d294ee38b35536cf9414109/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3532383465653837363332626163353031643030303132622f62616467652e706e67)](https://www.versioneye.com/user/projects/5284ee87632bac501d00012b)dev-masterpsr-0, psr-1 and psr-2Purpose
-------

[](#purpose)

When crafting code we think that it is essential to correctly distinguish and separate concerns. We believe that decoupled components are less painfull to maintain and easier to reuse.

Dat0r is a code-generation library that was built to ease our management of domain specific data-objects in php. The main difference to existing php solutions, that allow generating code to handle data structures is, that Dat0r is not an ORM and it doesn't implement any other concerns than mere data definition and containment. It allows to define data-structures in xml and then realizes them via code generation. Besides holding (complexly) structured data, we needed two more concerns to be taken care of.

- Ensure value consistency - we don't like broken data.
- Provide some kind of inheritance to allow reuse of structure definitions.

Consistency is achieved by field specific validation of values. Values are only set if validation succeeds, so data is most surely always held as defined. The xml markup for defining data-structures supports nested structures and exposes a classical inheritance model, so you can reuse and extend structures in a way that most probally is familiar.

Requirements and installation
-----------------------------

[](#requirements-and-installation)

This library can be used by integrating it via composer.

Add composer:

```
curl -s http://getcomposer.org/installer | php
```

Create a 'composer.json' file with the following content:

```
{
    "require": {
        "berlinonline/Dat0r": "dev-master"
    }
}
```

Then install via composer:

```
php composer.phar install
```

To verify that Dat0r has been correctly installed and is working, run:

```
./vendor/bin/dat0r.make test
```

Usage
-----

[](#usage)

### 1. Define

[](#1-define)

After installation you are ready to write your first data-definition. Below you will find an example for the definition of a simple article object.

*article\_module.xml*:

```

        Articles hold news related content
        and basically consist of a title, a paragraph and a teaser text.

            Holds an article's title.

            Holds an article's teaser.

            Holds an article's paragraph.

            Holds an article's slug.

            /^[a-z0-9-]+$/

```

### 2. Generate

[](#2-generate)

The next step after defining our desired data structure is to generate the corresponding code. Before the code generation can be kicked off, we need to create a little config file in order to control a few aspects of code generation. The contents of the config file we are using for this example is listed below.

*codegen\_config.ini*:

```
; Tell Dat0r where we want code to be generated to before deploying.
cacheDir=./codegen_cache

; Tell Dat0r where we want generated code to be deployed.
deployDir=./data_objects

; Tell whether we want generated code to completely moved or just copied
; from our cache to the deploy dir. Valid values are 'copy' or 'move'.
deployMethod=move
```

Then make sure, that the directories which we configured above actually exist:

```
mkdir data_objects codegen_cache
```

To then actually generate the code we run:

```
./vendor/bin/dat0r.console generate_code --config codegen_config.ini --schema article_module.xml
```

This should result within an Article folder being created inside our ./data\_objects directory, which should now correspond to the directory tree show below.

*expected structure of the `data_objects` directory:*

```
data_objects/
`-- Article
    |-- ArticleDocument.php
    |-- ArticleModule.php
    `-- Base
        |-- ArticleDocument.php
        `-- ArticleModule.php

```

### 3. Use

[](#3-use)

After generating the code we are now ready to make profit by using it. :) As shown in the above file tree, two concrete and two abstract classes have been generated from our definition. In order to get those classes autoloaded we will need to create a small autoload file for our example.

*autoload.php:*

```
