Skip to content

Commit 2731255

Browse files
committed
Update
1 parent e4fed51 commit 2731255

36 files changed

+1535
-770
lines changed

src/Connection/Db.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
use \PDOException;
2121
use \PDO;
2222

23+
/**
24+
* Advanced database manipulation.
25+
*/
2326
class Db
2427
{
2528
/**
@@ -71,7 +74,8 @@ public function close() : void
7174
*/
7275
public function bind(string $bind, $value = null) : void
7376
{
74-
$this->parameters[sizeof($this->parameters)] = [":{$bind}", $value];
77+
$count = sizeof($this->parameters);
78+
$this->parameters[$count] = [":{$bind}", $value];
7579
}
7680

7781
/**
@@ -357,7 +361,7 @@ protected function getStatementType(string $sql = '') : mixed
357361
*/
358362
protected function log(?string $message = null, string $sql = null) : string
359363
{
360-
if ( empty($message) ) {
364+
if ( !$message ) {
361365
$message = 'Unhandled Exception';
362366
}
363367

src/Filesystem/Archive.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use \RecursiveIteratorIterator;
2020
use \RecursiveDirectoryIterator;
2121

22+
/**
23+
* Advanced archive manipulation.
24+
*/
2225
final class Archive extends File
2326
{
2427
/**
@@ -60,9 +63,11 @@ public static function compress(string $path, string $to = '', string $archive =
6063
$zip->addFile($p, basename($name));
6164
}
6265
}
66+
6367
} elseif ( self::isFile($path) ) {
6468
$zip->addFile($path, basename($path));
6569
}
70+
6671
$zip->close();
6772
return true;
6873
}
@@ -101,6 +106,7 @@ public static function uncompress(string $archive, string $to = '', bool $remove
101106
$zip->close();
102107
$status = true;
103108
}
109+
104110
} elseif ( self::isGzip($archive) ) {
105111
$status = self::unGzip($archive);
106112
}

src/Filesystem/Arrayify.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,35 @@
1515

1616
namespace FloatPHP\Classes\Filesystem;
1717

18+
/**
19+
* Advanced array manipulation.
20+
*/
1821
final class Arrayify
1922
{
2023
/**
21-
* Check array item.
24+
* Check array value.
2225
*
2326
* @access public
24-
* @param mixed $item
27+
* @param mixed $value
2528
* @param array $array
2629
* @return bool
2730
*/
28-
public static function inArray($item, array $array) : bool
31+
public static function inArray($value, array $array) : bool
2932
{
30-
return in_array($item, $array, true);
33+
return in_array($value, $array, strict: true);
3134
}
3235

3336
/**
3437
* Search array key.
3538
*
3639
* @access public
37-
* @param mixed $item
40+
* @param mixed $value
3841
* @param array $array
3942
* @return mixed
4043
*/
41-
public static function search($item, array $array) : mixed
44+
public static function search($value, array $array) : mixed
4245
{
43-
return array_search($item, $array, true);
46+
return array_search($value, $array, strict: true);
4447
}
4548

4649
/**
@@ -59,25 +62,31 @@ public static function merge(array ...$arrays) : array
5962
* Merge multidimensional arrays.
6063
*
6164
* @access public
62-
* @param array $override
63-
* @param array $arrays
65+
* @param array $default
66+
* @param array $target
6467
* @return array
6568
*/
66-
public static function mergeAll(array $override, array &$array) : array
69+
public static function mergeAll(array $default, array $target, bool $strict = false) : array
6770
{
68-
$merged = $override;
69-
foreach ($array as $key => $value) {
70-
if (
71-
TypeCheck::isArray($value)
72-
&& isset($merged[$key])
73-
&& TypeCheck::isArray($merged[$key])
74-
) {
75-
$merged[$key] = self::mergeAll($merged[$key], $value);
71+
foreach ($default as $key => $value) {
72+
73+
if ( TypeCheck::isArray($value) ) {
74+
$temp = $target[$key] ?? [];
75+
$target[$key] = self::mergeAll($value, $temp, $strict);
76+
7677
} else {
77-
$merged[$key] = $value;
78+
if ( !isset($target[$key]) ) {
79+
$target[$key] = $value;
80+
81+
} else {
82+
if ( $strict && TypeCheck::isEmpty($target[$key]) ) {
83+
$target[$key] = $value;
84+
}
85+
}
7886
}
7987
}
80-
return $merged;
88+
89+
return $target;
8190
}
8291

8392
/**
@@ -351,12 +360,8 @@ public static function uniqueMultiple(array $array) : array
351360
* @param bool $preserve (keys)
352361
* @return array
353362
*/
354-
public static function sort(array $array, $orderby = [], string $order = 'ASC', bool $preserve = false) : array
363+
public static function sort(array $array, $orderby, string $order = 'ASC', bool $preserve = false) : array
355364
{
356-
if ( !$orderby ) {
357-
return $array;
358-
}
359-
360365
if ( TypeCheck::isString($orderby) ) {
361366
$orderby = [$orderby => $order];
362367
}
@@ -394,6 +399,7 @@ public static function sort(array $array, $orderby = [], string $order = 'ASC',
394399

395400
if ( $preserve ) {
396401
uasort($array, $sort);
402+
397403
} else {
398404
usort($array, $sort);
399405
}

src/Filesystem/Converter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
use FloatPHP\Classes\Security\Tokenizer;
1919

20+
/**
21+
* Advanced types manipulation.
22+
*/
2023
final class Converter
2124
{
2225
/**

src/Filesystem/Exception.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
namespace FloatPHP\Classes\Filesystem;
1717

1818
/**
19-
* Exception and error handler helper.
19+
* Advanced exception manipulation.
2020
*/
2121
final class Exception extends \Exception
2222
{

src/Filesystem/File.php

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace FloatPHP\Classes\Filesystem;
1717

18+
/**
19+
* Advanced file manipulation.
20+
*/
1821
class File
1922
{
2023
/**
@@ -24,7 +27,7 @@ class File
2427
* @param string $path
2528
* @return array
2629
*/
27-
public static function analyse($path) : array
30+
public static function analyse(string $path) : array
2831
{
2932
return [
3033
'parent' => self::getParentDir($path),
@@ -539,6 +542,26 @@ public static function w(string $path, $input = '', bool $append = false) : bool
539542
return (bool)@file_put_contents($path, $input, $flag);
540543
}
541544

545+
/**
546+
* Read file using stream.
547+
*
548+
* @access public
549+
* @param string $path
550+
* @return mixed
551+
*/
552+
public static function read(string $path) : mixed
553+
{
554+
if ( self::exists($path) ) {
555+
if ( ($handler = fopen($path, 'r')) ) {
556+
$size = self::getFileSize($path);
557+
$content = fread($handler, $size);
558+
fclose($handler);
559+
return $content;
560+
}
561+
}
562+
return false;
563+
}
564+
542565
/**
543566
* Scan directory,
544567
* [ASC: 0],
@@ -635,8 +658,10 @@ public static function count(string $path = '.') : mixed
635658
/**
636659
* Parse ini file.
637660
*
638-
* [Normal : 0]
639-
*
661+
* [INI_SCANNER_NORMAL : 0].
662+
* [FILE_IGNORE_NEW_LINES : 2].
663+
* [FILE_SKIP_EMPTY_LINES : 4].
664+
*
640665
* @access public
641666
* @param string $path
642667
* @param bool $sections
@@ -645,11 +670,78 @@ public static function count(string $path = '.') : mixed
645670
*/
646671
public static function parseIni(string $path, bool $sections = false, int $mode = 0) : mixed
647672
{
648-
return parse_ini_file(
649-
Stringify::formatPath($path),
650-
$sections,
651-
$mode
652-
);
673+
$path = Stringify::formatPath($path);
674+
675+
if ( TypeCheck::isFunction('parse_ini_file') ) {
676+
return parse_ini_file($path, $sections, $mode);
677+
}
678+
679+
if ( !self::exists($path) || !self::isReadable($path) ) {
680+
throw new \RuntimeException("File not found or not readable: {$path}");
681+
}
682+
683+
$lines = file($path, 2 | 4);
684+
$data = [];
685+
$section = null;
686+
687+
foreach ($lines as $line) {
688+
$line = trim($line);
689+
690+
// Skip comments and empty lines
691+
if ( $line === '' || $line[0] === ';' || $line[0] === '#' ) {
692+
continue;
693+
}
694+
695+
// Remove trailing semicolon
696+
if ( substr($line, -1) === ';' ) {
697+
$line = substr($line, 0, -1);
698+
}
699+
700+
// Process sections
701+
if ( $line[0] === '[' && substr($line, -1) === ']' ) {
702+
if ( $sections ) {
703+
$section = substr($line, 1, -1);
704+
$data[$section] = [];
705+
}
706+
continue;
707+
}
708+
709+
// Process key-value pairs
710+
$keyValue = explode('=', $line, 2);
711+
if ( count($keyValue) !== 2 ) {
712+
throw new \RuntimeException("Invalid line in INI file: {$line}");
713+
}
714+
715+
[$key, $value] = array_map('trim', $keyValue);
716+
717+
// Parse booleans, null, and numbers
718+
if ( strtolower($value) === 'true' ) {
719+
$value = true;
720+
721+
} elseif ( strtolower($value) === 'false' ) {
722+
$value = false;
723+
724+
} elseif ( strtolower($value) === 'null' ) {
725+
$value = null;
726+
727+
} elseif ( is_numeric($value) ) {
728+
// Convert to int or float
729+
$value = $value + 0;
730+
731+
} else {
732+
// Remove quotes if present
733+
$value = trim($value, '"\'');
734+
}
735+
736+
if ( $sections && $section !== null ) {
737+
$data[$section][$key] = $value;
738+
739+
} else {
740+
$data[$key] = $value;
741+
}
742+
}
743+
744+
return $data;
653745
}
654746

655747
/**

src/Filesystem/Image.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace FloatPHP\Classes\Filesystem;
1717

18+
/**
19+
* Advanced image manipulation.
20+
*/
1821
final class Image
1922
{
2023
/**
@@ -115,4 +118,22 @@ public static function resize(string $path, int $width = 50, int $height = 50, b
115118
// Save (PNG)
116119
return imagepng($image, Stringify::lowercase($path), quality: 0);
117120
}
121+
122+
/**
123+
* Validate image mime.
124+
*
125+
* @access public
126+
* @param string $file
127+
* @return bool
128+
*/
129+
public static function isMime(string $file) : bool
130+
{
131+
return Validator::isMime($file, [
132+
'jpg' => 'image/jpeg',
133+
'jpeg' => 'image/jpeg',
134+
'bmp' => 'image/bmp',
135+
'png' => 'image/png',
136+
'gif' => 'image/gif'
137+
]);
138+
}
118139
}

src/Filesystem/Json.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace FloatPHP\Classes\Filesystem;
1717

18+
/**
19+
* Advanced JSON manipulation.
20+
*/
1821
final class Json extends File
1922
{
2023
/**
@@ -58,9 +61,9 @@ public static function encode($value) : mixed
5861
/**
5962
* Encode JSON using flags.
6063
*
61-
* [SLASHES: 64].
62-
* [PRETTY: 128].
63-
* [UNICODE: 256].
64+
* [SLASHES : 64].
65+
* [PRETTY : 128].
66+
* [UNICODE : 256].
6467
*
6568
* @access public
6669
* @param mixed $value

src/Filesystem/Logger.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
use FloatPHP\Interfaces\Classes\LoggerInterface;
1919

20+
/**
21+
* Built-in logger class.
22+
*/
2023
class Logger implements LoggerInterface
2124
{
2225
/**

0 commit comments

Comments
 (0)