-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from heimrichhannot/feature/static-utils
Add static utilities
- Loading branch information
Showing
9 changed files
with
290 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\StaticUtil; | ||
|
||
/** | ||
* @internal | ||
* @codeCoverageIgnore | ||
*/ | ||
abstract class AbstractStaticUtil | ||
{ | ||
public function __call(string $name, array $arguments) | ||
{ | ||
return static::$name(...$arguments); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\StaticUtil; | ||
|
||
class SUtils | ||
{ | ||
protected static array $instances = []; | ||
|
||
public static function array(): StaticArrayUtil | ||
{ | ||
return static::getInstance(StaticArrayUtil::class); | ||
} | ||
|
||
public static function class(): StaticClassUtil | ||
{ | ||
return static::getInstance(StaticClassUtil::class); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param class-string<T> $class | ||
* @return T The instance of the given class. | ||
*/ | ||
protected static function getInstance(string $class): object | ||
{ | ||
if (!isset(static::$instances[$class])) { | ||
static::$instances[$class] = new $class; | ||
} | ||
|
||
return static::$instances[$class]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\StaticUtil; | ||
|
||
class StaticArrayUtil extends AbstractStaticUtil | ||
{ | ||
/** | ||
* Insert a new entry before a specific or multiple keys in array. | ||
* If the keys not exist, the new entry is added to the end of the array. | ||
* Array is passed as reference. | ||
* | ||
* @param array $array Array the new entry should inserted to | ||
* @param array|string $keys The key or keys where the new entry should be added before | ||
* @param string $newKey The key of the entry that should be added | ||
* @param mixed $newValue The value of the entry that should be added | ||
*/ | ||
public static function insertBeforeKey(array &$array, array|string $keys, string $newKey, mixed $newValue): void | ||
{ | ||
if (!is_array($keys)) { | ||
$keys = [$keys]; | ||
} | ||
|
||
if (array_intersect($keys, array_keys($array))) { | ||
$new = []; | ||
|
||
foreach ($array as $k => $value) { | ||
if (\in_array($k, $keys)) { | ||
$new[$newKey] = $newValue; | ||
} | ||
$new[$k] = $value; | ||
} | ||
$array = $new; | ||
} else { | ||
$array[$newKey] = $newValue; | ||
} | ||
} | ||
|
||
/** | ||
* Insert a value into an existing array by key name. | ||
* | ||
* Additional options: | ||
* - (bool) strict: Strict behavior for array search. Default false | ||
* - (bool) attachMissingKey: Attach value to the end of the array if the key does not exist. Default: true | ||
* - (int) offset: Add additional offset. | ||
* | ||
* @param array $array The target array | ||
* @param string $key the existing target key in the array | ||
* @param mixed $value the new value to be inserted | ||
* @param array{ | ||
* strict?: bool, | ||
* attachMissingKey?: bool, | ||
* offset?: int | ||
* } $options Additional options | ||
*/ | ||
public static function insertAfterKey(array &$array, string $key, mixed $value, string $newKey = null, array $options = []): void | ||
{ | ||
$options = array_merge([ | ||
'strict' => false, | ||
'attachMissingKey' => true, | ||
'offset' => 0, | ||
], $options); | ||
|
||
$keys = array_keys($array); | ||
$index = array_search($key, $keys, $options['strict']); | ||
|
||
if (false === $index && false === $options['attachMissingKey']) { | ||
return; | ||
} | ||
$pos = false === $index ? count($array) : $index + 1; | ||
$pos = $pos + $options['offset']; | ||
|
||
if ($newKey) { | ||
$value = [$newKey => $value]; | ||
} else { | ||
$value = [$value]; | ||
} | ||
|
||
$array = array_combine( | ||
array_merge(array_slice($keys, 0, $pos), array_keys($value), array_slice($keys, $pos)), | ||
array_merge(array_slice($array, 0, $pos), $value, array_slice($array, $pos)) | ||
); | ||
} | ||
|
||
/** | ||
* Removes a value from an array. | ||
* | ||
* @return bool Returns true if the value has been found and removed, false in other cases | ||
*/ | ||
public static function removeValue(mixed $value, array &$array): bool | ||
{ | ||
$position = array_search($value, $array); | ||
|
||
if (false !== $position) { | ||
unset($array[$position]); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\StaticUtil; | ||
|
||
class StaticClassUtil extends AbstractStaticUtil | ||
{ | ||
/** | ||
* Check if a class or any of its parents implements a trait. | ||
* | ||
* @param object|class-string $class The class to check. | ||
* @param class-string $trait The trait to check for. | ||
* @return bool True if the class or any of its parents implements the trait, false otherwise. | ||
*/ | ||
public static function hasTrait(object|string $class, string $trait): bool | ||
{ | ||
do { | ||
if (in_array($trait, class_uses($class))) { | ||
return true; | ||
} | ||
} while ($class = get_parent_class($class)); | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil; | ||
|
||
use Contao\TestCase\ContaoTestCase; | ||
use HeimrichHannot\UtilsBundle\StaticUtil\StaticArrayUtil; | ||
use HeimrichHannot\UtilsBundle\StaticUtil\StaticClassUtil; | ||
use HeimrichHannot\UtilsBundle\StaticUtil\SUtils; | ||
|
||
class SUtilsTest extends ContaoTestCase | ||
{ | ||
public function testSUtils() | ||
{ | ||
$this->assertInstanceOf(StaticArrayUtil::class, SUtils::array()); | ||
$this->assertInstanceOf(StaticClassUtil::class, SUtils::class()); | ||
} | ||
} |
Oops, something went wrong.