54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use App\Models\OrderModel;
|
|
use App\Models\ProductModel;
|
|
|
|
class EcommerceService
|
|
{
|
|
private $_productModel = null;
|
|
private $_orderModel = null;
|
|
public function __construct()
|
|
{
|
|
$this->_productModel = new ProductModel();
|
|
$this->_orderModel = new OrderModel();
|
|
}
|
|
|
|
//Form Fields
|
|
public function getFields(string $action): array
|
|
{
|
|
return ["product_uid", "quantity", "price", "status"];
|
|
}
|
|
//Field별 Form Rule용
|
|
public function getFieldRules(array $fields, string $action): array
|
|
{
|
|
$rules = array();
|
|
foreach ($fields as $field) {
|
|
switch ($field) {
|
|
case "product_uid":
|
|
$rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";
|
|
break;
|
|
case "sess_id":
|
|
$rules[$field] = "required|string";
|
|
break;
|
|
case 'quantity':
|
|
case 'price':
|
|
$rules[$field] = "required|numeric";
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return $rules;
|
|
}
|
|
|
|
//장바구니에 담기
|
|
public function addCart(array $fieldDatas)
|
|
{
|
|
$product = $this->_productModel->getEntity($fieldDatas['product_uid']);
|
|
$this->_productModel->addCart($product, $fieldDatas['quantity'], $fieldDatas['price']);
|
|
$this->_orderModel->addCart($product, $fieldDatas['quantity'], $fieldDatas['price']);
|
|
}
|
|
}
|