Cowgirl
Cowgirl

Reputation: 1494

A way to add Multiple object operator in a condition

In PHP, is there a way to correspond multiple object operator to check a certain condition

For example

if( !empty($this->user()->city && !empty($this->user()->name && !empty($this->user->phone) ) 

Here, I have to check condition for the city, name, phone and lot's of other properties, is there a way to add a shortcut like this

if(!empty($this->user()->{city,name,phone})

Upvotes: 0

Views: 391

Answers (3)

Alex Howansky
Alex Howansky

Reputation: 53573

There is no built-in for that sort of multiple-field check. Depending on the values in your variables and what you consider to be empty, you might be able to just concatenate all the fields together and check for the emptiness of that:

if (empty($user->city . $user->name . $user->phone)) {
    // all fields are empty
} else {
    // at least one field is not empty
}

... but that's rather... ugly.

and lot's of other properties,

Well, that's what loops are for. I'd simply write a method to verify that you have the required fields. Something like this maybe:

class User
{

    const REQUIRED_FIELDS = ['name', 'city', 'state'];

    public function hasRequiredFields(): bool
    {
        foreach (self::REQUIRED_FIELDS as $field) {
            if (empty($this->$field)) {
                return false;
            }
        }
        return true;
    }

}

Then you can just do:

if ($user->hasRequiredFields()) {
    ...
}

If your user object already has all the required fields defined as public properties, then you can just iterate over the object itself to get them:

class User
{

    public $name;
    public $city;
    public $state;

    public function hasRequiredFields(): bool
    {
        foreach ($this as $key => $value) {
            if (empty($value)) {
                return false;
            }
        }
        return true;
    }

}

Upvotes: 0

Nilesh Lathe
Nilesh Lathe

Reputation: 152

As per my knowledge there is no function to test this you have to write your own custom function for example check given entries in array is blank or not (All entries send to it are mendatory)

function checkEmpty($arr)
{
  foreach($arr as $value)
  {
    if(empty(trim($value)))
    {
      return false;
    }
  }
  return true;
}
$arr = array('name'=>'nilesh','age'=>' ');
if(checkEmpty($arr))
 echo "true part here";
else
 echo 'False Part Here';

Upvotes: 0

Don't Panic
Don't Panic

Reputation: 41810

I think the exact thing you're trying to do really isn't possible. The closest thing I can think of is to define an array of properties and check them in a loop.

$required = ['city', 'name', 'phone', 'lots', 'of', 'other', 'properties'];

$complete = true;
foreach ($required as $property) {
    if (empty($this->user()->$property)) {
        $complete = false;
        break;
    }
}

This can help to avoid a giant complex if condition like that; instead you just check

if ($complete) ...

after the loop.

It seems like the specific thing you're trying to do with this is entity validation. If you use one of the various PHP frameworks, there are utilities that can make this easier for you. You can just add a required annotation to the property or something like that.

Upvotes: 1

Related Questions