Skip to content

Commit 3b3095f

Browse files
committed
first
0 parents  commit 3b3095f

File tree

5 files changed

+293
-0
lines changed

5 files changed

+293
-0
lines changed

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Trait-Orm-Format
2+
3+
* Version: 1.0
4+
5+
## Information
6+
7+
* PHP >= 5.4
8+
* FuelPHP = 1.7/master
9+
10+
## Description
11+
12+
FuelPHPのORMに書式化出力を提供します。
13+
14+
モデルのフィールドに書式を設定し、プロパティアクセスで書式化出力を行う。
15+
* 例)Model_Personのageフィールドについて書式"%d歳"が設定されている場合
16+
17+
echo $person->age // "12"
18+
echo $person->formatted_age //"12歳"
19+
20+
## Install
21+
22+
* git clone https://github.com/goosys/Fuel-Package-TraitOrmFormat.git fuel/packages/trait-orm-format
23+
* vi fuel/app/config.php
24+
25+
always_load =>
26+
packages => 'trait-orm-format',
27+
language => 'trait-orm-format'
28+
29+
* cp fuel/packages/trait-orm-format/lang/ja/trait-orm-format.php fuel/app/lang/ja/trait-orm-format.php
30+
31+
## Example
32+
33+
### Model
34+
35+
* fuel/app/classes/model/person.php
36+
37+
class Model_Person extends \Orm\Model
38+
{
39+
use Trait_Orm_Format;
40+
41+
* var_dump($person);
42+
43+
array(
44+
'name' => 'taro',
45+
'age' => 12,
46+
'birthday' => '19800910',
47+
'gender' => 1, //0:woman,1:man
48+
);
49+
50+
### Lang
51+
52+
* vi fuel/app/lang/ja/trait-orm-format.php
53+
54+
'common' => array(
55+
'jpy' => function($val){ return number_format($val).'円'; },
56+
'percent' => '%d%%',
57+
'timetodate' => function($val){ return date('Y/m/d',$val); },
58+
'strtodate' => function($val){ return date('Y/m/d',strtotime($val)); },
59+
),
60+
61+
//Model_Person
62+
'model.person' => array(
63+
'age' => '%d歳',
64+
'birthday' => 'common.timetodate',
65+
'gender' => 'selector.gender',
66+
),
67+
68+
* vi fuel/app/lang/ja/selector.php
69+
70+
'gender' => array(
71+
0 => '女性',
72+
1 => '男性'
73+
),
74+
'country' => array(
75+
'jp' => '日本',
76+
'us' => 'アメリカ',
77+
'uk' => 'イギリス',
78+
'de' => 'ドイツ',
79+
),
80+
81+
### Output
82+
83+
echo $person->formatted_age; //"12歳"
84+
echo $person->formatted_birthday; //"1980/09/10"
85+
echo $person->formatted_name; //"taro"
86+
echo $person->formatted_gender; //"男性"
87+
88+
## Usage
89+
90+
### String Format
91+
92+
* sprintfの結果を出力
93+
94+
"%d%%" //"20%"
95+
"%.2f%%" //"20.05%
96+
"%s" //"hello"
97+
98+
### Selector Format
99+
100+
* 選択肢から選択した値を出力
101+
102+
"selector.gender" //"女性"
103+
"selector.country" //"日本"
104+
105+
### Functional Format
106+
107+
* Closureの結果を出力
108+
109+
function($val){ return date('Y/m/d',$val); } //$val="1411620061" //"2014/09/25"
110+
function($val){ return number_format($val).'円'; } //$val="200000" //"200,000円"
111+
112+
### Undefined Format
113+
114+
* 未定義なら元のプロパティ値を出力
115+
116+
## Customize
117+
118+
## License
119+
120+
MIT License

bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
Autoloader::add_classes(array(
3+
'Trait_Orm_Format' => __DIR__.'/classes/trait/orm/format.php',
4+
));

classes/trait/orm/format.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
trait Trait_Orm_Format {
4+
5+
/*
6+
public static function __callStatic($method, $args)
7+
{
8+
if (substr($method, 0, 10) == 'formatted_')
9+
{
10+
$value = call_user_func_array(substr($method, 10),$args);
11+
$result = $this->_format(substr($method, 10));
12+
return $result;
13+
}
14+
else
15+
{
16+
return call_user_func_array($method,$args);
17+
}
18+
}
19+
*/
20+
21+
/**
22+
* Allow for getter, setter and unset methods
23+
*
24+
*/
25+
public function & __get($property)
26+
{
27+
if (substr($property, 0, 10) == 'formatted_')
28+
{
29+
$result = $this->_format(substr($property, 10));
30+
return $result;
31+
}
32+
else
33+
{
34+
return $this->get($property);
35+
}
36+
}
37+
38+
protected function _format($property,$format='')
39+
{
40+
$result = '';
41+
if( empty($format) ){
42+
$format = $this->_get_format($property,$this->property($property));
43+
}
44+
45+
return static::format($this->get($property),$format);
46+
}
47+
48+
/**
49+
*
50+
* @param $value
51+
* @param $format: '%d' | 'common.jpn' | 'selector.method' | callable
52+
*/
53+
public function format($value,$format='')
54+
{
55+
$result = $value;
56+
if( !empty($format) && !is_callable($format) ){
57+
$format = static::_get_format('',array('format'=>$format));
58+
}
59+
60+
if( !is_null($value) && is_callable($format) )
61+
{
62+
$result = $format($value);
63+
}
64+
65+
return $result;
66+
}
67+
68+
protected static function _get_format($property,$p=array())
69+
{
70+
//$class = get_called_class();
71+
$class = get_class();
72+
$model = strtolower(substr($class, 6));//cut 'Model_'
73+
$format= '';
74+
75+
if( isset($p['format']) ){
76+
$format = $p['format'];
77+
}
78+
else if( __('trait-orm-format.model.'.$model) ){
79+
$lng = __('trait-orm-format.model.'.$model);
80+
$format = \Arr::get($lng, $property,'%s');
81+
}
82+
83+
if( substr($format, 0, 7) == 'common.')
84+
{
85+
$lng = __('trait-orm-format.common');
86+
$format = \Arr::get( $lng, substr($format,7), false);
87+
}
88+
89+
if( !is_callable($format) && substr($format, 0, 9) == 'selector.')
90+
{
91+
if( __($format) ){
92+
$selector = __($format);
93+
$format = function($val) use($selector) { return \Arr::get($selector,$val); };
94+
}
95+
}
96+
97+
if( !is_null($format) && !is_callable($format) ){
98+
$format2= $format;
99+
$format = function($val) use($format2) { return sprintf($format2,$val); };
100+
}
101+
102+
return ($format)?: false;
103+
}
104+
}
105+
106+
107+
/**
108+
Example:
109+
lang/ja/trait-orm-format.php
110+
'common' => array(
111+
'jpy' => function($val){ return number_format($val).'Yen'; },
112+
'percent' => '%d%%',
113+
),
114+
'model' => array(
115+
'item' => array(
116+
'price' => 'common.jpy',
117+
'tax' => 'common.percent',
118+
'num' => '%dko',
119+
120+
'payment_method' => 'selector.payment_method',
121+
),
122+
)
123+
lang/ja/selector.php
124+
'payment_method' => array(
125+
'' => '--'
126+
'1'=> 'cache',
127+
'2'=> 'card',
128+
)
129+
130+
Example:
131+
<?php echo $item->formatted_price; ?>
132+
<?php echo $item->format($item->price,'common.jpy'); ?>
133+
134+
<?php echo $item->formatted_tax; ?>
135+
<?php echo $item->formatted_num; ?>
136+
<?php echo $item->formatted_payment_method; ?>
137+
138+
*/

lang/en/trait-orm-format.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
/**
4+
*
5+
*/
6+
return array(
7+
);

lang/ja/trait-orm-format.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
*
5+
*/
6+
return array(
7+
8+
/*
9+
'common' => array(
10+
'jpy' => function($val){ return number_format($val).'円'; },
11+
'percent' => '%d%%',
12+
'timetodate' => function($val){ return date('Y/m/d',$val); },
13+
'strtodate' => function($val){ return date('Y/m/d',strtotime($val)); },
14+
),
15+
16+
//Model_Person
17+
'model.person' => array(
18+
'age' => '%d歳',
19+
'birthday' => 'common.timetodate',
20+
'gender' => 'selector.gender',
21+
),
22+
*/
23+
24+
);

0 commit comments

Comments
 (0)