1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
<?php
/**
* Copyright (c) 2017 Baidu, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @desc 卡片的基类
**/
namespace Baidu\Duer\Botsdk\Card;
abstract class BaseCard{
protected $data=[];
protected $supportSetField = [];
/**
* @param array $fields 允许通过魔术方法设置的字段
* @return null
**/
public function __construct($fields=[]) {
$this->supportSetField = $fields;
}
/**
* @desc 为卡片添加cue words 提示用户输入
* @param array|string $arr 比如:['###', '###',...,'###'], 或者'###'
* @return self
**/
public function addCueWords($arr){
if($arr) {
if(is_string($arr)) {
$arr = [$arr];
}
$this->data['cueWords'] = $this->data['cueWords']?$this->data['cueWords']:[];
$this->data['cueWords'] = array_merge($this->data['cueWords'], $arr);
}
return $this;
}
/**
* @desc 设置卡片链接
* @param string $url 比如:http(s)://....
* @param boolean $anchorText 链接显示的文字
* @return self
**/
public function setAnchor($url, $anchorText=''){
if($url) {
$this->data['url'] = $url;
if($anchorText) {
$this->data['anchorText'] = $anchorText;
}
}
return $this;
}
/**
* @param string $key 字段名。如果没有返回整个数据
* @return array|null
**/
public function getData($key=''){
if($key) {
return $this->data[$key];
}
return $this->data;
}
/**
* @desc 魔术方法
* @param string $name
* @param array $arguments
* @return null | $this;
**/
public function __call($name, $arguments){
/**
* 将规定的field 通过setFieldName('content')来设置
**/
$operation = substr($name, 0, 3);
$field = lcfirst(substr($name, 3));
if($operation == 'set' && in_array($field, $this->supportSetField)) {
$this->data[$field] = $arguments[0];
return $this;
}
throw new \Exception("$name function not found");
}
}