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\View\Helper;
11
12use Zend\Form\ElementInterface;
13use Zend\Form\Exception;
14
15class FormFile extends FormInput
16{
17    /**
18     * Attributes valid for the input tag type="file"
19     *
20     * @var array
21     */
22    protected $validTagAttributes = array(
23        'name'           => true,
24        'accept'         => true,
25        'autofocus'      => true,
26        'disabled'       => true,
27        'form'           => true,
28        'multiple'       => true,
29        'required'       => true,
30        'type'           => true,
31    );
32
33    /**
34     * Render a form <input> element from the provided $element
35     *
36     * @param  ElementInterface $element
37     * @throws Exception\DomainException
38     * @return string
39     */
40    public function render(ElementInterface $element)
41    {
42        $name = $element->getName();
43        if ($name === null || $name === '') {
44            throw new Exception\DomainException(sprintf(
45                '%s requires that the element has an assigned name; none discovered',
46                __METHOD__
47            ));
48        }
49
50        $attributes          = $element->getAttributes();
51        $attributes['type']  = $this->getType($element);
52        $attributes['name']  = $name;
53        if (array_key_exists('multiple', $attributes) && $attributes['multiple']) {
54            $attributes['name'] .= '[]';
55        }
56
57        $value = $element->getValue();
58        if (is_array($value) && isset($value['name']) && !is_array($value['name'])) {
59            $attributes['value'] = $value['name'];
60        } elseif (is_string($value)) {
61            $attributes['value'] = $value;
62        }
63
64        return sprintf(
65            '<input %s%s',
66            $this->createAttributesString($attributes),
67            $this->getInlineClosingBracket()
68        );
69    }
70
71    /**
72     * Determine input type to use
73     *
74     * @param  ElementInterface $element
75     * @return string
76     */
77    protected function getType(ElementInterface $element)
78    {
79        return 'file';
80    }
81}
82