Sokil\Mongo\Validator\TypeValidator::validateField PHP Method

validateField() public method

public validateField ( Structure $document, $fieldName, array $params )
$document Sokil\Mongo\Structure
$params array
    public function validateField(Structure $document, $fieldName, array $params)
    {
        $value = $document->get($fieldName);
        if (!$value) {
            return;
        }
        if (empty($params[0])) {
            throw new Exception('Type not specified');
        }
        $requiredTypes = (array) $params[0];
        $allowedTypes = array('array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'null', 'numeric', 'object', 'real', 'resource', 'scalar', 'string');
        foreach ($requiredTypes as $type) {
            if (!in_array($type, $allowedTypes)) {
                throw new Exception('Type must be one of ' . implode(', ', $allowedTypes));
            }
            if (true === call_user_func('is_' . $type, $value)) {
                return;
            }
        }
        if (!isset($params['message'])) {
            if (count($requiredTypes) === 1) {
                $params['message'] = 'Field "' . $fieldName . '" must be of type ' . $requiredTypes[0] . ' in ' . get_called_class();
            } else {
                $params['message'] = 'Field "' . $fieldName . '" must be one of types ' . implode(', ', $requiredTypes) . ' in ' . get_called_class();
            }
        }
        $document->addError($fieldName, $this->getName(), $params['message']);
    }
TypeValidator