本文整理匯總了PHP中Core\Helper\Utility\Validator::oneOf方法的典型用法代碼示例。如果您正苦於以下問題:PHP Validator::oneOf方法的具體用法?PHP Validator::oneOf怎麽用?PHP Validator::oneOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Core\Helper\Utility\Validator
的用法示例。
在下文中一共展示了Validator::oneOf方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: get
public function get($f3)
{
global $smarty;
// 首先做參數合法性驗證
$validator = new Validator($f3->get('GET'));
$pageNo = $validator->digits('pageNo 參數非法')->min(0, true, 'pageNo 參數非法')->validate('pageNo');
// 搜索參數數組
$searchFormQuery = array();
$searchFormQuery['g.category_id'] = $validator->required('商品分類不能為空')->digits('分類id非法')->min(1, true, '分類id非法')->filter('ValidatorIntValue')->validate('category_id');
// 這裏支持多品牌查詢
$searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id'));
// 價格區間查詢
$shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min');
$shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin);
$shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max');
$shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax);
$searchFormQuery['g.shop_price'] = array($shopPriceMin, $shopPriceMax);
// 屬性過濾
$filter = $validator->validate('filter');
// 排序
$orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy');
$orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir');
$orderByParam = array();
if (!empty($orderBy)) {
$orderByParam = array(array($orderBy, $orderDir));
}
//增加一些我們的缺省排序
$orderByParam[] = array('g.sort_order', 'desc');
$orderByParam[] = array('g.goods_id', 'desc');
// 參數驗證
if (!$this->validate($validator) || empty($searchFormQuery)) {
goto out_fail;
}
$pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0;
$pageSize = 45;
// 每頁固定顯示 45 個商品
// 生成 smarty 的緩存 id
$smartyCacheId = 'Goods|Category|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $filter . '_' . $pageNo . '_' . $pageSize);
// 開啟並設置 smarty 緩存時間
enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search'));
if ($smarty->isCached('goods_category.tpl', $smartyCacheId)) {
goto out_display;
}
$goodsCategoryService = new GoodsCategoryService();
$category = $goodsCategoryService->loadCategoryById($searchFormQuery['g.category_id'], 1800);
if ($category->isEmpty()) {
$this->addFlashMessage('分類[' . $searchFormQuery['category_id'] . ']不存在');
goto out_fail;
}
$smarty->assign('category', $category);
$metaData = json_decode($category['meta_data'], true);
$metaFilterArray = @$metaData['filterArray'];
// 1. 我們需要在左側顯示分類層級結構
$goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray($category['parent_meta_id'], false, 1800);
$smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray);
/**
* 構造 Filter 數組,結構如下
*
* array(
* '商品品牌' => array(
* filterKey => 'brand_id'
* filterValueArray => array( array(value=>'13', text=>'品牌1'), ...)
* ),
* '顏色' => array(
* filterKey => 'filter',
* filterValueArray => array( array(value=>'13', text=>'品牌1'), ...)
* )
* )
*
*/
$goodsFilterArray = array();
// filter 查詢在這個條件下進行
$goodsFilterQueryCond = array_merge($this->searchExtraCondArray, array(array('g.category_id', '=', $searchFormQuery['g.category_id'])));
// 2. 商品品牌查詢
$goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0);
$brandIdArray = array_map(function ($elem) {
return $elem['brand_id'];
}, $goodsBrandIdArray);
if (!empty($brandIdArray)) {
$goodsBrandService = new GoodsBrandService();
$goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray)));
$filterBrandArray = array();
foreach ($goodsBrandArray as $brand) {
$filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']);
}
if (!empty($filterBrandArray)) {
$goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray);
}
}
// 3. 查詢屬性過濾
if (!empty($metaFilterArray)) {
$goodsTypeService = new GoodsTypeService();
foreach ($metaFilterArray as $filterItem) {
$goodsTypeAttrItem = $goodsTypeService->loadGoodsTypeAttrItemById($filterItem['attrItemId']);
if ($goodsTypeAttrItem->isEmpty()) {
continue;
}
// 取得商品屬性值列表
$goodsAttrItemValueArray = SearchHelper::search(SearchHelper::Module_GoodsAttrGoods, 'min(ga.goods_attr_id) as goods_attr_id, ga.attr_item_value', array_merge($goodsFilterQueryCond, array(array('ga.attr_item_id', '=', $filterItem['attrItemId']))), null, 0, 0, 'ga.attr_item_value');
if (!empty($goodsAttrItemValueArray)) {
//.........這裏部分代碼省略.........
示例2: get
public function get($f3)
{
global $smarty;
// 首先做參數合法性驗證
$validator = new Validator($f3->get('GET'));
$pageNo = $validator->digits('pageNo 參數非法')->min(0, true, 'pageNo 參數非法')->validate('pageNo');
// 搜索參數數組
$searchFormQuery = array();
$searchFormQuery['category_id'] = $validator->digits('分類id非法')->min(1, true, '分類id非法')->filter('ValidatorIntValue')->validate('category_id');
$searchFormQuery['suppliers_id'] = $validator->digits('供貨商id非法')->min(1, true, '供貨商id非法')->filter('ValidatorIntValue')->validate('suppliers_id');
$searchFormQuery['goods_name'] = $validator->validate('goods_name');
// 價格區間查詢
$shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min');
$shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max');
$searchFormQuery['shop_price'] = array($shopPriceMin, $shopPriceMax);
// 排序
$orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy');
$orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir');
$orderByParam = array();
if (!empty($orderBy)) {
$orderByParam = array(array($orderBy, $orderDir));
}
//增加一些我們的缺省排序
$orderByParam[] = array('sort_order', 'desc');
$orderByParam[] = array('goods_id', 'desc');
// 參數驗證
if (!$this->validate($validator) || empty($searchFormQuery)) {
goto out_fail;
}
$pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0;
$pageSize = 10;
// 每頁固定顯示 10 個商品
// 生成 smarty 的緩存 id
$smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize);
// 開啟並設置 smarty 緩存時間
enableSmartyCache(true, MobileThemePlugin::getOptionValue('smarty_cache_time_goods_search'));
if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) {
goto out_display;
}
// 合並查詢參數
$searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray);
$totalCount = SearchHelper::count(SearchHelper::Module_Goods, $searchParamArray);
if ($totalCount <= 0) {
goto out_display;
// 沒有商品,直接顯示
}
// 頁號可能是用戶亂輸入的,我們需要檢查
if ($pageNo * $pageSize >= $totalCount) {
goto out_fail;
// 返回首頁
}
$goodsArray = SearchHelper::search(SearchHelper::Module_Goods, $this->searchFieldSelector, $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize);
if (empty($goodsArray)) {
goto out_display;
}
// 取得 商品ID 列表
$goodsIdArray = array();
foreach ($goodsArray as $goodsItem) {
$goodsIdArray[] = $goodsItem['goods_id'];
}
// 取得商品的圖片
$goodsGalleryService = new GoodsGalleryService();
$goodsGalleryArray = $goodsGalleryService->fetchGoodsGalleryArrayByGoodsIdArray($goodsIdArray);
$currentGoodsId = -1;
$goodsThumbImageArray = array();
$goodsImageArray = array();
foreach ($goodsGalleryArray as $goodsGalleryItem) {
if ($currentGoodsId == $goodsGalleryItem['goods_id']) {
continue;
//每個商品我們隻需要一張圖片,跳過其它的圖片
}
$currentGoodsId = $goodsGalleryItem['goods_id'];
// 新的商品 id
$goodsThumbImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['thumb_url']);
$goodsImageArray[$currentGoodsId] = RouteHelper::makeImageUrl($goodsGalleryItem['img_url']);
}
// 賦值給模板
$smarty->assign('totalCount', $totalCount);
$smarty->assign('pageNo', $pageNo);
$smarty->assign('pageSize', $pageSize);
$smarty->assign('goodsArray', $goodsArray);
$smarty->assign('goodsThumbImageArray', $goodsThumbImageArray);
$smarty->assign('goodsImageArray', $goodsImageArray);
out_display:
$smarty->display('goods_search.tpl', $smartyCacheId);
return;
out_fail:
// 失敗從這裏返回
RouteHelper::reRoute($this, '/');
// 返回首頁
}
示例3: get
public function get($f3)
{
global $smarty;
// 首先做參數合法性驗證
$validator = new Validator($f3->get('GET'));
$pageNo = $validator->digits('pageNo 參數非法')->min(0, true, 'pageNo 參數非法')->validate('pageNo');
// 搜索參數數組
$searchFormQuery = array();
$searchKeywords = $validator->validate('keywords');
$searchFormQuery['g.goods_name'] = $searchKeywords;
// 這裏支持多品牌查詢
$searchFormQuery['g.brand_id'] = array('=', $validator->validate('brand_id'));
// 價格區間查詢
$shopPriceMin = $validator->filter('ValidatorFloatValue')->validate('shop_price_min');
$shopPriceMin = null == $shopPriceMin ? null : Money::toStorage($shopPriceMin);
$shopPriceMax = $validator->filter('ValidatorFloatValue')->validate('shop_price_max');
$shopPriceMax = null == $shopPriceMax ? null : Money::toStorage($shopPriceMax);
$searchFormQuery['g.shop_price'] = array($shopPriceMin, $shopPriceMax);
// 排序
$orderBy = $validator->oneOf(array('', 'total_buy_number', 'shop_price', 'add_time'))->validate('orderBy');
$orderDir = $validator->oneOf(array('', 'asc', 'desc'))->validate('orderDir');
$orderByParam = array();
if (!empty($orderBy)) {
$orderByParam = array(array($orderBy, $orderDir));
}
//增加一些我們的缺省排序
$orderByParam[] = array('g.sort_order', 'desc');
$orderByParam[] = array('g.goods_id', 'desc');
// 參數驗證
if (!$this->validate($validator) || empty($searchFormQuery)) {
goto out_fail;
}
$pageNo = isset($pageNo) && $pageNo > 0 ? $pageNo : 0;
$pageSize = 45;
// 每頁固定顯示 45 個商品
// 生成 smarty 的緩存 id
$smartyCacheId = 'Goods|Search|' . md5(json_encode($searchFormQuery) . json_encode($orderByParam) . '_' . $pageNo . '_' . $pageSize);
// 開啟並設置 smarty 緩存時間
enableSmartyCache(true, bzf_get_option_value('smarty_cache_time_goods_search'));
if ($smarty->isCached('goods_search.tpl', $smartyCacheId)) {
goto out_display;
}
$goodsCategoryService = new GoodsCategoryService();
// 1. 我們需要在左側顯示分類層級結構
$goodsCategoryTreeArray = $goodsCategoryService->fetchCategoryTreeArray(0, false, 1800);
$smarty->assign('goodsCategoryTreeArray', $goodsCategoryTreeArray);
/**
* 構造 Filter 數組,結構如下
*
* array(
* '商品品牌' => array(
* filterKey => 'brand_id'
* filterValueArray => array( array(value=>'13', text=>'品牌1'), ...)
* ),
* '顏色' => array(
* filterKey => 'filter',
* filterValueArray => array( array(value=>'13', text=>'品牌1'), ...)
* )
* )
*
*/
$goodsFilterArray = array();
// filter 查詢在這個條件下進行
$goodsFilterQueryCond = array_merge(QueryBuilder::buildSearchParamArray(array('g.goods_name' => $searchKeywords)), $this->searchExtraCondArray);
// 2. 商品品牌查詢
$goodsBrandIdArray = SearchHelper::search(SearchHelper::Module_Goods, 'distinct(g.brand_id)', array_merge($goodsFilterQueryCond, array(array('g.brand_id > 0'))), null, 0, 0);
$brandIdArray = array_map(function ($elem) {
return $elem['brand_id'];
}, $goodsBrandIdArray);
if (!empty($brandIdArray)) {
$goodsBrandService = new GoodsBrandService();
$goodsBrandArray = $goodsBrandService->fetchBrandArrayByIdArray(array_unique(array_values($brandIdArray)));
$filterBrandArray = array();
foreach ($goodsBrandArray as $brand) {
$filterBrandArray[] = array('value' => $brand['brand_id'], 'text' => $brand['brand_name']);
}
if (!empty($filterBrandArray)) {
$goodsFilterArray['品牌'] = array('filterKey' => 'brand_id', 'filterValueArray' => $filterBrandArray);
}
}
if (!empty($goodsFilterArray)) {
$smarty->assign('goodsFilterArray', $goodsFilterArray);
}
// 3. 商品屬性過濾 TODO: 等以後擴展,看看 Search 怎麽做屬性過濾
// 4. 商品查詢
// 構造 filter 參數,注意 filter 參數在 GoodsGoodsAttr 中具體解析
// 合並查詢參數
$searchParamArray = array_merge(QueryBuilder::buildSearchParamArray($searchFormQuery), $this->searchExtraCondArray);
$totalCount = SearchHelper::count(SearchHelper::Module_GoodsGoodsAttr, $searchParamArray);
if ($totalCount <= 0) {
goto out_display;
// 沒有商品,直接顯示
}
// 頁號可能是用戶亂輸入的,我們需要檢查
if ($pageNo * $pageSize >= $totalCount) {
goto out_fail;
// 返回首頁
}
$goodsArray = SearchHelper::search(SearchHelper::Module_GoodsGoodsAttr, 'g.goods_id, g.cat_id, g.goods_sn, g.goods_name, g.brand_id, g.goods_number, g.market_price' . ', g.shop_price, g.suppliers_id, g.virtual_buy_number, g.user_buy_number, g.user_pay_number' . ', (g.virtual_buy_number + g.user_pay_number) as total_buy_number', $searchParamArray, $orderByParam, $pageNo * $pageSize, $pageSize);
if (empty($goodsArray)) {
//.........這裏部分代碼省略.........