public function __construct($title = '', $description = '', $url = '', $picUrl = '')
{
if (is_array($title)) {
$articles = $title;
if (array_keys($articles) !== range(0, count($articles) - 1)) {
//第一个参是关联数组
$articles = array($articles);
} else {
//第一个参是索引数组
foreach ($articles as $key => $item) {
if ($item instanceof News) {
$articles[$key] = $item->toArray();
} else {
$articles[$key] = (array) $item;
}
}
}
} else {
//第一个参数不是数组
$articles = array(compact(array('title', 'description', 'url', 'picUrl')));
}
//将key的首字母转为大写(微信Xml格式首字大写)
foreach ($articles as $key => $value) {
$temp = array();
foreach ($value as $k => $v) {
$temp[ucfirst($k)] = $v;
}
$articles[$key] = $temp;
}
$this->attributes = array('ArticleCount' => count($articles), 'Articles' => $articles);
}