Skip to content
This repository was archived by the owner on Jan 23, 2019. It is now read-only.

Commit 5435708

Browse files
committed
update, add some methods
1 parent 2c9cc00 commit 5435708

File tree

7 files changed

+206
-140
lines changed

7 files changed

+206
-140
lines changed

src/Collections/LiteCollection.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@
1414
*/
1515
class LiteCollection extends \ArrayObject implements CollectionInterface
1616
{
17+
/**
18+
* @param array|null $items
19+
* @return static
20+
*/
21+
public static function make($items = null)
22+
{
23+
return new static((array)$items);
24+
}
25+
26+
/**
27+
* Create new collection
28+
* @param array $items Pre-populate collection with this key-value array
29+
*/
30+
public function __construct(array $items = [])
31+
{
32+
parent::__construct();
33+
34+
$this->replace($items);
35+
}
36+
1737
/**
1838
* @param string $name
1939
* @param null|mixed $default

src/Components/ErrorHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ErrorHandler
5252
*/
5353
public function __construct(LoggerInterface $logger, array $options = [])
5454
{
55-
Obj::smartConfigure($this, $options);
55+
Obj::init($this, $options);
5656

5757
$this->logger = $logger;
5858
}
@@ -218,6 +218,7 @@ public function handleFatalError()
218218
$this->reservedMemory = null;
219219

220220
$lastError = error_get_last();
221+
221222
if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) {
222223
$this->logger->log(
223224
$this->fatalLevel ?? LogLevel::ALERT,
@@ -230,6 +231,10 @@ public function handleFatalError()
230231
]
231232
);
232233

234+
if (method_exists($this->logger, 'flush')) {
235+
$this->logger->flush();
236+
}
237+
233238
if ($this->logger instanceof Logger) {
234239
foreach ($this->logger->getHandlers() as $handler) {
235240
if ($handler instanceof AbstractHandler) {

src/Components/Language.php

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,39 +120,39 @@ protected function init()
120120
* {@inheritdoc}
121121
* @see self::translate()
122122
*/
123-
public function t($key, array $args = [], $default = '')
123+
public function t($key, array $args = [], $lang = '')
124124
{
125-
return $this->translate($key, $args, $default);
125+
return $this->translate($key, $args, $lang);
126126
}
127127

128128
/**
129129
* {@inheritdoc}
130130
* @see self::translate()
131131
*/
132-
public function tl($key, array $args = [], $default = null)
132+
public function tl($key, array $args = [], $lang = null)
133133
{
134-
return $this->translate($key, $args, $default);
134+
return $this->translate($key, $args, $lang);
135135
}
136136

137137
/**
138138
* {@inheritdoc}
139139
* @see self::translate()
140140
*/
141-
public function trans($key, array $args = [], $default = null)
141+
public function trans($key, array $args = [], $lang = null)
142142
{
143-
return $this->translate($key, $args, $default);
143+
return $this->translate($key, $args, $lang);
144144
}
145145

146146
/**
147147
* how to use language translate ? please see '/doc/language.md'
148148
* @param string|bool $key
149149
* @param array $args
150-
* @param string $default
150+
* @param string $lang
151151
* @return string|array
152152
* @throws \Inhere\Exceptions\NotFoundException
153153
* @throws \InvalidArgumentException
154154
*/
155-
public function translate($key, array $args = [], $default = null)
155+
public function translate($key, array $args = [], $lang = null)
156156
{
157157
if (!is_string($key)) {
158158
throw new \InvalidArgumentException('The translate key must be a string.');
@@ -162,14 +162,16 @@ public function translate($key, array $args = [], $default = null)
162162
throw new \InvalidArgumentException('Cannot translate the empty key');
163163
}
164164

165+
list($lang, $key) = $this->parseKey($key, $lang);
166+
165167
// translate form current language. if not found, translate form fallback language.
166168
if (($value = $this->findTranslationText($key)) === null) {
167-
$value = $this->transByFallbackLang($key, $default);
169+
$value = $this->transByFallbackLang($key);
168170
}
169171

170172
// no translate text
171173
if ($value === '' || $value === null) {
172-
return ucfirst(Str::toSnakeCase(str_replace(['-', '_'], ' ', $key), ' '));
174+
return ucfirst(Str::toSnake(str_replace(['-', '_'], ' ', $key), ' '));
173175
}
174176

175177
// $args is not empty
@@ -238,13 +240,12 @@ protected function findTranslationText($key)
238240

239241
/**
240242
* @param string $key
241-
* @param string $default
242243
* @return mixed
243244
*/
244-
protected function transByFallbackLang($key, $default = null)
245+
protected function transByFallbackLang($key)
245246
{
246247
if ($this->lang === $this->fallbackLang || !$this->fallbackLang) {
247-
return $default;
248+
return null;
248249
}
249250

250251
// init fallbackData
@@ -278,17 +279,40 @@ protected function transByFallbackLang($key, $default = null)
278279
$this->loadedFiles[] = $file;
279280
$this->fallbackData->set($fileKey, Collection::read($file, $this->format));
280281

281-
return $this->fallbackData->get($key, $default);
282+
return $this->fallbackData->get($key);
282283
}
283284
}
284285

285-
return $default;
286+
return null;
286287
}
287288

289+
288290
/*********************************************************************************
289-
* helper method
291+
* helper
290292
*********************************************************************************/
291293

294+
/**
295+
* @param string $key
296+
* @param string $lang
297+
* @return array
298+
*/
299+
private function parseKey($key, $lang = null)
300+
{
301+
if ($lang) {
302+
return [$lang, $key];
303+
}
304+
305+
if (strpos($key, $this->separator)) {
306+
$info = explode($this->separator, $key, 2);
307+
308+
if ($this->isLang($info[0])) {
309+
return $info;
310+
}
311+
}
312+
313+
return [$this->lang, $key];
314+
}
315+
292316
/**
293317
* @param $filename
294318
* @param string $lang
@@ -376,6 +400,19 @@ public function hasLangFileData($fileKey)
376400
* getter/setter
377401
*********************************************************************************/
378402

403+
/**
404+
* @param string $lang
405+
* @return bool
406+
*/
407+
public function hasLang($lang)
408+
{
409+
return $this->isLang($lang);
410+
}
411+
public function isLang($lang)
412+
{
413+
return $lang && in_array($lang, $this->langs,true);
414+
}
415+
379416
/**
380417
* Allow quick access default file translate by `$lang->key`,
381418
* is equals to `$lang->tl('key')`.

src/Helpers/Str.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,18 @@
1515
*/
1616
class Str extends StringHelper
1717
{
18+
/**
19+
* @param string $string
20+
* @param string $prefix
21+
* @param string $suffix
22+
* @return string
23+
*/
24+
public static function optional(string $string, string $prefix = ' ', string $suffix = ''): string
25+
{
26+
if (empty($string)) {
27+
return '';
28+
}
29+
30+
return $prefix . $string . $suffix;
31+
}
1832
}

src/Helpers/StringHelper.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,18 +568,23 @@ public static function truncateString($text, $length = 120, array $options = arr
568568
return $truncate;
569569
}
570570

571+
public static function toCamel($str, $upperFirstChar = false)
572+
{
573+
return self::toCamelCase($str, $upperFirstChar);
574+
}
575+
571576
/**
572577
* Translates a string with underscores into camel case (e.g. first_name -> firstName)
573578
* @prototype string public static function toCamelCase(string $str[, bool $capitalise_first_char = false])
574579
* @param $str
575-
* @param bool $upper_case_first_char
580+
* @param bool $upperFirstChar
576581
* @return mixed
577582
*/
578-
public static function toCamelCase($str, $upper_case_first_char = false)
583+
public static function toCamelCase($str, $upperFirstChar = false)
579584
{
580585
$str = self::strtolower($str);
581586

582-
if ($upper_case_first_char) {
587+
if ($upperFirstChar) {
583588
$str = self::ucfirst($str);
584589
}
585590

@@ -588,6 +593,11 @@ public static function toCamelCase($str, $upper_case_first_char = false)
588593
}, $str);
589594
}
590595

596+
public static function toSnake($str, $sep = '_')
597+
{
598+
return self::toSnakeCase($str, $sep);
599+
}
600+
591601
/**
592602
* Transform a CamelCase string to underscore_case string
593603
* @param string $str
@@ -697,7 +707,7 @@ public static function wordFormat($keyword)
697707
}
698708

699709
/**
700-
* 缩进格式化内容,去空白/注释 已不会影响到 HEREDOC 标记
710+
* 缩进格式化内容,去空白/注释
701711
* @param $fileName
702712
* @param int $type
703713
* @return mixed

src/Traits/PathResolverTrait.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-10-23
6+
* Time: 11:41
7+
*/
8+
9+
namespace Inhere\Library\Traits;
10+
11+
use Inhere\Library\Helpers\Php;
12+
13+
/**
14+
* Trait PathResolver
15+
* @package Inhere\Library\Traits
16+
*/
17+
trait PathResolverTrait
18+
{
19+
/**
20+
* @var callable
21+
*/
22+
protected $pathResolver;
23+
24+
/**
25+
* @param string $path
26+
* @return string
27+
*/
28+
public function resolverPath($path)
29+
{
30+
if (!$this->pathResolver) {
31+
return $path;
32+
}
33+
34+
return Php::call($this->pathResolver, $path);
35+
}
36+
37+
/**
38+
* @return callable
39+
*/
40+
public function getPathResolver(): callable
41+
{
42+
return $this->pathResolver;
43+
}
44+
45+
/**
46+
* @param callable $pathResolver
47+
*/
48+
public function setPathResolver(callable $pathResolver)
49+
{
50+
$this->pathResolver = $pathResolver;
51+
}
52+
}

0 commit comments

Comments
 (0)