<commit_before><?php
/**
 * Name: wechat-BaseAnalysis.php
 * CreateDateTime: 2017/4/29 15:02
 * Author: Chison
 * Describe:
 */

namespace Chison\Wechat\Statistics;

use Chison\Wechat\Base;
use Chison\Wechat\Tools\Http;

class BaseAnalysis extends Base
{
    /**
     * @var string 开始时间 与 结束时间的跨度不能超过接口的最大跨度
     */
    protected $beginDate = '2016-12-01';

    /**
     * @var string 结束时间 , 允许设置的最大值为昨日
     */
    protected $endDate = '2016-12-07';


    /**
     * 验证日期是否有效
     * @param $max
     * @throws \Exception
     */
    protected function isValidDate($max){
        $disparity = (strtotime($this->endDate)-strtotime($this->beginDate))/86400;
        if($disparity < 0)
            throw new \Exception('结束日期小于开始日期');
        if($disparity >= $max){
            throw new \Exception('时间跨度过大： 最大时间跨度：' . $max);
        }
    }

    /**
     * @param $type string 类型 , 需要和属性名称一致
     * @return mixed
     * @throws \Exception | array | bool
     */
    public function analysis(string $type){
        if(!property_exists($this , $type) && isset($this->$type['url'])){
            throw new \Exception('无效的图文分析方法');
        }
        $this->isValidDate($this->$type['max']);
        return Http::start($this->$type['url'] . $this->token , 'post')
            ->setData(
                json_encode(
                    [
                        'begin_date' => $this->beginDate,
                        'end_date'   => $this->endDate
                    ]
                )
            )
            ->send()
            ->jsonToArrayResponse();
    }

    /**
     * @return string
     */
    public function getBeginDate(): string
    {
        return $this->beginDate;
    }

    /**
     * @param string $beginDate<commit_msg>异常提示信息修改<commit_after><?php
/**
 * Name: wechat-BaseAnalysis.php
 * CreateDateTime: 2017/4/29 15:02
 * Author: Chison
 * Describe:
 */

namespace Chison\Wechat\Statistics;

use Chison\Wechat\Base;
use Chison\Wechat\Tools\Http;

class BaseAnalysis extends Base
{
    /**
     * @var string 开始时间 与 结束时间的跨度不能超过接口的最大跨度
     */
    protected $beginDate = '2016-12-01';

    /**
     * @var string 结束时间 , 允许设置的最大值为昨日
     */
    protected $endDate = '2016-12-07';


    /**
     * 验证日期是否有效
     * @param $max
     * @throws \Exception
     */
    protected function isValidDate($max){
        $disparity = (strtotime($this->endDate)-strtotime($this->beginDate))/86400;
        if($disparity < 0)
            throw new \Exception('结束日期小于开始日期');
        if($disparity >= $max){
            throw new \Exception('时间跨度过大： 最大时间跨度：' . $max);
        }
    }

    /**
     * @param $type string 类型 , 需要和属性名称一致
     * @return mixed
     * @throws \Exception | array | bool
     */
    public function analysis(string $type){
        if(!property_exists($this , $type) && isset($this->$type['url'])){
            throw new \Exception('无效的分析方法');
        }
        $this->isValidDate($this->$type['max']);
        return Http::start($this->$type['url'] . $this->token , 'post')
            ->setData(
                json_encode(
                    [
                        'begin_date' => $this->beginDate,
                        'end_date'   => $this->endDate
                    ]
                )
            )
            ->send()
            ->jsonToArrayResponse();
    }

    /**
     * @return string
     */
    public function getBeginDate(): string
    {
        return $this->beginDate;
    }

    /**
     * @param string $beginDate
{'fraction_non_alphanumeric': 0.1502158273381295, 'fraction_numerical': 0.018992805755395685, 'mean_word_length': 1.752177355502771, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4624218', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1215, 'n_words': 201}