jorjives / clay
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 07:27:15 UTC
README
Clay是一个高度可塑的建模类。
工作原理
使用点表示法与get()和set()方法,Clay允许访问和处理任何层次的数据。
智能来自模型映射。映射定义了在树/层次结构中数据如何处理。任何分支或叶子都可以传递一组闭包来处理数据。例如
####映射: root.branch.twig = function { append value } root.branch.twig.leaf = function { overwrite value only if higher }
值的处理是树中所有处理的组合。要覆盖处理,可以为每个函数分配一个键,并在树的上层覆盖该键。例如
####映射 root.branch = function { strtoupper; } root.branch.twig.leaf = function { strtolower; ucwords; }
####使用 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'
在测试上下文、覆盖和向上传递值时,事情变得更有趣。
Clay模型可以通过数组映射和数据在构造时扩展或定义。
提议
属性应该可以通过标准对象表示法访问,例如
$model->root->branch->twig->leaf[0]->vein = $value;
$value = $model->root->branch->twig->leaf[0]->vein;
我现在没有时间实现这些(重大)更改,但它们将在未来到来。
用法
<?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));
}