zoukankan      html  css  js  c++  java
  • 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]);
    	}
    
    }
    

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

  • 相关阅读:
    ural 1227 dfs判环&求最长路
    uva 11990 块状链表
    图论·二分图
    图论·双连通分量
    图论·欧拉迹
    组合博弈
    解题策略·状态精简
    组合计数·棋盘统计
    组合计数·图形统计
    组合计数·经典序列问题
  • 原文地址:https://www.cnblogs.com/snakevash/p/3045010.html
Copyright © 2011-2022 走看看