for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php namespace CMPayments\SchemaValidator\Validators;
use CMPayments\SchemaValidator\Exceptions\ValidateException;
/**
* Class EnumTrait
*
* @package CMPayments\SchemaValidator\Validators
* @Author Boy Wijnmaalen <[email protected]>
*/
trait EnumTrait
{
* Validates $data against a specific $schema->enum
* @param $data
* @param $schema
* @param $path
public function validateEnum($data, $schema, $path)
if (!isset($schema->enum)) {
return;
}
// if $data comes from an array than the action below has already been done in validateArray()
$needle = (isset($schema->caseSensitive) && !$schema->caseSensitive) ? strtolower($data) : $data;
$haystack = (isset($schema->caseSensitive) && !$schema->caseSensitive) ? array_map('strtolower', $schema->enum) : $schema->enum;
if (!in_array($needle, $haystack)) {
$this->addError(
ValidateException::ERROR_USER_ENUM_NEEDLE_NOT_FOUND_IN_HAYSTACK,
[$path, $data, $this->conjugationObject(count($schema->enum), 'this specific value', 'one of these values'), implode('\', \'', $schema->enum)]
);
* @param int $code
* @param array $args
* @return mixed
abstract public function addError($code, array $args = []);
* Returns a valid representation of 'items' (or other value)
* @param int $count
* @param string $single
* @param string $plural
* @return string
abstract public function conjugationObject($count, $single = 'item', $plural = 'items');