ArrayAccess、trait新特性写接口

config.php

<?php
namespace app\src;
use ArrayAccess;
/**
 * Config
 * 
 * @package   
 * @author QQ43599939
 * @copyright QQ43599939
 * @version 2017
 * @access public
 */
class config implements ArrayAccess
{
    private $configData;
    
    
    public function __construct(array $config = [])
    {
        $this->configData = $config;
    }
    /**
     * Config::offsetExists()
     * 
     * @param mixed $key
     * @return
     */
    public function offsetExists($key)
    {
        return isset($this->configData[$key]);
    }

    /**
     * Config::offsetSet()
     * 
     * @param mixed $key
     * @param mixed $value
     * @return
     */
    public function offsetSet($key, $value)
    {
        $this->configData[$key] = $value;
    }

    /**
     * Config::offsetGet()
     * 
     * @param mixed $key
     * @return
     */
    public function offsetGet($key)
    {
        return $this->configData[$key];
    }

    /**
     * Config::offsetUnset()
     * 
     * @param mixed $key
     * @return
     */
    public function offsetUnset($key)
    {
        unset($this->configData[$key]);
    }
}


factory.php

<?php
namespace app\src;

include __DIR__.DIRECTORY_SEPARATOR.'config.php';
include __DIR__.DIRECTORY_SEPARATOR.'myhttpRequest.php';
include __DIR__.DIRECTORY_SEPARATOR.'myException.php';

/**
 * factory
 * 
 * @package   
 * @author QQ43599939
 * @copyright QQ43599939
 * @version 2017
 * @access public
 */
class factory
{
    use myhttpRequest;
    private $config;

    private $city;

    
    /**
     * factory::__construct()
     * 
     * @param mixed $config
     * @return
     */
    public function __construct(array $config = [])
    {
        $this->config = new config($config);
    }
    
    /**
     * factory::city()
     * 
     * @param mixed $city
     * @return
     */
    public function city($city = '重庆')
    {
        $this->city =  $city;
        return $this;
    }
    
    /**
     * factory::build()
     * 
     * @return
     */
    public function build()
    {
        $headers = array();
        if($this->config['AppCode']=='' || !$this->config['AppCode'])
            throw new myException("AppCode is null!", -1);
        
        if($this->config['Url']=='' || !$this->config['Url'])
            throw new myException("Url is null!", -1);
            
        array_push($headers, "Authorization:APPCODE " . $this->config['AppCode']);
        return $this->getCurl($this->config['Url'].'?city='.$this->city, $headers);
    }
    
    
} 
    



myhttpRequest.php

<?php

namespace app\src;

trait myhttpRequest
{
   
    /**
     * getCurl()
     * 
     * @param mixed $url
     * @param mixed $headers
     * @return
     */
    public function getCurl($url,$headers){
        
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$url, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        return curl_exec($curl);
       
        
    }
}


myException.php

<?php

namespace app\src;

/**
 * myException
 * 
 * @package   
 * @author QQ43599939
 * @copyright QQ43599939
 * @version 2017
 * @access public
 */
 
class myException extends \Exception
{
    /**
     * myException::__construct()
     * 
     * @param mixed $errorMessage
     * @param mixed $errorCode
     * @return
     */
    function  __construct($errorMessage, $errorCode)
    {
        parent::__construct($errorMessage);
        $this->errorMessage = $errorMessage;
        $this->errorCode = $errorCode;
        $this->setErrorType("my");
    }
    
    private $errorCode;
    private $errorMessage;
    private $errorType;
    
    /**
     * myException::getErrorCode()
     * 
     * @return
     */
    public function getErrorCode()
    {
        return $this->errorCode;
    }
    
    /**
     * myException::setErrorCode()
     * 
     * @param mixed $errorCode
     * @return
     */
    public function setErrorCode($errorCode)
    {
        $this->errorCode = $errorCode;
    }
    
    /**
     * myException::getErrorMessage()
     * 
     * @return
     */
    public function getErrorMessage()
    {
        return $this->errorMessage;
    }
    
    /**
     * myException::setErrorMessage()
     * 
     * @param mixed $errorMessage
     * @return
     */
    public function setErrorMessage($errorMessage)
    {
        $this->errorMessage = $errorMessage;
    }
    
    /**
     * myException::getErrorType()
     * 
     * @return
     */
    public function getErrorType()
    {
        return $this->errorType;
    }
    
    /**
     * myException::setErrorType()
     * 
     * @param mixed $errorType
     * @return
     */
    public function setErrorType($errorType)
    {
        $this->errorType = $errorType;
    }
    

}


test.php

<?php
// +----------------------------------------------------------------------
// | Author: qq43599939 <43599939@qq.com-->
// +----------------------------------------------------------------------
namespace app\src;

include __DIR__.DIRECTORY_SEPARATOR.'factory.php';

$params = array(
    'Url' => 'http://jisutqybmf.market.alicloudapi.com/weather/query',
    'AppCode' => 'democode',

);

$factory = new factory($params);
$result_json = $factory->city('重庆')->build();

var_dump(json_decode($result_json,true));


result:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值