harp-orm / range
表示两个整数值的对象
0.2.1
2014-08-28 10:06 UTC
Requires
- php: >=5.4.0
- harp-orm/validate: ~0.4.0
Requires (Dev)
- harp-orm/harp: ~0.3.2
- psr/log: ~1.0
This package is auto-updated.
Last update: 2024-09-21 22:43:29 UTC
README
表示两个整数值的对象
用法
这是一个表示两个值 - 最小值和最大值的对象,可以存储为字符串(如"first_value|second_value")。您可以将其设置为/检索为字符串或数组,如下所示
$range = new Range(5, 10); // Will return 5 echo $range->getMin(); // Will return 10 echo $range->getMax(); // It also implements ArrayAccess echo $range[0]; // 5 echo $range[1]; // 10 $range[0] = 2; $range[1] = 9; // And you can convert it to a short string representation // For example for storing in the DB echo (string) $range; // 2|9 $newRange = Range::fromString('2|9'); // You can "add" ranges together // This will add the min and the max values $range = new Range(5, 10); $range->add(new Range(3, 20)); echo $range; // 8|30 // You can get a human readable version with humanize method $range = new Range(5, 10); echo $range->humanize(); // 5 - 10 // This is also custumizable $range = new Range(5, 10, '%s - / - %s'); echo $range->humanize(); // 5 - / - 10 // You can add a closure to further custumize this $range = new Range(5, 10, function ($min, $max) { return $min.'..'.$max; }); echo $range->humanize(); // 5..10
聚合方法
有几个方法可以用于处理多个范围
$range1 = new Range(5, 10); $range2 = new Range(2, 8); $range3 = new Range(9, 8); // Sum adds all of the ranges together $range = Range::sum([$range1, $range2, $range3], '%s - %s'); echo $range; // 16|26 // Get the maximum values for the first and second value $range = Range::merge([$range1, $range2, $range3], '%s - %s'); echo $range; // 9|10
与模型一起使用
有一个特性可以将处理范围的方法添加到模型中。为了持久化数据,它会在模型中添加 "days" 属性。
use Harp\Harp\AbstractModel; use Harp\Range\DaysRangeTrait; class TestModel extends AbstractModel { use DaysRangeTrait; public static function initialize($config) { DaysRangeTrait::initialize($config); } } $testModel = new TestModel(); // Will get you a Range object $testModel->getDays(); $testModel->setDays(new Range(5, 10)); // Will return "5|10" echo $testModel->days;
许可证
版权 (c) 2014, Clippings Ltd. 由 Ivan Kerin 开发
在 BSD-3-Clause 许可证下,请阅读 LICENSE 文件。