CouponFloorProcess.php 4.32 KB
<?php
/**
 * Created by PhpStorm.
 * User: Targaryen
 * Date: 2016/4/15
 * Time: 13:17
 */

namespace Plugin\DataProcess;


use Plugin\Images;

class CouponFloorProcess
{
    // 兼容驼峰规则命名的 template_name,此处是以 templateName 命名的数据
    private static $TEMPLATE_LIST = array(
        'single_image',
        'focus'
    );

    /**
     * 获取楼层数据
     *
     * @param $data
     * @return array
     */
    public static function getContent($data)
    {
        $result = array();

        if (empty($data)) {
            return array();
        }

        $build = array();
        foreach ($data as $k => &$v) {
            $fun = '';
            if (empty($v) || !is_array($v) || !array_key_exists('template_name', $v)) {
                if (array_key_exists('templateName', $v)) {
                    $fun = $v['templateName'];
                } else {
                    continue;
                }
            }
            if (!in_array($fun, self::$TEMPLATE_LIST)) {
                $fun = $v['template_name'];
            }
            if (empty($v['data']) || !is_callable("self::$fun")) {
                continue;
            }
            // tar note 处理楼层标题
            if ($fun === 'getCoupon' && array_key_exists('template_name', $data[$k - 1]) && $data[$k - 1]['template_name'] === 'text') {
                $v['data']['floorTitle'] = $data[$k - 1]['data'];
            }
            $build = self::$fun($v['data']);
            if (empty($build)) {
                continue;
            }
            $result[] = $build;
        }
        $build = array();
        return $result;
    }

    /**
     * 焦点图处理
     *
     * @param $data
     * @return mixed
     */
    private static function carousel_banner($data)
    {
        if (empty($data['list'])) {
            return array();
        }
        foreach ($data['list'] as &$item) {
            $item['img'] = Images::getImageUrl($item['src'], 0, 0);
            if (!is_string($item['url'])) {
                $item['url'] = '';
            }
        }
        $data['isCarouselBanner'] = true;
        return $data;
    }

    /**
     *
     * 领券楼层数据处理
     *
     * @param $data
     * @return array
     * @internal param $type
     */
    private static function getCoupon($data)
    {
        $result = array();
        if (empty($data)) {
            return array();
        }
        $floorTitle = '';
        foreach ($data as $key => &$item) {
            if ($key === 'floorTitle') {
                $floorTitle = (key_exists('text', $item) && ($item['text'] !== '')) ? $item['text'] : '';
                continue;
            }
            $imageSrc = Images::getImageUrl($item['image']['src'], 0, 0);
            $item['image']['src'] = $imageSrc;
            switch ($item['status']) {
                case 1:
                    $item['isGet'] = true;
                    break;
                case 2:
                    $item['isZero'] = true;
                    break;
                case 3:
                    $item['isGeted'] = true;
                    break;
                default:
                    break;
            }
        }
        if ($floorTitle !== '') {
            $data[0]['floorTitle'] = $floorTitle;
            $data[0]['showFloorTitle'] = true;
        }
        $result = $data[0];
        $result['isCoupon'] = true;
        return $result;
    }

    /**
     * 处理标题数据
     *
     * @param $data
     * @return bool
     */
    private static function text($data)
    {
        $data['isTitle'] = true;
        if ($data['text']) {
            $data['isShow'] = true;
        }
        return $data;
    }

    /**
     *
     * 单张图片的处理方法
     *
     * @param $data
     * @return mixed
     */
    private static function single_image($data)
    {
        $data[0]['src'] = Images::getImageUrl($data[0]['src'], 0, 0);
        $data[0]['isSingleImage'] = true;
        return $data[0];
    }

    /**
     *
     * 辅助方法 Debug 数据输出
     *
     * @param $data
     * @param bool $is_exit 输出后是否中断执行
     */
    public static function debugOut($data, $is_exit = true)
    {
        header("Content-type:text/html;charset=utf-8");
        echo '<pre>';
        print_r($data);
        echo '</pre>';
        if ($is_exit) {
            exit;
        }
    }
}