Skip to content

Commit b2be810

Browse files
committed
update
2 parents 13c56fb + 6b6c823 commit b2be810

6 files changed

Lines changed: 200 additions & 18 deletions

File tree

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Maurits van der Schee
3+
Copyright (c) 2025 Maurits van der Schee
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
}
1010
],
1111
"require": {
12+
<<<<<<< HEAD
1213
"php": ">=8",
1314
"ext-memcache": "*",
15+
=======
16+
"php": ">=5.3.0",
17+
"ext-memcached": "*",
18+
>>>>>>> 6b6c8232749ed3772a26d7b4b4cbc99c22309371
1419
"ext-mysqli": "*"
1520
},
1621
"require-dev": {

src/Cache.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
class Cache
66
{
7-
public static string $prefix = 'mintyphp';
8-
public static string $servers = '127.0.0.1';
9-
public static int $compressTreshold = 20000;
10-
public static float $compressSavings = 0.2;
7+
public static $prefix = 'mintyphp';
8+
public static $servers = '127.0.0.1';
119

12-
protected static ?\Memcache $memcache = null;
10+
/**
11+
* @var \Memcached
12+
*/
13+
protected static $memcache = null;
1314

1415
protected static function initialize(): void
1516
{
16-
if (!self::$memcache) {
17-
self::$memcache = new \Memcache();
18-
$servers = explode(',', self::$servers);
17+
if (!static::$memcache) {
18+
static::$memcache = new \Memcached();
19+
$servers = explode(',', static::$servers);
1920
$servers = array_map(function ($server) {
2021
$server = explode(':', trim($server));
2122
if (count($server) == 1) $server[1] = '11211';
@@ -24,7 +25,6 @@ protected static function initialize(): void
2425
foreach ($servers as $server) {
2526
self::$memcache->addServer($server[0], intval($server[1]));
2627
}
27-
self::$memcache->setCompressThreshold(self::$compressTreshold, self::$compressSavings);
2828
}
2929
}
3030

@@ -56,8 +56,8 @@ protected static function variable(mixed $var): string
5656
public static function add(string $key, mixed $var, int $expire = 0): bool
5757
{
5858
if (Debugger::$enabled) $time = microtime(true);
59-
if (!self::$memcache) self::initialize();
60-
$res = self::$memcache->add(self::$prefix . $key, $var, 0, $expire);
59+
if (!static::$memcache) static::initialize();
60+
$res = static::$memcache->add(static::$prefix . $key, $var, $expire);
6161
if (Debugger::$enabled) {
6262
$duration = microtime(true) - $time;
6363
$command = 'add';
@@ -134,8 +134,8 @@ public static function increment(string $key, int $value = 1): int
134134
public static function replace(string $key, mixed $var, int $expire = 0): bool
135135
{
136136
if (Debugger::$enabled) $time = microtime(true);
137-
if (!self::$memcache) self::initialize();
138-
$res = self::$memcache->replace(self::$prefix . $key, $var, 0, $expire);
137+
if (!static::$memcache) static::initialize();
138+
$res = static::$memcache->replace(static::$prefix . $key, $var, $expire);
139139
if (Debugger::$enabled) {
140140
$duration = microtime(true) - $time;
141141
$command = 'replace';
@@ -150,8 +150,8 @@ public static function replace(string $key, mixed $var, int $expire = 0): bool
150150
public static function set(string $key, mixed $var, int $expire = 0): bool
151151
{
152152
if (Debugger::$enabled) $time = microtime(true);
153-
if (!self::$memcache) self::initialize();
154-
$res = self::$memcache->set(self::$prefix . $key, $var, 0, $expire);
153+
if (!static::$memcache) static::initialize();
154+
$res = static::$memcache->set(static::$prefix . $key, $var, $expire);
155155
if (Debugger::$enabled) {
156156
$duration = microtime(true) - $time;
157157
$command = 'set';

src/Curl.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,13 @@ public static function call(string $method, string $url, string|array $data = ''
108108
list($head, $body) = array($result, '');
109109
} else {
110110
list($head, $body) = explode("\r\n\r\n", $result, 2);
111-
while (preg_match('|\s+100\s+Continue|', explode("\r\n", $head)[0])) {
111+
$statusCodes = [100];
112+
if ($options['CURLOPT_FOLLOWLOCATION'] ?? false) {
113+
$statusCodes[] = 301;
114+
$statusCodes[] = 302;
115+
}
116+
$regex = '/\s+(' . implode('|', $statusCodes) . ')\s+/';
117+
while (preg_match($regex, explode("\r\n", $head)[0])) {
112118
list($head, $body) = explode("\r\n\r\n", $body, 2);
113119
}
114120
}

src/I18n.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
3+
namespace MintyPHP;
4+
5+
class I18n
6+
{
7+
private static $strings = [];
8+
9+
public static $domain = 'default';
10+
public static $locale = ''; // should be either: 'en', 'de', 'fr', 'nl'
11+
public static $defaultLocale = 'en';
12+
13+
public static $formats = [
14+
'currency' => [
15+
'en' => ['thousandSeparator' => ',', 'decimalSeparator' => '.'],
16+
'de' => ['thousandSeparator' => '.', 'decimalSeparator' => ','],
17+
'fr' => ['thousandSeparator' => '.', 'decimalSeparator' => ','],
18+
'nl' => ['thousandSeparator' => '.', 'decimalSeparator' => ','],
19+
],
20+
'datetime' => [
21+
'en' => ['date' => 'm/d/Y', 'datetime' => 'm/d/Y H:i:s', 'time' => 'H:i:s'],
22+
'de' => ['date' => 'd.m.Y', 'datetime' => 'd.m.Y H:i:s', 'time' => 'H:i:s'],
23+
'fr' => ['date' => 'd-m-Y', 'datetime' => 'd-m-Y H:i:s', 'time' => 'H:i:s'],
24+
'nl' => ['date' => 'd-m-Y', 'datetime' => 'd-m-Y H:i:s', 'time' => 'H:i:s'],
25+
],
26+
'weekDays' => [
27+
'en' => ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
28+
'de' => ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'],
29+
'fr' => ['dimanche', 'lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'],
30+
'nl' => ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag', 'zondag'],
31+
],
32+
'monthNames' => [
33+
'en' => ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
34+
'de' => ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
35+
'fr' => ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'],
36+
'nl' => ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
37+
]
38+
39+
];
40+
41+
public static function price($price, $minDecimals = 2, $maxDecimals = 2): string
42+
{
43+
if ($price === null) return '';
44+
return "" . self::currency($price, $minDecimals, $maxDecimals);
45+
}
46+
47+
public static function currency($currency, $minDecimals = 2, $maxDecimals = 2): string
48+
{
49+
if ($currency === null) return '';
50+
51+
$formats = self::$formats['currency'];
52+
53+
$number = rtrim(sprintf("%0.{$maxDecimals}F", $currency), '0');
54+
$decimalPos = strpos($number, '.');
55+
if ($decimalPos === false) {
56+
$number .= '.';
57+
}
58+
list($whole, $fraction) = explode('.', $number);
59+
if ($number < 0) {
60+
$sign = '-';
61+
$whole = -1 * (int)$whole;
62+
} else {
63+
$sign = '';
64+
}
65+
$whole = (string) $whole;
66+
$format = $formats[self::$locale] ?? $formats[self::$defaultLocale];
67+
$whole = strrev(implode($format['thousandSeparator'], str_split(strrev($whole), 3)));
68+
$fraction = str_pad($fraction, $minDecimals, '0');
69+
return $sign . $whole . $format['decimalSeparator'] . $fraction;
70+
}
71+
72+
public static function date($str): string
73+
{
74+
return $str ? self::formatDateTime('date', "$str") : '';
75+
}
76+
77+
public static function dateUtc($str): string
78+
{
79+
return $str ? self::formatDateTime('date', "$str UTC") : '';
80+
}
81+
82+
public static function duration(int $seconds, bool $trim = false): string
83+
{
84+
$hours = floor($seconds / 3600);
85+
$seconds -= $hours * 3600;
86+
$minutes = floor($seconds / 60);
87+
$seconds -= $minutes * 60;
88+
$formatted = sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
89+
if ($trim) {
90+
if (substr($formatted, 0, 3) == '00:') {
91+
$formatted = substr($formatted, 3);
92+
}
93+
}
94+
return $formatted;
95+
}
96+
97+
public static function datetime($str): string
98+
{
99+
return $str ? self::formatDateTime('datetime', "$str") : '';
100+
}
101+
102+
public static function datetimeUtc($str): string
103+
{
104+
return $str ? self::formatDateTime('datetime', "$str UTC") : '';
105+
}
106+
107+
public static function time(int $hours, int $minutes, int $seconds = 0): string
108+
{
109+
return self::formatDateTime('time', date('Y-m-d ') . sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds));
110+
}
111+
112+
public static function timeUtc(int $hours, int $minutes, int $seconds = 0): string
113+
{
114+
return self::formatDateTime('time', date('Y-m-d ') . sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds) . 'UTC');
115+
}
116+
117+
public static function weekDay(int $dayOfWeek): string
118+
{
119+
$weekDays = self::$formats['weekDays'];
120+
return $weekDays[self::$locale][$dayOfWeek] ?? $weekDays[self::$defaultLocale][$dayOfWeek];
121+
}
122+
123+
public static function monthName(int $monthOfYear): string
124+
{
125+
$monthNames = self::$formats['monthNames'];
126+
return $monthNames[self::$locale][$monthOfYear - 1] ?? $monthNames[self::$defaultLocale][$monthOfYear - 1];
127+
}
128+
129+
private static function formatDateTime(string $type, string $str): string
130+
{
131+
$formats = self::$formats['datetime'];
132+
$format = $formats[self::$locale] ?? $formats[self::$defaultLocale];
133+
return date($format[$type], strtotime($str));
134+
}
135+
136+
public static function datetimeShort($str): string
137+
{
138+
if (!$str) {
139+
return '';
140+
}
141+
$sep = self::$formats['datetime']['en']['date'][1];
142+
if (date('Y', strtotime($str)) != date('Y')) {
143+
return implode($sep, array_map('intval', explode($sep, self::formatDateTime('date', "$str"))));
144+
}
145+
if (time() - strtotime($str) < 24 * 60 * 60) {
146+
return implode(':', array_slice(explode(':', self::formatDateTime('time', "$str")), 0, 2));
147+
}
148+
if (time() - strtotime($str) < 7 * 24 * 60 * 60) {
149+
$day = substr(self::weekDay(date('N', strtotime($str))), 0, 2);
150+
$time = implode(':', array_slice(explode(':', self::formatDateTime('time', "$str")), 0, 2));
151+
return "$day $time";
152+
}
153+
$day = substr(self::weekDay(date('N', strtotime($str))), 0, 2);
154+
$date = implode($sep, array_map('intval', array_slice(explode($sep, self::formatDateTime('date', "$str")), 0, 2)));
155+
return "$day $date";
156+
}
157+
158+
public static function translate(string $id)
159+
{
160+
// read from disk or cache
161+
if (!isset(self::$strings[self::$domain][self::$locale])) {
162+
// load from disk
163+
$filename = 'i18n/' . self::$domain . '_' . self::$locale . '.json';
164+
if (file_exists($filename)) {
165+
self::$strings[self::$domain][self::$locale] = json_decode(file_get_contents($filename), true);
166+
}
167+
}
168+
// lookup id
169+
return self::$strings[self::$domain][self::$locale][$id] ?? $id;
170+
}
171+
}

src/Orm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Orm
66
{
77
/**
8-
* @param array<string, ?string> $object
8+
* @var array<string, ?string> $object
99
*/
1010
public static function insert(string $tableName, array $object): int
1111
{

0 commit comments

Comments
 (0)