OptionsResolver:如何验证未知选项树

时间:2016-09-26 10:08:54

标签: arrays validation symfony

SCENARIO

我正在写一个类来渲染一个纯文本表,一个由数组表示的数据矩阵。

一个简单的数组就是这样:

$data = [
    [
        'title'  => '20.000 Leghe sotto i mari',
        'author' => 'Jules Verne',
        'review'=> 'Una misteriosa creatura marina che affonda navi da guerra di ogni paese...'
    ],
    [
        'title'  => 'Il signore degli anelli: La compagnia dell\'anello',
        'author' => 'J. R. R. Tolkien',
        'review' => 'In questo primo romanzo della trilogia di Tolkien, il lettore conosce gli Hobbit...'
    ],
    [
        'title'  => 'Il leviatano',
        'author' => 'T. Hobbes contraddittorietà',
        'review' => 'Vitale rappresentazione dello Stato moderno, longeva e barocca figura di pensiero politico nelle sembianze di un mostro biblico...'
    ]
];

实际上,每个键都是列的名称,所以,最后,我得到一个这样的表:

+---------------------------------------------------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------+
| 20.000 Leghe sotto i mari                         | Jules Verne                 | Una misteriosa creatura marina che affonda navi da guerra di ogni paese...                                                         |
+---------------------------------------------------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------+
| Il signore degli anelli: La compagnia dell'anello | J. R. R. Tolkien            | In questo primo romanzo della trilogia di Tolkien, il lettore conosce gli Hobbit...                                                |
+---------------------------------------------------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------+
| Il leviatano                                      | T. Hobbes contraddittorietà | Vitale rappresentazione dello Stato moderno, longeva e barocca figura di pensiero politico nelle sembianze di un mostro biblico... |
+---------------------------------------------------+-----------------------------+------------------------------------------------------------------------------------------------------------------------------------+

现在,我希望能够定义每个列的最大宽度,为此,我想传递一个这样的数组:

// The array containing the data
$options = [
    'columns' => [
        'review' => [
            'max_width' => 50
        ]
    ]
];

我定义了一个名为columns的选项(已知选项),我在其中设置了max_width选项(已知选项for the column review`(未知选项)。

我知道columns选项只能是array,而max_width选项只能是integer,但如何验证Symfony的[OptionsResolver组件] 1

这是我目前对选项的实施:

/**
 * @param array $options
 */
private function resolveOptions(array $options = [])
{
    $resolver = new OptionsResolver();
    $resolver->setDefaults([
        // The horizontal separator
        'sep_h' => '-',
        // The vertical separator
        'sep_v' => '|',
        // The cross separator
        'sep_x' => '+',
        // The number of spaces to put on the left and on the right of the content of each cell
        'cells_padding' => [
            // Top padding
            1,
            // Right padding
            1,
            // Bottom padding
            1,
            // Left padding
            1
        ]
    ])
        // This options can be passed or not
        ->setDefined('columns');

    // Set type validation
    $resolver->setAllowedTypes('sep_h', 'string')
        ->setAllowedTypes('sep_v', 'string')
        ->setAllowedTypes('sep_x', 'string')
        ->setAllowedTypes('cells_padding', 'array')
        ->setAllowedTypes('columns', 'array')
        // Here use wrong validation to trigger an error as I'm passing
        // an integer that is the correct value type
        ->setAllowedTypes('max_width', 'string');

    $this->options = $resolver->resolve($options);
}

Full code on GitHub

那么,再次,如何验证这种选项结构?

正如您所看到的,我现在将max_width的可接受值设置为string,并且我传递了正确的integer值:这可能会触发例外,但它不是。我认为,正确的选择是在树中更深入,但我不明白如何检查它。我应该创建大量的子$resolvers来验证选项树的每个节点吗?这是正确的方法,还是我错过了什么?

0 个答案:

没有答案