<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\modules\article\validate\domain;

use app\validate\domain\DomainValidatorInterface;

/**
 * Article Domain Validator
 * @package Applications\sample\modules\article\validate\domain
 */
class ArticleDomainValidator implements DomainValidatorInterface
{
    /**
     * 验证文章数据
     * @param array $data 待验证数据
     * @param string $scene 验证场景
     * @return array
     */
    public function validate(array $data, string $scene = ''): array
    {
        $errors = [];
        if ($scene === 'save' || $scene === 'create') {
            if (empty($data['title'])) {
                $errors['title'] = '标题不能为空';
            }
            if ((!isset($data['id']) || $data['id'] <= 0) && empty($data['content'])) {
                $errors['content'] = '内容不能为空';
            }
            if (isset($data['price']) && (float)$data['price'] < 0) {
                $errors['price'] = '价格不能为负数';
            }
        }
        return ['ok' => empty($errors), 'errors' => $errors];
    }
}
