• Laravel Fluent类分析


    <?php namespace Laravel;
    
    /**
     * 从一个数组中获得值 利用点符号
     *
     * <code>
     *		// 同$array['user']['name']
     *		$name = array_get($array, 'user.name');
     *
     *		// 返回默认的
     *		$name = array_get($array, 'user.name', 'Taylor');
     * </code>
     *
     * @param  array   $array
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    function array_get($array, $key, $default = null)
    {
    	if (is_null($key)) return $array; # 关键字为空
    
    	// To retrieve the array item using dot syntax, we'll iterate through
    	// each segment in the key and look for that value. If it exists, we
    	// will return it, otherwise we will set the depth of the array and
    	// look for the next segment.
    	foreach (explode('.', $key) as $segment) # 把$key分成数组 并且遍历
    	{
    		if ( ! is_array($array) or ! array_key_exists($segment, $array))
    		{
    			return value($default); # 不存在返回默认
    		}
    
    		$array = $array[$segment]; # 递归数组 第二次
    	}
    
    	return $array; # 获得默认值
    }
    
    /**
     * 包装值
     *
     * 如果是闭包,那么返回闭包值
     *
     * @param  mixed  $value
     * @return mixed
     */
    function value($value)
    {
    	return (is_callable($value) and ! is_string($value)) ? call_user_func($value) : $value;
    }
    
    class Fluent {
    
    	/**
    	 * 属性数组
    	 *
    	 * @var array
    	 */
    	public $attributes = array();
    
    	/**
    	 * 创建一个新的Fluent容器实例
    	 *
    	 * <code>
    	 *		带属性创建
    	 *		$fluent = new Fluent(array('name' => 'Taylor'));
    	 * </code>
    	 *
    	 * @param  array  $attributes
    	 * @return void
    	 */
    	public function __construct($attributes = array())
    	{
    		foreach ($attributes as $key => $value)
    		{
    			$this->$key = $value;
    		}
    	}
    
    	/**
    	 * 获得属性
    	 *
    	 * @param  string  $attribute
    	 * @param  mixed   $default
    	 * @return mixed
    	 */
    	public function get($attribute, $default = null)
    	{
    		return array_get($this->attributes, $attribute, $default);
    	}
    
    	/**
    	 * 处理动态设置属性
    	 *
    	 * <code>
    	 *		// 流式设置属性值
    	 *		$fluent->name('Taylor')->age(25);
    	 *
    	 *		// nullable()为布尔
    	 *		$fluent->nullable()->name('Taylor');
    	 * </code>
    	 */
    	public function __call($method, $parameters)
    	{
    		$this->$method = (count($parameters) > 0) ? $parameters[0] : true; # 只获取第一个
    
    		return $this;
    	}
    
    	/**
    	 * 动态获取值
    	 */
    	public function __get($key)
    	{
    		if (array_key_exists($key, $this->attributes))
    		{
    			return $this->attributes[$key];
    		}
    	}
    
    	/**
    	 * 动态设置值
    	 */
    	public function __set($key, $value)
    	{
    		$this->attributes[$key] = $value;
    	}
    
    	/**
    	 * 动态判断是否有值
    	 */
    	public function __isset($key)
    	{
    		return isset($this->attributes[$key]);
    	}
    
    	/**
    	 * 动态删除值
    	 */
    	public function __unset($key)
    	{
    		unset($this->attributes[$key]);
    	}
    
    }
    

    这个只是一个包装类,进行了中间的包装。

  • 相关阅读:
    Git 的版本库创建和修改
    appnium框架以及源码研究
    根据图片的URL生成PDF保存到本地(前台js)
    根据图片的URL生成PDF保存到服务器上(后台C#实现)
    麻烦的控件只读
    利用Javascript生成txt文本文件
    KendoUI AngularJS Bootstrap
    给Grid动态添加列和添加样式
    linq 分组求和的一般方法
    KendoUi学习之旅 Combobox的使用
  • 原文地址:https://www.cnblogs.com/snakevash/p/3045010.html
Copyright © 2020-2023  润新知