1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yemenifree\LaravelArabicNumbersMiddleware\Middleware; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Illuminate\Foundation\Http\Middleware\TransformsRequest; |
7
|
|
|
|
8
|
|
|
abstract class BaseNumbersMiddleware extends TransformsRequest |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
protected $except = []; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $from = 'eastern'; |
15
|
|
|
|
16
|
|
|
/** @var array */ |
17
|
|
|
protected $easternNumbers = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']; |
18
|
|
|
|
19
|
|
|
/** @var array|mixed */ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* BaseNumbersMiddleware constructor. |
24
|
|
|
* |
25
|
|
|
* @param Repository $config |
26
|
|
|
*/ |
27
|
10 |
|
public function __construct(Repository $config) |
28
|
|
|
{ |
29
|
10 |
|
$this->config = $config->get('arabic-numbers-middleware'); |
30
|
10 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* get except fields. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
10 |
|
public function getExcept() |
38
|
|
|
{ |
39
|
10 |
|
return $this->except + $this->getOption('except_from_all', []) + $this->attributes; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* get options from config. |
44
|
|
|
* @param string $key |
45
|
|
|
* @param null $default |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
10 |
|
protected function getOption($key, $default = null) |
49
|
|
|
{ |
50
|
10 |
|
return array_get($this->config, $key, $default); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Transform the given value. |
55
|
|
|
* |
56
|
|
|
* @param string $key |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
10 |
|
protected function transform($key, $value) |
61
|
|
|
{ |
62
|
10 |
|
if (in_array($key, $this->getExcept(), true)) { |
63
|
6 |
|
return $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
return is_string($value) ? $this->transformNumber($value) : $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* transform eastern/(arabic|english) numbers to (arabic|english)/eastern numbers inside string. |
71
|
|
|
* |
72
|
|
|
* @param string $value |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
10 |
|
protected function transformNumber($value) |
76
|
|
|
{ |
77
|
10 |
|
return strtr($value, $this->getNumbers()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* get array numbers to transforms. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
10 |
|
protected function getNumbers() |
86
|
|
|
{ |
87
|
10 |
|
return $this->isFromEastern() ? array_flip($this->getEasternNumbers()) : $this->getEasternNumbers(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* check if transform from (arabic|english) to eastern. |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
10 |
|
public function isFromEastern() |
96
|
|
|
{ |
97
|
10 |
|
return $this->from === 'eastern'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get eastern numbers array. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
10 |
|
public function getEasternNumbers() |
106
|
|
|
{ |
107
|
10 |
|
return $this->easternNumbers; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|