PhpStorm reformat array [duplicate] - php

I'm using Laravel framework. I want to change the default code style format from this:
return [
'name' => 'required|min:4|string',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:4|confirmed',
'password-confirmation' => 'required|min:4'
];
to this:
return [
'name' => 'required|min:4|string',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:4|confirmed',
'password-confirmation' => 'required|min:4'
];
I did a lot of searches in PhpStorm settings but I couldn't find the solution.

File | Settings (PhpStorm | Preferences on macOS)
Editor | Code Style | PHP
Wrapping and Braces tab
Array initializer | Align key-value pairs option

Related

use tow locale in php faker at same time

im using laravel 7 and im using php faker ..
if i want to seed customer i did this code ..
$factory->define(App\Models\Customer::class, function (Faker $faker) {
return [
'number' => $faker->numberBetween(1,1000000),
'name' => $faker->name,
'foreign_name' => $faker->name,
'financial_account_type_id' => 2,
'address' => $faker->address,
'tax_id' => $faker->numberBetween(1,100000),
'phone' => $faker->e164PhoneNumber,
'mobile' => $faker->e164PhoneNumber,
'email' => $faker->email,
'sales_payment_term_id' => $faker->numberBetween(1,7),
'purchase_payment_term_id' => $faker->numberBetween(1,7),
'note' => $faker->sentence,
];
});
now in my app.php i did this code also ..
'faker_locale' => 'ar_SA',
i want the 'foreign_name' => $faker->name, to be in english language just this line ..
is that possible to change only one line locale ..
thanks ..

codeception seeResponseMatchesJsonType allow null | array

I would like to check that the json structure matches the following:
$I->seeResponseMatchesJsonType([
'user' => [
'id' => 'string',
'name' => 'string',
'avatar' => 'string'
]);
However it should also accept if the user is null. It works with primitive types:
$I->seeResponseMatchesJsonType([
'user' => 'null|string'
]);
But how can I do this with the nested array?

Regex use in Laravel

This is my Laravel code:
$validator = \Validator::make($val, [
'service_activity' => 'required|max:1000',
'inspired' => 'required',
'obstacles' => 'required|max:1000',
'working_on_acitivity' => array('required', 'regex:^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$'),
'spend_time' => 'required',
'activity_impact' => 'required|max:1000',
'fund_raising' => 'required',
'raised_amount' => 'required|numeric',
'people_involved' => 'required|numeric',
'learned_from_experience' => 'required|max:1000',
],$messages);
I want to use the regex for the field in working_on_acitivity but always getting the same error. I have used the regex in an array as given in the Laravel documentation:
type":"ErrorException","message":"preg_match(): No ending delimiter '^' found
Change
'regex:^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$'
to
'regex:#^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$#'
using # as delimiter.
(This is based on your error message talking about missing delimiters.)

How to organize parsing and validation of REST API parameters?

I have a rest api that has many parameters via the query string. I am wondering if someone knows of a design pattern or has a nice way of organizing all the parameters (Objects, functions, array, json). Right now I am parsing and validating all my parameters in the same function, very ugly code.
Ideally I would like some way to handle the parameters similar to a database ORM or even a config file/array/json. However, I have tried to come up with a solution to this without any luck.
Any insight would be appreciated!
Example of my thoughts:
<?php
...
$parameters = [
// ?fields=id,name
'fields' => [
'default' => ['id', 'name'],
'valid' => ['id', 'name', 'date],
'type' => 'csv', // list of values (id & name)
'required' => ['id'],
'replace' => ['title' => 'name'], // if the database & api names don't match
'relation' => null, // related database table
],
// ?list=true
'list' => [
'default' => ['false'],
'valid' => ['true', 'false'],
'type' => 'boolean' // single value (true or false)
'required' => [],
'replace' => [], // if the database & api names don't match
'relation' => 'category', // related database table
],
....
];
Seems to me like you are looking for a validation library. My favorite is Symfony's: https://github.com/symfony/validator. I know Zend Framework 2 also has a validation component. I haven't used it personally, but I expect that to be very good too.
Example from the symfony/validator readme:
<?php
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
$validator = Validation::createValidator();
$constraint = new Assert\Collection(array(
'name' => new Assert\Collection(array(
'first_name' => new Assert\Length(array('min' => 101)),
'last_name' => new Assert\Length(array('min' => 1)),
)),
'email' => new Assert\Email(),
'simple' => new Assert\Length(array('min' => 102)),
'gender' => new Assert\Choice(array(3, 4)),
'file' => new Assert\File(),
'password' => new Assert\Length(array('min' => 60)),
));
$input would be $_GET or something obtained with parse_str etc. It is also possible to define the validation rules in some other format, such as YAML.

how to validate a registration form in zend framework 2?

i am trying to validate a user registration form in Zend Framework 2.
More specifically how to validate the email, ZF1 i could do:
$email->setValidators( array(new Zend_Validate_EmailAddress()) );
I'm wondering if i can just call something similar like this.
Also I'm wondering how to validate two fields that need to be the same like the password field and the password verification.
I guess that when i say if($form->isValid()).. this will check the getInputFilter() method for all validation.
I've been taking a look at ZfcUser module but, right now, i can't understand much since i don't have a full grasp on how ZF2 works
Any ideas, maybe a simple example?
thanks
Have you read the official tutorial to see how the new ZF2 Form component works?
At a very high level, you need a Form object and a Filter object working together. The Filter object is where you place your filters and validators. However, if you use a form element of type EmailAddress in your Form, then it will automatically add the correct validator. There is more information in the manual.
I recently did a webinar on forms for Zend which you should be able to find on this page.
i've figure it out.
the validators are multidimensional arrays and each array has a name and some options. It might be a bit wired in the beginning to notice it, but much configuration in zf2 is in this way
see an example for the password:
$inputFilter->add($factory->createInput([
'name' => 'password',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 6,
'max' => 128,
],
],
],
]));
$inputFilter->add($factory->createInput([
'name' => 'password_verify',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
array(
'name' => 'StringLength',
'options' => array( 'min' => 6 ),
),
array(
'name' => 'identical',
'options' => array('token' => 'password' )
),
],
]));
note, in php 5.3 > an array could be written like array() or [], in the above example i mix them up for no particular reason.

Categories