1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Log\Filter;
11
12use Traversable;
13use Zend\Log\Exception;
14use Zend\Validator\ValidatorInterface as ZendValidator;
15
16class Validator implements FilterInterface
17{
18    /**
19     * Regex to match
20     *
21     * @var ZendValidator
22     */
23    protected $validator;
24
25    /**
26     * Filter out any log messages not matching the validator
27     *
28     * @param  ZendValidator|array|Traversable $validator
29     * @throws Exception\InvalidArgumentException
30     * @return Validator
31     */
32    public function __construct($validator)
33    {
34        if ($validator instanceof Traversable) {
35            $validator = iterator_to_array($validator);
36        }
37        if (is_array($validator)) {
38            $validator = isset($validator['validator']) ? $validator['validator'] : null;
39        }
40        if (!$validator instanceof ZendValidator) {
41            throw new Exception\InvalidArgumentException(sprintf(
42                'Parameter of type %s is invalid; must implement Zend\Validator\ValidatorInterface',
43                (is_object($validator) ? get_class($validator) : gettype($validator))
44            ));
45        }
46        $this->validator = $validator;
47    }
48
49    /**
50     * Returns TRUE to accept the message, FALSE to block it.
51     *
52     * @param array $event event data
53     * @return bool
54     */
55    public function filter(array $event)
56    {
57        return $this->validator->isValid($event['message']);
58    }
59}
60