selami / stdlib
Standard Library for Selami libraries.
Requires
- php: ^8.0
- ext-dom: *
- psr/container: ^1.0
Requires (Dev)
- codeception/codeception: ^4.1
- codeception/module-asserts: ^1.2
- doctrine/coding-standard: ^9.0
- laminas/laminas-servicemanager: ^3.6
- php-coveralls/php-coveralls: ^v2.2.0
- squizlabs/php_codesniffer: ^3.6
Suggests
- ext-intl: Slugify needs this extension
- ext-libxml: TagAndAttributeRemover needs this extention
README
Standard Library for Selami and other libraries.
EqualsBuilder
This class provides methods to build a equals method for any class. Intended to be use to compare Value Objects.
<?php declare(strict_types=1); use Selami\Stdlib\EqualsBuilder; class SomeValueObject() { private $value1; private $value2; public function __construct(string $value1, string $value2) { $this->value1 = $value1; $this->value2 = $value2; } public function getValue1() :string { return $value1; } public function getValue2() : string { return $value2; } public function equals($otherObject) : bool { return EqualsBuilder::create() ->append($this->value1, $otherObject->getValue1()) ->append($this->value2, $otherObject->getValue2()) ->isEquals(); } }
Resolver
This class provides a method to obtain typehints of a method. Intended to be used to autowire classes.
<?php declare(strict_types=1); use Selami\Stdlib\Resolver; class BlogService {} class Controller { private $argument; private $service; public function __construct(BlogService $service, array $argument) { $this->service = $service; $this->argument = $argument; } } $arguments = Resolver::getParameterHints(Controller::class, '__construct'); var_dump($arguments); /* Prints array(2) { ["service"]=> string(11) "BlogService" ["argument"]=> string(5) "array" } */
CaseConverter
This class provides methods to convert strings to camelCase, PascalCase or snake_case string.
<?php declare(strict_types=1); use Selami\Stdlib\CaseConverter; $source = 'test string'; $result = CaseConverter::toCamelCase($source); // returns: testString $result = CaseConverter::toPascalCase($source); // returns: TestString $result = CaseConverter::toSnakeCase($source); // returns: test_string
Git\Version
This class provides a methods to get short version of git. For deployments using git, it can be used to give version numbers to JS, CSS files to ensure to cache updated version of these files.
<?php // common.php declare(strict_types=1); use Selami\Stdlib\Git\Version; $gitVersion = Version::short(); $twig->addGlobal('version', $gitVersion);
<!-- main.twig --> <html> <head> <link href="/assets/css/main.css?v={{version}}" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="/assets/js/main.min.js?v={{version}}"></script> </head> <body> ... </body> </html>
BaseUrlExtractor
This class provides a methods to get base url where applications runs.
Caution: This method may not return real base url if you are behind some services like Cloudflare and when you use Flexible SSL feature.
<?php declare(strict_types=1); use Selami\Stdlib\BaseUrlExtractor; $baseUrl = BaseUrlExtractor::getBaseUrl($_SERVER); echo $baseUrl; /* Prints base url like: http://127.0.0.1:8080 http://127.0.0.1:8080/myapp https://myapp.com */