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\Form\Element;
11
12use Zend\Form\Element;
13use Zend\InputFilter\InputProviderInterface;
14use Zend\Validator\Regex as RegexValidator;
15
16class Color extends Element implements InputProviderInterface
17{
18    /**
19     * Seed attributes
20     *
21     * @var array
22     */
23    protected $attributes = array(
24        'type' => 'color',
25    );
26
27    /**
28     * @var \Zend\Validator\ValidatorInterface
29     */
30    protected $validator;
31
32    /**
33     * Get validator
34     *
35     * @return \Zend\Validator\ValidatorInterface
36     */
37    protected function getValidator()
38    {
39        if (null === $this->validator) {
40            $this->validator = new RegexValidator('/^#[0-9a-fA-F]{6}$/');
41        }
42        return $this->validator;
43    }
44
45    /**
46     * Provide default input rules for this element
47     *
48     * Attaches a color validator.
49     *
50     * @return array
51     */
52    public function getInputSpecification()
53    {
54        return array(
55            'name' => $this->getName(),
56            'required' => true,
57            'filters' => array(
58                array('name' => 'Zend\Filter\StringTrim'),
59                array('name' => 'Zend\Filter\StringToLower'),
60            ),
61            'validators' => array(
62                $this->getValidator(),
63            ),
64        );
65    }
66}
67