<?php
 namespace Module\Product\Util; use ModStart\Core\Dao\ModelUtil; use ModStart\Core\Util\ArrayUtil; use ModStart\Core\Util\HtmlUtil; class ProductUtil { public static function buildRecord($record) { $item = ArrayUtil::keepKeys($record, ['id', 'cover', 'title', 'updated_at',]); $item['day'] = date('Y-m-d', strtotime($record['created_at'])); if (empty($record['cover'])) { $info = HtmlUtil::extractTextAndImages($record['content']); if (!empty($info['images'])) { $item['cover'] = $info['images'][0]; } } return $item; } public static function buildRecords($records) { foreach ($records as $k => $record) { $records[$k] = self::buildRecord($record); } return $records; } public static function listLatest($limit = 5) { $records = ModelUtil::model('product') ->orderBy('id', 'desc') ->limit($limit)->get()->toArray(); return self::buildRecords($records); } public static function get($id) { return ModelUtil::get('product', ['id' => $id]); } public static function listCategories() { return ModelUtil::model('product_category')->orderBy('sort', 'asc')->get()->toArray(); } }