Skip to content

Commit c343ff8

Browse files
committed
New functionalities
1 parent 85e788d commit c343ff8

21 files changed

+1044
-79
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"require": {
2020
"php": "^5.6",
2121
"ext-mbstring": "*",
22-
"symfony/property-access": "^3.4"
22+
"symfony/property-access": "^3.4",
23+
"doctrine/annotations": "^1.8"
2324
},
2425
"require-dev": {
2526
"phpunit/phpunit": "^5.7",

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($class, $property, $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($class, $method, $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($class, $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: 15 additions & 28 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 */
@@ -305,14 +308,14 @@ public static function itemsWithMin($subject, $propertyPath = null)
305308
{
306309
throw new InvalidArgumentException('Parameter $subject has to be type of array or Traversable.');
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
$minValue = null;
317320
$items = [];
318321
foreach ($subject as $item)
@@ -373,14 +376,14 @@ public static function itemsWithMax($subject, $propertyPath = null)
373376
{
374377
throw new InvalidArgumentException('Parameter $subject has to be type of array or Traversable.');
375378
}
376-
if (Arrays::isEmpty($subject))
377-
{
378-
throw new InvalidArgumentException('Parameter $subject is empty.');
379-
}
380379
if ($propertyPath !== null && !is_callable($propertyPath) && !is_string($propertyPath) && !$propertyPath instanceof PropertyPathInterface)
381380
{
382381
throw new InvalidArgumentException('Parameter $propertyPath has to be NULL, callable, string or instance of ' . PropertyPathInterface::class . '.');
383382
}
383+
if (Arrays::isEmpty($subject))
384+
{
385+
return [];
386+
}
384387
$maxValue = null;
385388
$items = [];
386389
foreach ($subject as $item)
@@ -518,10 +521,6 @@ public static function groupBy($subject, $propertyPath)
518521
*/
519522
public static function binarySearch(array $subject, $value)
520523
{
521-
if (!is_array($subject))
522-
{
523-
throw new InvalidArgumentException('Parameter $subject has to be type of array.');
524-
}
525524
$left = 0;
526525
$right = count($subject) - 1;
527526
while ($left <= $right)
@@ -638,12 +637,8 @@ public static function contains($subject, $value)
638637
* @param $value
639638
* @return string|int|null
640639
*/
641-
public static function keyOf($subject, $value)
640+
public static function keyOf(array $subject, $value)
642641
{
643-
if (!is_array($subject))
644-
{
645-
throw new InvalidArgumentException('Parameter $subject has to be type of array.');
646-
}
647642
$key = array_search($value, $subject, true);
648643
return $key === false ? null : $key;
649644
}
@@ -725,12 +720,8 @@ public static function reverse($subject)
725720
* @param array $subject
726721
* @return array
727722
*/
728-
public static function flip($subject)
723+
public static function flip(array $subject)
729724
{
730-
if (!is_array($subject))
731-
{
732-
throw new InvalidArgumentException('Parameter $subject has to be type of array.');
733-
}
734725
return array_flip($subject);
735726
}
736727

@@ -813,12 +804,8 @@ public static function mapByProperty($subject, $keyPropertyPath)
813804
* @param mixed $value
814805
* @return boolean
815806
*/
816-
public static function extract($subject, $keyPath, &$value = null)
807+
public static function extract(array $subject, $keyPath, &$value = null)
817808
{
818-
if (!is_array($subject))
819-
{
820-
throw new InvalidArgumentException('Parameter $subject has to be type of array.');
821-
}
822809
if (!is_string($keyPath) || Strings::isNullOrWhiteSpace($keyPath))
823810
{
824811
throw new InvalidArgumentException('Parameter $keyPath has to be non empty ("") and non-whitespace string.');
@@ -840,4 +827,4 @@ public static function extract($subject, $keyPath, &$value = null)
840827
}
841828
return false;
842829
}
843-
}
830+
}

src/Dates.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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($maximumTime = false)
21+
{
22+
ParamsChecker::isBool('$maximumTime', $maximumTime, __METHOD__);
23+
$today = Dates::now();
24+
if ($maximumTime)
25+
{
26+
$today->setTime(23, 59, 59);
27+
}
28+
else
29+
{
30+
$today->setTime(0, 0);
31+
}
32+
return $today;
33+
}
34+
35+
/**
36+
* @return DateTime
37+
*/
38+
public static function now()
39+
{
40+
try
41+
{
42+
$today = new DateTime();
43+
}
44+
catch (Exception $exception)
45+
{
46+
throw new RuntimeException("Creating new blank instance [e.g. new DateTime()] of DateTime was not successful.");
47+
}
48+
return $today;
49+
}
50+
51+
/**
52+
* @param int $timestamp
53+
* @return DateTime
54+
*/
55+
public static function fromTimestamp($timestamp)
56+
{
57+
ParamsChecker::range('$timestamp', $timestamp, 0, null, __METHOD__);
58+
try
59+
{
60+
$dateTime = new DateTime("@" . $timestamp);
61+
}
62+
catch (Exception $exception)
63+
{
64+
throw new RuntimeException(sprintf("Creating new instance of DateTime from timestamp '%s' was not successful.", $timestamp));
65+
}
66+
return $dateTime;
67+
}
68+
}

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 */
@@ -415,4 +418,4 @@ public static function scanDirectory($directory, $mode = FileSystem::SCAN_ALL, $
415418
}
416419
return $output;
417420
}
418-
}
421+
}

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[] */
@@ -112,4 +115,4 @@ public static function formatFileSize($bytes, $unit = null, $format = null, $use
112115
if ($power === null) $power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
113116
return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
114117
}
115-
}
118+
}

0 commit comments

Comments
 (0)