From cf5c893c4476dc748677b60157223b0b46875625 Mon Sep 17 00:00:00 2001 From: rickzheng <158672319@qq.com> Date: Thu, 31 Mar 2022 18:06:13 +0800 Subject: [PATCH 1/2] v-0.0.1 --- composer.json | 3 ++- src/Service/interface/Log4php.php | 35 +++++++++++++++++++++++++++ src/Service/interface/ThinkLog.php | 38 ++++++++++++++++++++++++++++++ tests/App/DemoTest.php | 22 +++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 src/Service/interface/Log4php.php create mode 100644 src/Service/interface/ThinkLog.php create mode 100644 tests/App/DemoTest.php diff --git a/composer.json b/composer.json index 9bed123..e9cf56c 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,8 @@ "type": "library", "license": "MIT", "require": { - "apache/log4php": "2.3.0" + "apache/log4php": "2.3.0", + "topthink/think-log": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/Service/interface/Log4php.php b/src/Service/interface/Log4php.php new file mode 100644 index 0000000..31ee50b --- /dev/null +++ b/src/Service/interface/Log4php.php @@ -0,0 +1,35 @@ +logger = \Logger::getLogger("Log"); + } + + function info($message = '') + { + $this->logger->info($message); + } + + function debug($message = '') + { + $this->logger->debug($message); + + } + + function error($message = '') + { + $this->logger->error($message); + + } +} \ No newline at end of file diff --git a/src/Service/interface/ThinkLog.php b/src/Service/interface/ThinkLog.php new file mode 100644 index 0000000..b79fabf --- /dev/null +++ b/src/Service/interface/ThinkLog.php @@ -0,0 +1,38 @@ + 'file', + 'channels' => [ + 'file' => [ + 'type' => 'file', + 'path' => './logs/', + ], + ], + ]); + } + + function info($message = '') + { + Log::info($message); + } + + function debug($message = '') + { + Log::debug($message); + + } + + function error($message = '') + { + Log::error($message); + } +} \ No newline at end of file diff --git a/tests/App/DemoTest.php b/tests/App/DemoTest.php new file mode 100644 index 0000000..c3f2efd --- /dev/null +++ b/tests/App/DemoTest.php @@ -0,0 +1,22 @@ +get_user_info(); + print_r($data); + } + +} \ No newline at end of file From 9052f2b3cbc8439a859f2aa5e0630d68a6cfd6d1 Mon Sep 17 00:00:00 2001 From: rickzheng <158672319@qq.com> Date: Thu, 31 Mar 2022 18:24:50 +0800 Subject: [PATCH 2/2] v-0.0.2 --- src/App/Demo.php | 4 +- src/Service/AppLogger.php | 46 +++++++++++++---------- src/Service/ProductHandler.php | 56 ++++++++++++++++++++++++++++ tests/Service/AppLoggerTest.php | 2 +- tests/Service/ProductHandlerTest.php | 15 +++++--- 5 files changed, 96 insertions(+), 27 deletions(-) diff --git a/src/App/Demo.php b/src/App/Demo.php index 9c9dae0..04db4a1 100644 --- a/src/App/Demo.php +++ b/src/App/Demo.php @@ -1,4 +1,5 @@ _logger->error("fetch data error."); diff --git a/src/Service/AppLogger.php b/src/Service/AppLogger.php index 185e191..69df8c5 100644 --- a/src/Service/AppLogger.php +++ b/src/Service/AppLogger.php @@ -2,31 +2,39 @@ namespace App\Service; -class AppLogger +abstract class AppLogger { - const TYPE_LOG4PHP = 'log4php'; + /** + * 打印信息日志 + * @param $message + * @return mixed + */ + abstract function info($message = ''); - private $logger; + /** + * 调试信息 + * @param $message + * @return mixed + */ + abstract function debug($message = ''); - public function __construct($type = self::TYPE_LOG4PHP) - { - if ($type == self::TYPE_LOG4PHP) { - $this->logger = \Logger::getLogger("Log"); - } - } + /** + * 错误信息 + * @param $message + * @return mixed + */ + abstract function error($message = ''); - public function info($message = '') - { - $this->logger->info($message); - } - public function debug($message = '') + public static function getInstance($className) { - $this->logger->debug($message); - } + $className = ucfirst(trim($className)); + $classFile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'interface' . DIRECTORY_SEPARATOR . $className . ".php"; + if (!file_exists($classFile)) + exit("Logger file $classFile is not exist"); + include_once($classFile); - public function error($message = '') - { - $this->logger->error($message); + $cl = new \ReflectionClass($className); + return $cl->newInstance(); } } \ No newline at end of file diff --git a/src/Service/ProductHandler.php b/src/Service/ProductHandler.php index dd0fdfc..6f267cc 100644 --- a/src/Service/ProductHandler.php +++ b/src/Service/ProductHandler.php @@ -4,5 +4,61 @@ class ProductHandler { + /** + * 统计商品总额 + * @param $products + * @return int|mixed + */ + public static function countAllPrice($products) + { + $totalPrice = 0; + foreach ($products as $product) { + $price = $product['price'] ?: 0; + $totalPrice += $price; + } + return $totalPrice; + } + + /** + * 排序并归类 + * @param $products + * @param string $type + * @return array + */ + public static function sortAndGroup($products, string $type = ''): array + { + if (!is_array($products)) { + return []; + } + $array_column = array_column($products, 'price'); // 根据产品排序 + array_multisort($array_column, SORT_DESC, $products); + if ($type) { + $data = []; + foreach ($products as $v) { + if ($type == $v['type']) { + $data[] = $v; + } + } + return $data; + } + return $products; + } + + /** + * 转换时间 + * @param $products + * @param string $index + * @return array + */ + public static function transformTime($products, string $index = 'create_at'): array + { + if (!is_array($products)) { + return []; + } + foreach ($products as &$row) { + $row[$index] = strtotime($row[$index]); + } + return $products; + } } \ No newline at end of file diff --git a/tests/Service/AppLoggerTest.php b/tests/Service/AppLoggerTest.php index 4981ba6..14271a3 100644 --- a/tests/Service/AppLoggerTest.php +++ b/tests/Service/AppLoggerTest.php @@ -13,7 +13,7 @@ class AppLoggerTest extends TestCase public function testInfoLog() { - $logger = new AppLogger('log4php'); + $logger = AppLogger::getInstance('log4php'); $logger->info('This is info log message'); } } \ No newline at end of file diff --git a/tests/Service/ProductHandlerTest.php b/tests/Service/ProductHandlerTest.php index 4ec9161..9f91cfd 100644 --- a/tests/Service/ProductHandlerTest.php +++ b/tests/Service/ProductHandlerTest.php @@ -57,12 +57,15 @@ class ProductHandlerTest extends TestCase public function testGetTotalPrice() { - $totalPrice = 0; - foreach ($this->products as $product) { - $price = $product['price'] ?: 0; - $totalPrice += $price; - } + $this->assertEquals(143, ProductHandler::countAllPrice($this->products)); + } + + public function testOrderProduct(){ + print_r(ProductHandler::sortAndGroup($this->products)); + print_r(ProductHandler::sortAndGroup($this->products,'Dessert')); + } - $this->assertEquals(143, $totalPrice); + public function testTransformTime(){ + print_r(ProductHandler::transformTime($this->products)); } } \ No newline at end of file