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+ */
0 commit comments