Skip to content

Commit 78ad81b

Browse files
committed
New functionalities
1 parent 2683cbc commit 78ad81b

21 files changed

+1008
-66
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"require": {
1515
"php": "^7.1",
1616
"ext-mbstring": "*",
17-
"symfony/property-access": "^4.3"
17+
"symfony/property-access": "^4.3",
18+
"doctrine/annotations": "^1.8"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": ">=6.0 <8.0",

src/Annotations.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
4+
namespace Pechynho\Utility;
5+
6+
7+
use Doctrine\Common\Annotations\AnnotationException;
8+
use Doctrine\Common\Annotations\AnnotationReader;
9+
use Doctrine\Common\Annotations\AnnotationRegistry;
10+
use RuntimeException;
11+
12+
/**
13+
* @author Jan Pech <[email protected]>
14+
*/
15+
class Annotations
16+
{
17+
/**
18+
* @param string $class
19+
* @param string $property
20+
* @param string $annotation
21+
* @return object|null
22+
*/
23+
public static function getPropertyAnnotation(string $class, string $property, string $annotation)
24+
{
25+
ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__);
26+
ParamsChecker::notWhiteSpaceOrNullString('$property', $property, __METHOD__);
27+
ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__);
28+
$property = Reflections::getProperty($class, $property);
29+
$annotation = self::createAnnotationReader()->getPropertyAnnotation($property, $annotation);
30+
return $annotation;
31+
}
32+
33+
/**
34+
* @param string $class
35+
* @param string $method
36+
* @param string $annotation
37+
* @return object|null
38+
*/
39+
public static function getMethodAnnotation(string $class, string $method, string $annotation)
40+
{
41+
ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__);
42+
ParamsChecker::notWhiteSpaceOrNullString('$method', $method, __METHOD__);
43+
ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__);
44+
$method = Reflections::getMethod($class, $method);
45+
$annotation = self::createAnnotationReader()->getMethodAnnotation($method, $annotation);
46+
return $annotation;
47+
}
48+
49+
/**
50+
* @param string $class
51+
* @param string $annotation
52+
* @return object|null
53+
*/
54+
public static function getClassAnnotation(string $class, string $annotation)
55+
{
56+
ParamsChecker::notWhiteSpaceOrNullString('$class', $class, __METHOD__);
57+
ParamsChecker::notWhiteSpaceOrNullString('$annotation', $annotation, __METHOD__);
58+
$reflectionClass = Reflections::createReflectionClass($class);
59+
$annotationReader = self::createAnnotationReader();
60+
while ($reflectionClass !== false)
61+
{
62+
$annotationInstance = $annotationReader->getClassAnnotation($reflectionClass, $annotation);
63+
if ($annotationInstance !== null)
64+
{
65+
return $annotationInstance;
66+
}
67+
$reflectionClass = $reflectionClass->getParentClass();
68+
}
69+
return null;
70+
}
71+
72+
/**
73+
* @return AnnotationReader
74+
*/
75+
public static function createAnnotationReader()
76+
{
77+
try
78+
{
79+
if (method_exists(AnnotationRegistry::class, "registerLoader"))
80+
{
81+
AnnotationRegistry::registerLoader("class_exists");
82+
}
83+
return new AnnotationReader();
84+
}
85+
catch (AnnotationException $exception)
86+
{
87+
throw new RuntimeException(sprintf("Creating new instance of '%s' class was not successful.", AnnotationReader::class));
88+
}
89+
}
90+
}

src/Arrays.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Symfony\Component\PropertyAccess\PropertyPathInterface;
1212
use Traversable;
1313

14+
/**
15+
* @author Jan Pech <[email protected]>
16+
*/
1417
class Arrays
1518
{
1619
/** @var string */
@@ -245,14 +248,14 @@ public static function min(iterable $subject, $propertyPath = null)
245248
*/
246249
public static function itemsWithMin(iterable $subject, $propertyPath = null): array
247250
{
248-
if (Arrays::isEmpty($subject))
249-
{
250-
throw new InvalidArgumentException('Parameter $subject is empty.');
251-
}
252251
if ($propertyPath !== null && !is_callable($propertyPath) && !is_string($propertyPath) && !$propertyPath instanceof PropertyPathInterface)
253252
{
254253
throw new InvalidArgumentException('Parameter $propertyPath has to be NULL, callable, string or instance of ' . PropertyPathInterface::class . '.');
255254
}
255+
if (Arrays::isEmpty($subject))
256+
{
257+
return [];
258+
}
256259
$minValue = null;
257260
$items = [];
258261
foreach ($subject as $item)
@@ -305,14 +308,14 @@ public static function max(iterable $subject, $propertyPath = null)
305308
*/
306309
public static function itemsWithMax(iterable $subject, $propertyPath = null): array
307310
{
308-
if (Arrays::isEmpty($subject))
309-
{
310-
throw new InvalidArgumentException('Parameter $subject is empty.');
311-
}
312311
if ($propertyPath !== null && !is_callable($propertyPath) && !is_string($propertyPath) && !$propertyPath instanceof PropertyPathInterface)
313312
{
314313
throw new InvalidArgumentException('Parameter $propertyPath has to be NULL, callable, string or instance of ' . PropertyPathInterface::class . '.');
315314
}
315+
if (Arrays::isEmpty($subject))
316+
{
317+
return [];
318+
}
316319
$maxValue = null;
317320
$items = [];
318321
foreach ($subject as $item)
@@ -700,4 +703,4 @@ public static function extract(array $subject, string $keyPath, &$value = null)
700703
}
701704
return false;
702705
}
703-
}
706+
}

src/Dates.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
4+
namespace Pechynho\Utility;
5+
6+
7+
use DateTime;
8+
use Exception;
9+
use RuntimeException;
10+
11+
/**
12+
* @author Jan Pech <[email protected]>
13+
*/
14+
class Dates
15+
{
16+
/**
17+
* @param boolean $maximumTime
18+
* @return DateTime
19+
*/
20+
public static function today(bool $maximumTime = false)
21+
{
22+
$today = Dates::now();
23+
if ($maximumTime)
24+
{
25+
$today->setTime(23, 59, 59);
26+
}
27+
else
28+
{
29+
$today->setTime(0, 0);
30+
}
31+
return $today;
32+
}
33+
34+
/**
35+
* @return DateTime
36+
*/
37+
public static function now()
38+
{
39+
try
40+
{
41+
$today = new DateTime();
42+
}
43+
catch (Exception $exception)
44+
{
45+
throw new RuntimeException("Creating new blank instance [e.g. new DateTime()] of DateTime was not successful.");
46+
}
47+
return $today;
48+
}
49+
50+
/**
51+
* @param int $timestamp
52+
* @return DateTime
53+
*/
54+
public static function fromTimestamp(int $timestamp)
55+
{
56+
ParamsChecker::range('$timestamp', $timestamp, 0, null, __METHOD__);
57+
try
58+
{
59+
$dateTime = new DateTime("@" . $timestamp);
60+
}
61+
catch (Exception $exception)
62+
{
63+
throw new RuntimeException(sprintf("Creating new instance of DateTime from timestamp '%s' was not successful.", $timestamp));
64+
}
65+
return $dateTime;
66+
}
67+
}

src/FileSystem.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use InvalidArgumentException;
88

9+
/**
10+
* @author Jan Pech <[email protected]>
11+
*/
912
class FileSystem
1013
{
1114
/** @var string */
@@ -327,4 +330,4 @@ public static function scanDirectory(string $directory, string $mode = FileSyste
327330
}
328331
return $output;
329332
}
330-
}
333+
}

src/Formatting.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use InvalidArgumentException;
88

9+
/**
10+
* @author Jan Pech <[email protected]>
11+
*/
912
class Formatting
1013
{
1114
/** @var string[] */
@@ -81,4 +84,4 @@ public static function formatFileSize(int $bytes, ?string $unit = null, ?string
8184
if ($power === null) $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
8285
return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
8386
}
84-
}
87+
}

0 commit comments

Comments
 (0)