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/ */
// Current locale used
NumberType::spellout(123.45); // one hundred twenty-three point four five
// Specific locale used
NumberType::spellout(123.45, 'ru'); // сто двадцать три целых сорок пять сотых
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');
...
}
}
use Cosmologist\Gears\Symfony\Validator\ValidationFailedException;
ValidationFailedException::violate($foo, "Foo with invalid bar");
ValidationFailedException::violate($foo, "Foo with invalid {{ bar }}", compact('bar'));
ValidationFailedException::violate($foo, "Foo with invalid bar", propertyPath: 'bar');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.