Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

[RFR] Introducting the valid() modifier #836

Merged
merged 4 commits into from
Feb 23, 2016
Merged

[RFR] Introducting the valid() modifier #836

merged 4 commits into from
Feb 23, 2016

Conversation

fzaninotto
Copy link
Owner

<?php

// valid() only accepts valid values according to the passed validator functions
$values = array();
$evenValidator = function($digit) {
    return $digit % 2 === 0;
};
for ($i=0; $i < 10; $i++) {
    $values []= $faker->valid($evenValidator)->randomDigit;
}
print_r($values); // [0, 4, 8, 4, 2, 6, 0, 8, 8, 6]

// just like unique(), valid() throws an overflow exception when it can't generate a valid value
$values = array();
try {
  $faker->valid($evenValidator)->randomElement(1, 3, 5, 7, 9);
} catch (\OverflowException $e) {
  echo "Can't pick an even number in that set!";
}

Refs #567

@@ -551,4 +552,9 @@ public function unique($reset = false, $maxRetries = 10000)

return $this->unique;
}

public function valid($validator = null, $maxRetries = 10000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing phpdoc?

fzaninotto added a commit that referenced this pull request Feb 23, 2016
[RFR] Introducting the valid() modifier
@fzaninotto fzaninotto merged commit f03cdfd into master Feb 23, 2016
@fzaninotto fzaninotto deleted the valid_modifier branch February 23, 2016 15:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants