intro
Zend_Filter_Interfaceを拡張して、配列により値を置き換えるフィルタを作成します。
1.カスタムフィルタの作成
以下のように
library/My/Filter/ArrayValue.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php require_once 'Zend/Filter/Interface.php'; class My_Filter_ArrayValue implements Zend_Filter_Interface { protected $_list = array(); public function __construct($list = array()) { $this->_list = $list; } public function filter($value) { if (isset($this->_list[$value])) { return $this->_list[$value]; } return $value; } } |
2.コントローラの作成
以下のように
application/controllers/FilterController.php
ファイルを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?php require_once 'Zend/Filter/Interface.php'; class My_Filter_ArrayValue implements Zend_Filter_Interface { protected $_list = array(); public function __construct($list = array()) { $this->_list = $list; } public function filter($value) { if (is_array($value)) { foreach($value as &$element) { $element = $this->filter($element); } } else if (isset($this->_list[$value])) { return $this->_list[$value]; } return $value; } } |
3.ビューの作成
以下のように
application/views/script/filter/index.phtml
ファイルを作成します。
1 2 3 4 5 |
<ul> <?php foreach($this->values as $key => $value) : ?> <li><?= $this->of->$key ?></li> <?php endforeach; ?> </ul> |
3.実行結果
ビューには以下のように出力されます。
1 2 3 4 |
* ONE! * MyTableColumn * this is <b>tag</b> * Second |
履歴
日付 | 内容 |
---|---|
2008/4/25 | 公開 |
2008/4/27 | 配列のフィルタに対応しました。 |