Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

How can I check that array contain sub array? #103

Closed
ManasAtabaev opened this issue Feb 10, 2017 · 6 comments
Closed

How can I check that array contain sub array? #103

ManasAtabaev opened this issue Feb 10, 2017 · 6 comments
Assignees
Labels

Comments

@ManasAtabaev
Copy link

Hi,
Need check that some array contain subarray, only way I find create rule for all items in subarray, like "item1 in ['item1', 'item2', 'item3', 'item4', 'item5'] and item2 in ['item1', 'item2', 'item3', 'item4', 'item5'] and item5 in ['item1', 'item2', 'item3', 'item4', 'item5']"
Thanks!

@Hywan Hywan self-assigned this Feb 10, 2017
@Hywan Hywan added the question label Feb 10, 2017
@Hywan
Copy link
Member

Hywan commented Feb 10, 2017

Hello 😄,

I think creating an operator for that would be appropriate, like is_subset_of:

$ruler->getDefaultAsserter()->setOperator('is_subset_of', function (array $needles, array $haystack): bool {
    foreach ($needles as $needle) {
        if (false === in_array($needle, $haystack)) {
            return false;
        }
    }

    return true;
});

then:

[item1, item2, item5] in ['item1', 'item2', 'item3', 'item4', 'item5']

Not tested, but it should work.

Is it clear?

@ManasAtabaev
Copy link
Author

ManasAtabaev commented Feb 12, 2017

Thank you, it work perfect!
Also one more question, is it possible to return on which value test was stopped?
Is there a way to return the rule operator and the value in case error occurs during the check?

$ruler->getDefaultAsserter()->setOperator('is_subset_of', function (array $needles, array $haystack) {
    foreach ($needles as $needle) {
        if (false === in_array($needle, $haystack)) {
            if($this->returnError) {
               return $needle;
           }
            return false;
        }
    }
    return true;
});

@Hywan
Copy link
Member

Hywan commented Feb 13, 2017

@Commander223 There is no error API yet. You might want to check #41. cc @vonglasow

@Hywan Hywan closed this as completed Feb 13, 2017
@ManasAtabaev
Copy link
Author

@Hywan Ok. Thx for help.

@Hywan
Copy link
Member

Hywan commented Feb 13, 2017

You're welcome! Feel free to ask anything else! And thanks for using our libraries 😃.

@Hywan
Copy link
Member

Hywan commented Feb 13, 2017

@Commander223 Are you still living in Kyrgizstan? If yes, can you come on IRC (freenode #hoaproject), I would have some questions for you 😉.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants