<commit_before><?php
/**
 * Qwin Framework
 *
 * @copyright   Copyright (c) 2008-2012 <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Qwin;

require_once 'Widgetable.php';

/**
 * The base class for all widget
 *
 * @package     Qwin
 * @author      <NAME> <<EMAIL>>
 * @todo        global ? shared ? how to defined?
 */
abstract class Widget implements Widgetable
{
    /**
     * Options
     *
     * @var array
     */
    public $options = array(
        'widget' => null,
        'deps' => array(),
    );
    
    /**
     * The widget manager object
     * 
     * @var \Qwin\WidgetManager
     */
    protected $widgetManager;

    /**
     * init widget
     *
     * @param mixed $options 对象的值
     * @return Qwin_Widget 当前对象
     */
    public function __construct(array $options = array())
    {
        $this->option($options);
       
        if (!isset($options['widget']) || empty($options['widget'])) {
            $options['widget'] = WidgetManager::getInstance();
        } elseif (!$options['widget'] instanceof self) {
            throw new \InvalidArgumentException('Option "widget" should be an instance of "' . __CLASS__ . '"');
        }

        $this->widgetManager = &$options['widget'];
    }

    /**
     * Get or set options
     *
     * @param mixed $name option name or options array
     * @param mixed $value
     * @return mixed
     * @example $widget->option('name');            // get "name" option's value
     *          $widget->option('name', 'value');   // set "name" to "value"
     *          $widget->option();                  // get all options
     *          $widget->option(array());           // set options
     * @todo append
     */
    public function option($name = null, $value = null)
    {
        // set options
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                $this->option($k, $v);
            }
            return $this->options;
        }

        if (is_string($name) || is_int($name)) {
            // get option
            if (1 == func_num_args()) {
                $method = 'get' . ucfirst($name) . 'Option';
                if (method_exists($this, $method)) {
                    return $this->$method();
                } else {
                    return isset($this->options[$name]) ? $this->options[$name] : null;
                }
            // set option
            } else {
                $method = 'set' . ucfirst($name) . 'Option';
                if (method_exists($this, $method)) {
                    return $this->$method($value);
                } else {
                    return $this->options[$name] = $value;
                }
            }
        }

        // get all options
        if (null === $name) {
            return $this->options;
        }

        // not match any actions
        return null;
    }

    /**
     * 魔术方法,实现通过方法调用同名微件
     *
     * @param string $name The name of widget
     * @param array $args The arguments for widget's __invoke method
     * @return mixed
     */
    public function __call($name, $args)
    {
        //return call_user_func_array($this->$name, $args);
        return $this->widgetManager->invokeWidget($name, $args, null, $this->option('deps'));
    }

    /**
     * 魔术方法,实现通过对象属性获取同名微件
     *
     * @param string $name The name of widget
     * @return \Qwin\Widget
     */
    public function __get($name)
    {
        return $this->$name = $this->widgetManager->getWidget($name, null, $this->option('deps'));
    }

    // should be implemented by subclasses
    // avoid "Strict standards: Declaration of xxx::__invoke() should be compatible with that of xxx::__invoke()
    //public function __invoke(){}
}
<commit_msg>updated document<commit_after><?php
/**
 * Qwin Framework
 *
 * @copyright   Copyright (c) 2008-2012 <NAME>
 * @license     http://www.opensource.org/licenses/apache2.0.php Apache License
 */

namespace Qwin;

require_once 'Widgetable.php';

/**
 * The base class for all widget
 *
 * @package     Qwin
 * @author      <NAME> <<EMAIL>>
 * @todo        global ? shared ? how to defined?
 */
abstract class Widget implements Widgetable
{
    /**
     * Options
     *
     * @var array
     */
    public $options = array(
        'widget' => null,
        'deps' => array(),
    );
    
    /**
     * The widget manager object
     * 
     * @var \Qwin\WidgetManager
     */
    protected $widgetManager;

    /**
     * Constructor
     *
     * @param array $options The options
     * @return \Qwin\Widget
     */
    public function __construct(array $options = array())
    {
        $this->option($options);
       
        if (!isset($options['widget']) || empty($options['widget'])) {
            $options['widget'] = WidgetManager::getInstance();
        } elseif (!$options['widget'] instanceof self) {
            throw new \InvalidArgumentException('Option "widget" should be an instance of "' . __CLASS__ . '"');
        }

        $this->widgetManager = &$options['widget'];
    }

    /**
     * Get or set options
     *
     * @param mixed $name option name or options array
     * @param mixed $value
     * @return mixed
     * @example $widget->option('name');            // get "name" option's value
     *          $widget->option('name', 'value');   // set "name" to "value"
     *          $widget->option();                  // get all options
     *          $widget->option(array());           // set options
     * @todo append
     */
    public function option($name = null, $value = null)
    {
        // set options
        if (is_array($name)) {
            foreach ($name as $k => $v) {
                $this->option($k, $v);
            }
            return $this->options;
        }

        if (is_string($name) || is_int($name)) {
            // get option
            if (1 == func_num_args()) {
                $method = 'get' . ucfirst($name) . 'Option';
                if (method_exists($this, $method)) {
                    return $this->$method();
                } else {
                    return isset($this->options[$name]) ? $this->options[$name] : null;
                }
            // set option
            } else {
                $method = 'set' . ucfirst($name) . 'Option';
                if (method_exists($this, $method)) {
                    return $this->$method($value);
                } else {
                    return $this->options[$name] = $value;
                }
            }
        }

        // get all options
        if (null === $name) {
            return $this->options;
        }

        // not match any actions
        return null;
    }

    /**
     * 魔术方法,实现通过方法调用同名微件
     *
     * @param string $name The name of widget
     * @param array $args The arguments for widget's __invoke method
     * @return mixed
     */
    public function __call($name, $args)
    {
        //return call_user_func_array($this->$name, $args);
        return $this->widgetManager->invokeWidget($name, $args, null, $this->option('deps'));
    }

    /**
     * 魔术方法,实现通过对象属性获取同名微件
     *
     * @param string $name The name of widget
     * @return \Qwin\Widget
     */
    public function __get($name)
    {
        return $this->$name = $this->widgetManager->getWidget($name, null, $this->option('deps'));
    }

    // should be implemented by subclasses
    // avoid "Strict standards: Declaration of xxx::__invoke() should be compatible with that of xxx::__invoke()
    //public function __invoke(){}
}

{'fraction_non_alphanumeric': 0.14576719576719577, 'fraction_numerical': 0.00291005291005291, 'mean_word_length': 1.7013219006788138, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3581557', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2172, 'n_words': 542}