<?php
/*
 *
 * @package		ARI Framework
 * @author		ARI Soft
 * @copyright	Copyright (c) 2011 www.ari-soft.com. All rights reserved
 * @license		GNU/GPL (http://www.gnu.org/copyleft/gpl.html)
 *
 */

(defined('_JEXEC') && defined('ARI_FRAMEWORK_LOADED')) or die('Direct Access to this location is not allowed.');

AriKernel::import('SimpleTemplate.Filters.FilterBase');
AriKernel::import('Utils.Utils');

class AriSimpleTemplatePluckFilter extends AriSimpleTemplateFilterBase
{
    function getFilterName()
    {
        return 'pluck';
    }

    function parse($obj, $key)
    {
        $keys = null;
        if (strpos($key, '.') !== false)
        {
            $keys = explode('.', $key);
        }
        else
        {
            $keys = array($key);
        }

        $value = $obj;
        foreach ($keys as $cKey)
        {
            $value = AriUtils::getParam($value, $cKey, null);

            if (is_null($value))
                break;
        }

        if (is_array($value))
            $value = null;

        return $value;
    }
}

new AriSimpleTemplatePluckFilter();