예제 #1
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'Location_X', 'Location_Y', 'Scale', 'Label', 'MsgId');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #2
0
파일: Image.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'PicUrl', 'MediaId', 'MsgId');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     $validtated = $validator->validate($options);
     $this->setOptions($validtated);
 }
예제 #3
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'Event', 'Latitude', 'Longitude', 'Precision');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     $validtated = $validator->validate($options);
     $this->setOptions($validtated);
 }
예제 #4
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'Title', 'Description', 'Url', 'MsgId');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #5
0
파일: Music.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'Music');
     $validator = new OptionValidator();
     $validator->setDefined($required)->setRequired($required)->setAllowedTypes('Music', array('array'));
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #6
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'Event', 'EventKey', 'Ticket');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #7
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'Event');
     $defined = array_merge($required, array('EventKey'));
     $validator = new OptionValidator();
     $validator->setRequired($required)->setDefined($defined);
     $validtated = $validator->validate($options);
     $this->setOptions($validtated);
 }
예제 #8
0
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'MsgType', 'MediaId', 'Format', 'MsgId');
     $defined = array_merge($required, array('Recognition'));
     $validator = new OptionValidator();
     $validator->setDefined($defined)->setRequired($required);
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #9
0
 /**
  * 构造方法
  */
 public function __construct(Wechat $wechat, array $options)
 {
     $this->wechat = $wechat;
     $required = array('access_token', 'expires_in', 'refresh_token', 'openid', 'scope');
     $defined = array_merge($required, array('unionid'));
     $validator = new OptionValidator();
     $validator->setDefined($defined)->setRequired($required);
     $validated = $validator->validate($options);
     parent::__construct($validated);
 }
예제 #10
0
파일: Video.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'Video');
     $validator = new OptionValidator();
     $validator->setDefined($required)->setRequired($required)->setAllowedTypes('Video', array('array'))->setAllowedValues('Video', function ($value) {
         if (!array_key_exists('MediaId', $value)) {
             throw new \InvalidArgumentException('Undefined index: MediaId');
         }
         return true;
     });
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #11
0
파일: Wechat.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(array $options)
 {
     $certNormalizer = function ($options, $value) {
         // authenticate_cert 为一个 包含 cert 和 key 的数组
         if (!(array_key_exists('cert', $value) && array_key_exists('key', $value))) {
             throw new \InvalidArgumentException('Authenticate_cert cert/key is required');
         }
         // authenticate_cert 中的 cert 和 key 必需为已存在的文件
         if (!(file_exists($value['cert']) && file_exists($value['key']))) {
             throw new \InvalidArgumentException('Authenticate_cert cert/key is invalid file');
         }
     };
     $validator = new OptionValidator();
     $validator->setDefined(array('appid', 'appsecret', 'mchid', 'mchkey', 'authenticate_cert'))->setRequired(array('appid', 'appsecret'))->setAllowedTypes('authenticate_cert', 'array')->setNormalizer('authenticate_cert', $certNormalizer);
     $options = $validator->validate($options);
     parent::__construct($options);
 }
예제 #12
0
파일: User.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(AccessToken $accessToken)
 {
     if (!$accessToken->isValid()) {
         $accessToken->refresh();
     }
     $request = Http::get(self::USERINFO_URL, array('query' => array('access_token' => $accessToken['access_token'], 'openid' => $accessToken['openid'])));
     $response = $request->json();
     if (array_key_exists('errcode', $response) && array_key_exists('errmsg', $response)) {
         throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
     }
     $required = array('openid', 'nickname', 'sex', 'language', 'city', 'province', 'country', 'headimgurl');
     $defined = array_merge($required, array('privilege', 'unionid'));
     $validator = new OptionValidator();
     $validator->setDefined($defined)->setRequired($required);
     $validated = $validator->validate($response);
     parent::__construct($validated);
 }
예제 #13
0
 /**
  * 检测本次请求是有有效
  */
 public function isValid()
 {
     if (empty($this->options)) {
         $this->error = 'Invalid Request';
         return false;
     }
     $required = array('appid', 'bank_type', 'cash_fee', 'fee_type', 'is_subscribe', 'mch_id', 'nonce_str', 'openid', 'out_trade_no', 'result_code', 'return_code', 'sign', 'time_end', 'total_fee', 'trade_type', 'transaction_id');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     try {
         $validator->validate($this->options);
     } catch (\InvalidArgumentException $e) {
         $this->error = $e->getMessage();
         return false;
     }
     return true;
 }
예제 #14
0
 /**
  * 检测本次请求是有有效
  */
 public function isValid()
 {
     if (empty($this->options)) {
         $this->error = 'Invalid Request';
         return false;
     }
     $required = array('appid', 'openid', 'mch_id', 'is_subscribe', 'nonce_str', 'product_id', 'sign');
     $validator = new OptionValidator();
     $validator->setRequired($required);
     try {
         $validator->validate($this->options);
     } catch (\InvalidArgumentException $e) {
         $this->error = $e->getMessage();
         return false;
     }
     return true;
 }
예제 #15
0
파일: Cash.php 프로젝트: SummerMo/wechat
 /**
  * 构造方法
  */
 public function __construct(Wechat $wechat, array $options)
 {
     if (!$wechat->offsetExists('mchid')) {
         throw new \InvalidArgumentException('The required options "mch_id" are missing.');
     }
     if (!$wechat->offsetExists('mchkey')) {
         throw new \InvalidArgumentException('The required options "mch_key" are missing.');
     }
     if (!$wechat->offsetExists('authenticate_cert')) {
         throw new \InvalidArgumentException('The required options "authenticate_cert" are missing.');
     }
     $defaults = array('wxappid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'nonce_str' => uniqid(), 'client_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0');
     $validator = new OptionValidator();
     $validator->setDefined($this->required)->setRequired($this->required)->setDefaults($defaults);
     $validated = $validator->validate($options);
     $this->wechat = $wechat;
     $this->options = $validated;
 }
예제 #16
0
 /**
  * 构造方法
  */
 public function __construct(Wechat $wechat, array $options)
 {
     if (!$wechat->offsetExists('mchid')) {
         throw new \InvalidArgumentException('The required options "mchid" are missing.');
     }
     if (!$wechat->offsetExists('mchkey')) {
         throw new \InvalidArgumentException('The required options "mchkey" are missing.');
     }
     $defaults = array('appid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'time_stamp' => time(), 'nonce_str' => uniqid());
     $validator = new OptionValidator();
     $validator->setDefined($this->required)->setRequired($this->required)->setDefaults($defaults);
     $options = $validator->validate($options);
     // 按 ASCII 码排序
     ksort($options);
     $signature = http_build_query($options);
     $signature = urldecode($signature);
     $signature = strtoupper(md5($signature . '&key=' . $wechat['mchkey']));
     $options['sign'] = $signature;
     $this->options = $options;
 }
예제 #17
0
파일: News.php 프로젝트: kevan/wechat-1
 /**
  * 构造方法
  */
 public function __construct(array $options = array())
 {
     $required = array('ToUserName', 'FromUserName', 'CreateTime', 'ArticleCount', 'Articles');
     $validator = new OptionValidator();
     $validator->setDefined($required)->setRequired($required)->setAllowedTypes('Articles', array('array'))->setAllowedValues('Articles', function ($value) {
         if (!array_key_exists('item', $value)) {
             throw new \InvalidArgumentException('Undefined index: item');
         }
         if (!is_array($value['item'])) {
             throw new \InvalidArgumentException('Articles item must be array');
         }
         if (count($value['item']) > 10) {
             throw new \InvalidArgumentException('Articles item must be Less than 10');
         }
         return true;
     })->setDefault('ArticleCount', function (Options $options) {
         return count($options['Articles']['item']);
     });
     $validtated = $validator->validate($options);
     parent::__construct($validtated);
 }
예제 #18
0
 /**
  * 构造方法
  */
 public function __construct(Wechat $wechat, array $options)
 {
     if (!$wechat->offsetExists('mchid')) {
         throw new \InvalidArgumentException('The required options "mch_id" are missing.');
     }
     if (!$wechat->offsetExists('mchkey')) {
         throw new \InvalidArgumentException('The required options "mch_key" are missing.');
     }
     $normalizer = function ($options, $value) {
         if ($value === 'JSAPI' && !isset($options['openid'])) {
             throw new \InvalidArgumentException('The required options "openid" are missing.');
         }
         return $value;
     };
     $defaults = array('appid' => $wechat['appid'], 'mch_id' => $wechat['mchid'], 'spbill_create_ip' => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0', 'trade_type' => current($this->tradeType), 'nonce_str' => uniqid());
     $validator = new OptionValidator();
     $validator->setDefined($this->defined)->setRequired($this->required)->setAllowedValues('trade_type', $this->tradeType)->setNormalizer('trade_type', $normalizer)->setDefaults($defaults);
     $validated = $validator->validate($options);
     $this->wechat = $wechat;
     $this->options = $validated;
 }