openbuildings/jam-monetary 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

openbuildings/jam-monetary

Composer 安装命令:

composer require openbuildings/jam-monetary

包简介

A Jam Field to transparently use 'monetary' as a jam field, and have currency exchange arithmetic out of the box

README 文档

README

A Jam Field to transparently use "monetary" as a Jam field, and have currency exchange arithmetic out of the box

Build Status Scrutinizer Quality Score Code Coverage Latest Stable Version

Usage

In your model, define the field as usual:

class Model_Product extends Jam_Model {

	static public function initialize(Jam_Meta $meta)
	{
		$meta
			->fields(array(
				'id' => Jam::field('primary'),
				'name' => Jam::field('string'),
				'price' => Jam::field('price', array('default_currency' => 'GBP')),
				'discount_price' => Jam::field('price'),
			));
	}
}

And to use it you can:

$product = Jam::build('product', array('price' => 10));

echo $product->price->amount();         // will output 10.00 float
echo $product->price->currency();       // will output GBP - the price currency
echo $product->price;                   // will output '10.00'
echo $product->price->as_string();      // will output '10.00'
echo $product->price->as_string('USD'); // will output '10.00'
echo $product->price->in('USD');        // will output 18.12 float
echo $product->price->humanize();       // will output '£10.00'
echo $product->price->humanize('USD');  // will output '$18.12'
echo $product->price->as_html();        // will output '£10.00'
echo $product->price->as_html('USD');   // will output '$18.12'
echo $product->price->as_html('EUR');   // will output '€18.12'

// Price arithmetic
$product->price->add(10);

// Add 2 prices together, doing the exchange conversion arithmetic on the fly
$product->price->add(new Jam_Price(20, 'EUR'));

// Adding more than one price
$product->price->add(new Jam_Price(20, 'EUR'), new Jam_Price(10, 'GBP'), 12.32);

Methods

  • in($currency) : display the amount in the specified currency, put through number_format with 2 digits after the dot
  • as_string($currency = NULL) : return the number_format() on the price's amount, with 2 digits after the dot.
  • humanize($currency = NULL) : display the amount with showing the proper currency sign in the correct position
  • as_html($currency = NULL) : same as humanize(), but with HTML entities support
  • add(... prices) : add one or more price values to this price (you can add negative prices in order to substract)

Automatic currency and monetary values

If the model has a method currency(), then each time a price object is requested, the result of this method is used for the price currency. That will allow you storing the currency alongside the amount iteself in the model

The same goes for a monetary() method - if its there in the model, then it'll be used for all the conversions.

Validators

There are 2 out of the box validator rules - one for currency and one for price.

The price rule is basically a numeric rule, which performes the checks on the price's amount.

The currency validator is a choice validator, with the currencies of the world preselected in the "in" variable.

class Model_Product extends Jam_Model {

	static public function initialize(Jam_Meta $meta)
	{
		$meta
			->fields(array(
				'id' => Jam::field('primary'),
				'price' => Jam::field('price'),
				'currency' => Jam::field('string'),
			))
			->validator('price' => array('price' => array('greater_than' => 10)))
			->validator('currency' => array('currency' => TRUE));
	}
}

License

Copyright (c) 2012-2013, OpenBuildings Ltd. Developed by Ivan Kerin as part of clippings.com

Under BSD-3-Clause license, read LICENSE file.

openbuildings/jam-monetary 适用场景与选型建议

openbuildings/jam-monetary 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 170.32k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2013 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「monetary」 「money」 「jam」 「price」 「kohana」 「exchange」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 openbuildings/jam-monetary 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 openbuildings/jam-monetary 我们能提供哪些服务?
定制开发 / 二次开发

基于 openbuildings/jam-monetary 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 170.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 18
  • 依赖项目数: 4
  • 推荐数: 3

GitHub 信息

  • Stars: 0
  • Watchers: 10
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2013-09-05