PHP code example of mmo / sf-utils

1. Go to this page and download the library: Download mmo/sf-utils 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/ */

    

mmo / sf-utils example snippets




mo\sf\Validator\Constraints\Itin;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->getValidator();
$validator->validate('foo', new Itin()); // NOT VALID
$validator->validate('918-97-5273', new Itin()); // VALID



use mmo\sf\Validator\Constraints\Birthday;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->getValidator();
$validator->validate((new DateTimeImmutable('now'))->modify('-5 years'), new Birthday(['minAge' => 18])); // NOT VALID
$validator->validate((new DateTimeImmutable('now'))->modify('-120 years'), new Birthday()); // NOT VALID
$validator->validate((new DateTimeImmutable('now'))->modify('-5 years'), new Birthday()); // VALID



use mmo\sf\Validator\Constraints\BankRoutingNumber;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->getValidator();
$validator->validate('1234567890', new BankRoutingNumber()); // NOT VALID
$validator->validate('275332587', new BankRoutingNumber()); // VALID



use mmo\sf\Validator\Constraints\Utf8Letters;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->getValidator();
$validator->validate('foo.bar', new Utf8Letters()); // NOT VALID
$validator->validate('Zażółć', new Utf8Letters()); // VALID



use mmo\sf\Validator\Constraints\Utf8Words;
use Symfony\Component\Validator\Validation;

$validator = Validation::createValidatorBuilder()->getValidator();
$validator->validate('foo.bar', new Utf8Words()); // NOT VALID
$validator->validate('Zażółć gęślą', new Utf8Words()); // VALID



use mmo\sf\Validator\Constraints\OnlyDigits;
use Symfony\Component\Validator\Validation;

lidate('f1234', new OnlyDigits()); // NOT VALID
$violations = $validator->validate('1234567', new OnlyDigits()); // VALID

use Symfony\Component\Validator\Validation;
use mmo\sf\Validator\ArrayConstraintValidatorFactory;
use Kiczort\PolishValidatorBundle\Validator\Constraints\NipValidator;

// ....
$validatorFactory = new ArrayConstraintValidatorFactory(['kiczort.validator.nip' => new NipValidator()]);
$validator = Validation::createValidatorBuilder()
            ->setConstraintValidatorFactory($validatorFactory)
            ->getValidator();
// ...



mo\sf\Translation\FakeTranslator;

$translator = new FakeTranslator('en');
$translator->trans('foo'); // en-foo
$translator->trans('foo', [], null, 'us'); // us-foo



use mmo\sf\Util\Encrypter;

cret-key');
$secret = $encrypter->encryptString('secret message');
$plaintext = $encrypter->decryptString($secret); 



mo\sf\Security\Test\AlwaysTheSameEncoderFactory;
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;

$factory = new AlwaysTheSameEncoderFactory(new PlaintextPasswordEncoder());
$encoder = new UserPasswordEncoder($factory);
// now you can pass $encoder to your service, which expect `UserPasswordEncoderInterface`



mo\sf\Security\Test\MemoryUserProvider;
use Symfony\Component\Security\Core\User\User;

$provider = new MemoryUserProvider(User::class, []);
$provider->createUser(new User('test', 'foo'));
$provider->loadUserByUsername('test');

//...

use mmo\sf\Form\DataTransformer\StringInsteadNullTransformer;

class StateType extends AbstractType
{
    private StatesProviderInterface $statesProvider;

    public function __construct(StatesProviderInterface $statesProvider)
    {
        $this->statesProvider = $statesProvider;
    }

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->addModelTransformer(new StringInsteadNullTransformer());
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(['choices' => $this->statesProvider->getStates()]);
    }

    public function getParent(): string
    {
        return ChoiceType::class;
    }
}


class FormDto
{
    /**
     * @var string|null 
    */
    public $text;
    
    /**
     * @var PersonDto|null
     */
    public $person;
}

class PersonDto
{
    /**
     * @var string|null
     */
    public $firstName;

    /**
     * @var string|null
     */
    public $lastName;
}



use mmo\sf\Form\ReplaceIfNotSubmittedListener;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;

class FormToTestReplaceValueType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder->add('text', TextType::class);
        $builder->add('person', PersonType::class, [
            '



mo\sf\Util\Transliterator;

Transliterator::transliterate('¿Español?'); // Espanol?
Transliterator::transliterate('Українська: ґанок, європа', Transliterator::TRANSLITERATE_STRICT); // Ukraí̈nsʹka: g̀anok, êvropa



mo\sf\Util\EntityTestHelper;

EntityTestHelper::setPrivateProperty($entity, 12);
EntityTestHelper::setPrivateProperty($entity, 12, 'fieldName');



mo\sf\Serializer\Normalizer\MyCLabsEnumNormalizer;
use MyCLabs\Enum\Enum;
use Symfony\Component\Serializer\Serializer;

/**
 * @method static static DRAFT()
 * @method static static PUBLISHED()
 */
class MyEnum extends Enum
{
    private const DRAFT = 'draft';
    private const PUBLISHED = 'published';
}

$serializer = new Serializer([new MyCLabsEnumNormalizer()]);
$serializer->denormalize('draft', MyEnum::class); // return instance of MyEnum



oney\Money;
use mmo\sf\Serializer\Normalizer\MoneyNormalizer;
use Symfony\Component\Serializer\Serializer;

$serializer = new Serializer([new MoneyNormalizer()]);
$money = $serializer->denormalize($serializer->normalize(Money::EUR(100)), Money::class);
 php


f\Util\ObjectHelper;

$object = ObjectHelper::arrayToObject(['foo' => 'bar', 'baz' => ['foo' => 'bar']]);

// class stdClass#3 (2) {
//   public $foo =>
//   string(3) "bar"
//   public $baz =>
//   class stdClass#2 (1) {
//     public $foo =>
//     string(3) "bar"
//   }
// }