2017 © Pedro Peláez
 

library clay

image

jorjives/clay

  • Wednesday, January 8, 2014
  • by jorjives
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

clay

Clay is a highly malleable modelling class., (*1)

How it works

Using dot notation with get() and set() methods, clay allows access and processing of any hierarhy of data., (*2)

The smarts comes from the moel map. The map defines how data is processed in the tree/hierarchy. Any branch or leaf can have a set of closures passed to it which handles the data in some way. For example:, (*3)

Map:

root.branch.twig = function { append value }
root.branch.twig.leaf = function { overwrite value only if higher }

Processing of a value is a combination of all processing leading up through the tree. To override processing you can assign a key to each function and override the key with a new function higher up the tree. e.g.:, (*4)

Map

root.branch = function { strtoupper; }
root.branch.twig.leaf = function { strtolower; ucwords; }

Use

set('root.branch.test.[]', 'this is')   # root.branch.test = array('THIS IS')
set('root.branch.test.[]', 'a test')    # root.branch.test = array('THIS IS', 'A TEST')
set('root.branch.twig.leaf', 'example') # root.branch.twig.leaf = 'Example'

Things get interesting with tests for higher or lower, overwriting, and trickle-up values., (*5)

A Clay model can be extended or defined at construction with an array map and data., (*6)

Proposals

Properties should be accessible through standard object notation e.g.:, (*7)

$model->root->branch->twig->leaf[0]->vein = $value;
$value = $model->root->branch->twig->leaf[0]->vein;

I don't have the time to implement these (major) changes right now but they will come in the future., (*8)

Usage

<?php

use Clay\Clay;

$data = array(
    'this' => array(
        'is' => 'just',
        'a' => array(
            'simple',
            'test'
        )
    ),
    'isnt' => 'anything'
);

$clay = new Clay(
    $data,
    array(
        'this.is.a' => array(
            'upper' => function(&$new) { $new = strtoupper($new); return true; }
        ),
        'this.is.a.simple.test' => array(
            'type' => function(&$new) { return !is_numeric($new); },
            'merge' => function(&$new, $current) { return $new = array_merge((array)$current, (array)$new); }
        ),
        'this.is.test.1' => array(
            'convert' => function(&$new) { return $new = preg_replace('/[^\d]+/', '', $new); },
            'type' => function(&$new) { return is_numeric($new); },
            'merge_higher' => function(&$new, $current) {
                if($new <= max((array)$current)) return false;
                return $new = array_merge((array)$current, (array)$new);
            }
        ),
        'this.is.test.2' => array(
            'replace_longer' => function(&$new, $current) {
                if(strlen($new) <= max(array_map('strlen', (array)$current))) return false;
                return $new;
            }
        ),
        'this.is.test.3' => array(
            'numeric_lower' => function(&$new, $current) {
                if(empty($current)) return $new;
                $newnum = preg_replace('/[^\d]+/', '', $new);
                $curnum = preg_replace('/[^\d]+/', '', $current);
                return $new = $newnum < $curnum ? $new : $current;
            }
        )
    )
);

$clay->set('this.is.a.simple.test', '100 pounds');
$clay->set('this.is.a.simple.test', '200 pounds');
$clay->set('this.is.a.simple.test', '300 pounds');
$clay->set('this.is.test.1', '200 pounds');
$clay->set('this.is.test.1', '100 pounds');
$clay->set('this.is.test.1', '300 pounds');
$clay->set('this.is.test.2', '200 pounds');
$clay->set('this.is.test.2', '100 pounds and 50 pence');
$clay->set('this.is.test.2', '300 pounds');
$clay->set('this.is.test.3', '200 pounds');
$clay->set('this.is.test.3', '100 pounds');
$clay->set('this.is.test.3', '300 pounds');

foreach($clay as $k => $v) {
    print_r(array($k => $v));
}

The Versions

08/01 2014

dev-master

9999999-dev

  Sources   Download

The Requires

  • php >=5.3.0