<?php
namespace Payment; use Payment\Common\BaseStrategy; use Payment\Common\PayException; use Payment\Helper\Cmb\BindCardHelper; use Payment\Helper\Cmb\PubKeyHelper; class HelperContext { protected $helper; public function initHelper($way, array $config) { try { switch ($way) { case Config::CMB_BIND: $this->helper = new BindCardHelper($config); break; case Config::CMB_PUB_KEY: $this->helper = new PubKeyHelper($config); break; default: throw new PayException('当前仅支持：CMB_BIND CMB_PUB_KEY 操作'); } } catch (PayException $e) { throw $e; } } public function helper(array $data) { if (! $this->helper instanceof BaseStrategy) { throw new PayException('请检查初始化是否正确'); } try { return $this->helper->handle($data); } catch (PayException $e) { throw $e; } } } 