PEAR :: Structures_DataGrid列格式化程序的建议

This posting outlines a proposal for an addition to the PEAR package Structures_DataGrid. More specifically it concerns the formatter method of the Structures_DataGrid_Column class.

这篇文章概述了添加PEARStructures_DataGrid的建议。 更具体地说,它涉及Structures_DataGrid_Column类的格式化程序方法

First off, great package! Many thanks to the authors Andrew Nagy [Wishlist] and Olivier Guilyardi!

首先,很棒的包装! 非常感谢作者Andrew Nagy [愿望清单]和Olivier Guilyardi

I was trying to use the package to create a listing of database data (a list of users actually) , with a link to delete a record next to each data row. So I needed a custom column that doesn't exist in in the database and in order to create it I needed to use a callback function as a formatter of the column. Following the examples in the documentation I did:

我试图使用该程序包创建数据库数据列表(实际上是用户列表),并在每个数据行旁边添加一个删除记录的链接。 因此,我需要一个数据库中不存在的自定义列,为了创建它,我需要使用回调函数作为该列的格式化程序。 按照文档中的示例进行操作:

<?php
$column =& new Structures_DataGrid_Column('&nbsp;');
$column->formatter = 'getDeleteLink(link_text=Delete)';
?>

This is a strange syntax to define a callback function, with the brackets and the way parameters are given. But I can live with it, having in mind all the benefits I gain from using the package.

这是定义回调函数的奇怪语法,带有括号和给出参数的方式。 但我可以忍受它,并牢记使用该软件包所获得的所有好处。

Next, I wanted the getDeleteLink function to be part of my User class, the same class that contained the listing() method, which is where I create an use the DataGrid object. So I tried something along the lines of

接下来,我希望getDeleteLink函数成为我的User类的一部分,它是包含listing()方法的同一类,在该类中,我创建了一个使用DataGrid对象的地方。 所以我尝试了一些类似的方法

<?php
array($this, 'getDeleteLink')
?>

This is the usual way to use the callback pseudo type in PHP when you've in a class whose method you want to call back. It didn't work, but I found another solution. If the callback method is static, I can use the syntax to call the method statically:

当您要调用其方法的类时,这是在PHP中使用回调伪类型的通常方法。 它没有用,但是我找到了另一个解决方案。 如果回调方法是静态的,那么我可以使用语法来静态地调用该方法:

<?php
$column =& new Structures_DataGrid_Column('&nbsp;');
$column->formatter = 'User::getDeleteLink(link_text=Delete)';
?>

This is still not so intuitive way of defining a callback function but it worked and this is the important thing.

这仍然不是定义回调函数的直观方法,但是它起作用了,这很重要。

OK, then for my one of the columns I wanted to call back htmlentities(), just in case someone really wants the username "I'm an <script>var XSSGuy;</script>" 😉 I tried quite a few way to make it work, but I couldn't. In the end, the formatter() method always adds the current record as a parameter to the function being called back and htmlentities() won't accept an array. I guess I could wrap htmlentities() in another static method in my class and call the new method, but seemed like such a waste. In addition I saw a @todo comment that says the formatter() needs a revision, so I went ahead.

好的,然后在我想回叫htmlentities()的那一列中,以防万一有人真的想要用户名“我是<script> var XSSGuy; </ script>”“我尝试了多种方法使它工作,但我做不到。 最后, formatter()方法始终将当前记录作为参数添加到要回调的函数中,而htmlentities()将不接受数组。 我想我可以在类中的另一个静态方法中包装htmlentities()并调用新方法,但这似乎很浪费。 另外,我看到了@todo注释,其中说formatter()需要修订,所以我继续进行。

What I ended up having is much shorter measured in lines of code that the original implementation. Also it is more flexible and intuitive, following the usual way a callback function is defined.

我最终得到的结果比原始实现的代码行要短得多。 遵循定义回调函数的常用方法,它也更加灵活和直观。

Also with my implementation I could use the array($this, 'method') syntax.

同样在我的实现中,我可以使用array($ this,'method')语法。

A step back. What are we looking for in the formatter method? It should give the ability to use:

退后一步。 我们在格式化程序中寻找什么? 它应该具有使用以下功能:

  • A function with no parameters.

    没有参数的函数。
  • A function with one parameter which is the value of the current column of the data row (the most common use probably)

    具有一个参数的函数,该参数是数据行当前列的值(可能是最常用的用法)
  • A function that accepts the whole data row (record) as a parameter

    接受整个数据行(记录)作为参数的函数
  • A function that accepts the whole data row as a parameter plus any number and type of other parameters

    该函数接受整个数据行作为参数以及任意数量和类型的其他参数

OK, that's for the parameters. Now for the function itself. It could be:

好的,那是参数。 现在针对函数本身。 它可能是:

  • A stand-alone, old-procedural-school function

    独立的旧程序学校功能
  • A static method of a class

    类的静态方法
  • A non-static method of a class

    类的非静态方法

And finally we should be able to call the function inside and outside of a class.

最后,我们应该能够在类的内部和外部调用该函数。

Follows a list of valid ways to define a formatter function with my implementation.

遵循在我的实现中定义格式化程序功能的有效方法列表。

<?php
// This will call myFunction passing the
// current column record as a parameter
array('myFunction')
// This is also the syntax to call a function
// without any parameters.
// It will happen in case the column is not mapped
// to a table column
// (the second parameter of the
// Structures_DataGrid_Column constructor)
// To be exact, this second use will call the
// callback function with FALSE parameter
// PHP allows passing parameters to functions
// that don't expect any

// This will pass the current record
// as an array with key 'record'
// The key 'record' is backwards-compatible
// with the way it's currently working
array('myFunction', true)


// This allows for any number if parameters to be
// passed in addition to the
// current record, which is still
// identified by its 'record' key
array('myFunction',
        array(
            'param1' => 'value1',
            'param2' => 'value2'
        )
     )


// For the call from within a class or
// outside of it, this example shows all valid calls
class myClass {
    
    function blah(){}
    
    function dg(){
        $column =& new Structures_DataGrid_Column('&nbsp;');
        
        // static
        $column->formatter(
            array(
                array('myClass', 'blah')
            )
        );
        
        // non-static, using $this instance
        $column->formatter(
            array(
                array($this, 'blah')
            )
        );
    
    }
}

// non-static using an instance of the class, outside of it
$column =& new Structures_DataGrid_Column('&nbsp;');
$myInstance = new MyClass();

$column->formatter = array(array($myInstance, 'blah'));
?>

The callback function is always defined as an array, this is in case the authors of the package are committed to the API and doesn't want to change it. Currently the formatter is a string, so a simple check is_array() can serve is a switch between the old and the new implementation.

回调函数始终定义为数组,以防包的作者提交给API且不希望对其进行更改。 当前,格式化程序是一个字符串,因此简单的检查is_array()可以在新旧实现之间进行切换。

Finally, here's the actual implementation of the formatter() method. It may be disappointingly short, after this whole long posting 😉

最后,这是formatter()方法的实际实现。 在整个漫长的发布之后,可能会令人失望的简短short

<?php
class Structures_DataGrid_Column
{
// ...
    function formatter($record)
    {
        
        if (empty($this->formatter[0])) {
            return false;
        }
        
        $parameters = false;
        if (empty($this->formatter[1])) {
            if (!empty($this->fieldName)){
                $parameters = $record[$this->fieldName];
            }
        } else {
            $parameters = array('record' => $record);
            if (!is_bool($this->formatter[1])) {
                $parameters = array_merge(array('record' => $record), $this->formatter[1]);
            }
        }
        
        return call_user_func($this->formatter[0], $parameters);
    }

...
}
?>
        
        

Tell your friends about this post on Facebook and Twitter

FacebookTwitter上告诉您的朋友有关此帖子的信息

翻译自: https://www.phpied.com/structures-datagrid-column-formatter/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值