1. Go to this page and download the library: Download cosmologist/gears library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Get the first item of any iterable
ArrayType::first([1, 2, 3]); // returns 1
// Find first even number
ArrayType::first([1, 3, 4, 6], fn($x) => $x % 2 === 0); // returns 4
// Use named argument for optional parameter
ArrayType::first([1, 2, 3], condition: fn($x) => $x > 1); // returns 2
// Returns null if no match or empty
ArrayType::first([], condition: fn($x) => $x > 0); // returns null
// Get the last item of any iterable
ArrayType::last([1, 2, 3]); // returns 3
// Find last even number
ArrayType::last([1, 4, 3, 6], fn($x) => $x % 2 === 0); // returns 6
// Use named argument for optional parameter
ArrayType::last([1, 2, 3], condition: fn($x) => $x < 3); // returns 2
// Returns null if no match or empty
ArrayType::last([], condition: fn($x) => $x > 0); // returns null
// Current locale used
NumberType::spellout(123.45); // one hundred twenty-three point four five
// Specific locale used
NumberType::spellout(123.45, 'ru'); // сто двадцать три целых сорок пять сотых
StringType::replaceFirst('name name name', 'name', 'title'); // 'title name name'
// Basic search in a UTF-8 string
$pos = StringType::position('Hello 世界', '世'); // returns 6
// Case-insensitive search
$pos = StringType::position('Hello World', 'WORLD', searchCaseSensitive: false); // returns 6
// Find last occurrence of substring
$pos = StringType::position('abcbc', 'bc', searchFromEnd: true); // returns 3
// Returns null when substring is not found (not false)
$pos = StringType::position('test', 'x'); // returns null
// Disable multibyte mode for ASCII-only strings
$pos = StringType::position('simple text', 'text', multibyteEncoding: false); // returns 6
// Returns 'Hello ' (before 'World' in a case-sensitive search)
StringType::strBefore('Hello World', 'World');
// Returns null because 'world' is not found with case-sensitive search
StringType::strBefore('Hello World', 'world');
// Returns 'Hello ' due to case-insensitive search
StringType::strBefore('Hello World', 'world', searchCaseSensitive: false);
// Returns 'Hello Wor' (before last 'l', searching from the end)
StringType::strBefore('Hello World', 'l', searchFromEnd: true);
// Returns 'Привет ' (correctly handles Cyrillic characters)
StringType::strBefore('Привет Мир', 'Мир');
// Returns null when needle is not found
StringType::strBefore('Test', 'xyz');
// Returns 'World' (after 'Hello ' in a case-sensitive search)
StringType::strAfter('Hello World', 'Hello ');
// Returns null because 'hello ' is not found when case-sensitive
StringType::strAfter('Hello World', 'hello ');
// Returns 'World' due to case-insensitive search
StringType::strAfter('Hello World', 'hello ', searchCaseSensitive: false);
// Returns 'd' (after the last occurrence of 'l', searching from the end)
StringType::strAfter('Hello World', 'l', searchFromEnd: true);
// Returns 'Мир' (correctly handles multibyte UTF-8 characters)
StringType::strAfter('Привет Мир', 'Привет ');
// Returns null when needle is at the end and nothing follows
StringType::strAfter('Test', 'st');
StringType::sentences('Fry me a Beaver. Fry me a Beaver! Fry me a Beaver? Fry me Beaver no. 4?! Fry me many Beavers... End);
[
[0] => 'Fry me a Beaver.',
[1] => 'Fry me a Beaver!',
[2] => 'Fry me a Beaver?',
[3] => 'Fry me Beaver no. 4?!',
[4] => 'Fry me many Beavers...',
[5] => 'End'
]
StringType::words('Fry me many Beavers... End'); // ['Fry', 'me', 'many', 'Beavers', 'End']
StringType::unword('Remove word from text', 'word'); // 'Remove from text'
public function __construct(private Doctrine\Persistence\ManagerRegistry $doctrine)
{
$doctrineUtils = new Cosmologist\Gears\Doctrine\DoctrineUtils($doctrine);
}
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\Expr;
DoctrineUtils::mergeCriteria(
new Criteria(new Expr\Comparison('status', Expr\Comparison::EQ, 'new')),
new Criteria(new Expr\Comparison('type', Expr\Comparison::NEQ, 'foo'))
);
$qb = $entityManager->getRepository(Company::class)->createQueryBuilder('contact');
// Adds a join and returns an alias of added join
DoctrineUtils::joinOnce($qb, 'contact.user', 'u1'); // "u1"
// If a join with specified parameters exists then only returns an alias of existed join
DoctrineUtils::joinOnce($qb, 'contact.user', 'u2'); // "u1"
class AppExtension extends Extension
{
#[Override]
public function load(array $configs, ContainerBuilder $container)
{
$container->addExpressionLanguageProvider(new class implements ExpressionFunctionProviderInterface {
public function getFunctions(): array {
return [ExpressionFunctionUtils::fromCallable([WalletIdentifier::class, 'create'], 'walletId')];
}
});
$container
->getDefinition(OrderService::class)
->setArgument('$wallet', expr('walletId(13)'));
}
}
use Cosmologist\Gears\Symfony\Form\FormUtils;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Validator\Exception\ValidationFailedException;
if ($form->isSubmitted()) {
try {
return $this->handler->create($form->getData());
} catch (ValidationFailedException $exception) {
$violationMapper = new ViolationMapper();
foreach ($exception->getViolations() as $domainViolation) {
$violationMapper->mapViolation(FormUtils::convertDomainViolationToFormViolation($domainViolation), $form);
}
}
}
return $form->createView();
use Cosmologist\Gears\Symfony\Form\DataFormsMapperDefaultTrait;
class TransactionFormType extends AbstractType implements DataMapperInterface
{
use DataFormsMapperDefaultTrait;
#[Override]
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class)
->setDataMapper($this);
}
#[Override]
public function mapFormsToData(Traversable $forms, mixed &$viewData): void
{
$forms = iterator_to_array($forms);
$viewData = new Contact($forms['name']->getData());
}
namespace App;
use App\DependencyInjection\AppExtension;
use Cosmologist\Gears\Symfony\Framework\AppExtension\AppExtensionKernelInterface;
use Cosmologist\Gears\Symfony\Framework\AppExtension\RegisterAppExtensionKernelTrait;
use Override;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel implements AppExtensionKernelInterface
{
use MicroKernelTrait;
use RegisterAppExtensionKernelTrait;
#[Override]
public function getAppExtension(): ExtensionInterface
{
return new AppExtension();
}
}
class FooTest extends KernelTestCase
{
use MessengerTestUtilsTrait;
protected function setUp(): void
{
self::bootKernel();
// A MessengerTestUtilsTrait needs your command bus
$this->commandBus = $this->getContainer()->get('command.bus');;
}
public function testBar() {
$this->assertCommandShouldFail(new FooCommand);
$this->assertCommandShouldFail(new FooCommand, BarException::class);
}
}
$this->messenger->dispatch(new App\Event\FooEvent('bar'));
// or
$this->messengerBus->dispatch(new App\Event\FooEvent('bar'));
use Cosmologist\Gears\Symfony\PropertyAccessor\RecursivePropertyAccessor;
$grandfather = new Person(name: 'grandfather');
$dad = new Person(name: 'dad', parent: $grandfather);
$i = new Person(name: 'i', parent: $dad);
(new RecursivePropertyAccessor())->getValue($i, 'parent'); // [Person(dad), Person(grandfather)]
class FooController extends AbstractController
{
public function barAction(): Response
{
$this->denyAccessUnlessGranted(SuperUserRoleVoter::ROLE_SUPER_USER);
...
}
}
use Cosmologist\Gears\Symfony\Test\TestUtils;
class FooTest extends WebTestCase
{
protected function testBar(): void
{
$browser = self::createClient();
TestUtils::addHeader($browser, 'User-Agent', 'Symfony KernelBrowser');
...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.