Make API Typecho_Common::arrayFlatten deprecated, please use array_column instead.

This commit is contained in:
joyqi
2021-08-20 16:42:40 +08:00
parent 687ab6260a
commit 75d5677d7d
10 changed files with 23 additions and 47 deletions
+1 -1
View File
@@ -86,7 +86,7 @@ Typecho_Widget::widget('Widget_Contents_Post_Edit')->to($post);
<ul>
<?php
if ($post->have()) {
$categories = Typecho_Common::arrayFlatten($post->categories, 'mid');
$categories = array_column($post->categories, 'mid');
} else {
$categories = array();
}
+5 -29
View File
@@ -436,39 +436,15 @@ EOF;
}
/**
* 抽取多维数组的某个元素,组成一个新数组,使这个数组变成一个扁平数组
* 使用方法:
* <code>
* <?php
* $fruit = array(array('apple' => 2, 'banana' => 3), array('apple' => 10, 'banana' => 12));
* $banana = Typecho_Common::arrayFlatten($fruit, 'banana');
* print_r($banana);
* //outputs: array(0 => 3, 1 => 12);
* ?>
* </code>
*
* @access public
*
* @param array $value 被处理的数组
* @param string $key 需要抽取的键值
* @param array $value
* @param $key
* @deprecated use array_column instead
*
* @return array
*/
public static function arrayFlatten(array $value, $key)
public static function arrayFlatten(array $value, $key): array
{
$result = [];
if ($value) {
foreach ($value as $inval) {
if (is_array($inval) && isset($inval[$key])) {
$result[] = $inval[$key];
} else {
break;
}
}
}
return $result;
return array_column($value, $key);
}
/**
+1 -1
View File
@@ -218,7 +218,7 @@ class Widget_Abstract_Contents extends Widget_Abstract
*/
public function applyFields(array $fields, $cid)
{
$exists = array_flip(Typecho_Common::arrayFlatten($this->db->fetchAll($this->db->select('name')
$exists = array_flip(array_column($this->db->fetchAll($this->db->select('name')
->from('table.fields')->where('cid = ?', $cid)), 'name'));
foreach ($fields as $name => $value) {
+3 -3
View File
@@ -132,13 +132,13 @@ class Widget_Abstract_Metas extends Widget_Abstract
*/
public function merge($mid, $type, array $metas)
{
$contents = Typecho_Common::arrayFlatten($this->db->fetchAll($this->select('cid')
$contents = array_column($this->db->fetchAll($this->select('cid')
->from('table.relationships')
->where('mid = ?', $mid)), 'cid');
foreach ($metas as $meta) {
if ($mid != $meta) {
$existsContents = Typecho_Common::arrayFlatten($this->db->fetchAll($this->db
$existsContents = array_column($this->db->fetchAll($this->db
->select('cid')->from('table.relationships')
->where('mid = ?', $meta)), 'cid');
@@ -250,7 +250,7 @@ class Widget_Abstract_Metas extends Widget_Abstract
public function clearTags()
{
// 取出count为0的标签
$tags = Typecho_Common::arrayFlatten($this->db->fetchAll($this->db->select('mid')
$tags = array_column($this->db->fetchAll($this->db->select('mid')
->from('table.metas')->where('type = ? AND count = ?', 'tags', 0)), 'mid');
foreach ($tags as $tag) {
+1 -1
View File
@@ -1748,7 +1748,7 @@ class Widget_Archive extends Widget_Abstract_Contents
$this->_archiveTitle = $this->title;
/** 设置关键词 */
$this->_keywords = implode(',', Typecho_Common::arrayFlatten($this->tags, 'name'));
$this->_keywords = implode(',', array_column($this->tags, 'name'));
/** 设置描述 */
$this->_description = $this->description;
+1 -1
View File
@@ -269,7 +269,7 @@ class Widget_Contents_Attachment_Edit extends Widget_Contents_Post_Edit implemen
$deleteCount = 0;
do {
$posts = Typecho_Common::arrayFlatten($this->db->fetchAll($this->select('cid')
$posts = array_column($this->db->fetchAll($this->select('cid')
->from('table.contents')
->where('type = ? AND parent = ?', 'attachment', 0)
->page($page, 100)), 'cid');
+2 -2
View File
@@ -473,7 +473,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
$categories = array_unique(array_map('trim', $categories));
/** 取出已有category */
$existCategories = Typecho_Common::arrayFlatten($this->db->fetchAll(
$existCategories = array_column($this->db->fetchAll(
$this->db->select('table.metas.mid')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
@@ -538,7 +538,7 @@ class Widget_Contents_Post_Edit extends Widget_Abstract_Contents implements Widg
$tags = array_filter($tags, ['Typecho_Validate', 'xssCheck']);
/** 取出已有tag */
$existTags = Typecho_Common::arrayFlatten($this->db->fetchAll(
$existTags = array_column($this->db->fetchAll(
$this->db->select('table.metas.mid')
->from('table.metas')
->join('table.relationships', 'table.relationships.mid = table.metas.mid')
+1 -1
View File
@@ -32,7 +32,7 @@ class Widget_Contents_Related extends Widget_Abstract_Contents
$this->parameter->setDefault('limit=5');
if ($this->parameter->tags) {
$tagsGroup = implode(',', Typecho_Common::arrayFlatten($this->parameter->tags, 'mid'));
$tagsGroup = implode(',', array_column($this->parameter->tags, 'mid'));
$this->db->fetchAll($this->select()
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.relationships.mid IN (' . $tagsGroup . ')')
+1 -1
View File
@@ -22,7 +22,7 @@ class Widget_Metas_Category_Admin extends Widget_Metas_Category_List
$select = $this->db->select('mid')->from('table.metas')->where('type = ?', 'category');
$select->where('parent = ?', $this->request->parent ? $this->request->parent : 0);
$this->stack = $this->getCategories(Typecho_Common::arrayFlatten(
$this->stack = $this->getCategories(array_column(
$this->db->fetchAll($select->order('table.metas.order', Typecho_Db::SORT_ASC)), 'mid'));
}
+7 -7
View File
@@ -1513,8 +1513,8 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
/** 对文章内容做截取处理,以获得description和text_more*/
[$excerpt, $more] = $this->getPostExtended($post);
/** 只需要分类的name*/
$categories = Typecho_Common::arrayFlatten($post->categories, 'name');
$tags = Typecho_Common::arrayFlatten($post->tags, 'name');
$categories = array_column($post->categories, 'name');
$tags = array_column($post->tags, 'name');
$postStruct = [
'dateCreated' => new IXR_Date($this->options->timezone + $post->created),
@@ -1570,8 +1570,8 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
/** 只需要分类的name*/
/** 可以用flatten函数处理 */
$categories = Typecho_Common::arrayFlatten($posts->categories, 'name');
$tags = Typecho_Common::arrayFlatten($posts->tags, 'name');
$categories = array_column($posts->categories, 'name');
$tags = array_column($posts->tags, 'name');
$postStructs[] = [
'dateCreated' => new IXR_Date($this->options->timezone + $posts->created),
@@ -1804,7 +1804,7 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
return new IXR_Error($e->getCode(), $e->getMessage());
}
$post->setCategories($postId, Typecho_Common::arrayFlatten($categories, 'categoryId'),
$post->setCategories($postId, array_column($categories, 'categoryId'),
'publish' == $post->status);
return true;
}
@@ -1916,7 +1916,7 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
return new IXR_Error($e->getCode(), $e->getMessage());
}
$categories = Typecho_Common::arrayFlatten($post->categories, 'name');
$categories = array_column($post->categories, 'name');
$content = '<title>' . $post->title . '</title>';
$content .= '<category>' . implode(',', $categories) . '</category>';
@@ -1973,7 +1973,7 @@ class Widget_XmlRpc extends Widget_Abstract_Contents implements Widget_Interface
$postStructs = [];
while ($posts->next()) {
$categories = Typecho_Common::arrayFlatten($posts->categories, 'name');
$categories = array_column($posts->categories, 'name');
$content = '<title>' . $posts->title . '</title>';
$content .= '<category>' . implode(',', $categories) . '</category>';