<?php
 namespace Module\CmsProperty\Util; use ModStart\Core\Dao\ModelUtil; use ModStart\Core\Util\ArrayUtil; use ModStart\Core\Util\HtmlUtil; class CmsPropertyUtil { 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('cms_property') ->orderBy('id', 'desc') ->limit($limit)->get()->toArray(); return self::buildRecords($records); } public static function get($id) { return ModelUtil::get('cms_property', ['id' => $id]); } public static function listCategories() { return ModelUtil::model('cms_property_category')->orderBy('sort', 'asc')->get()->toArray(); } }