1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @see       http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 */
9
10namespace Zend\Diactoros\Response;
11
12use function array_keys;
13use function array_reduce;
14use function strtolower;
15
16trait InjectContentTypeTrait
17{
18    /**
19     * Inject the provided Content-Type, if none is already present.
20     *
21     * @param string $contentType
22     * @param array $headers
23     * @return array Headers with injected Content-Type
24     */
25    private function injectContentType($contentType, array $headers)
26    {
27        $hasContentType = array_reduce(array_keys($headers), function ($carry, $item) {
28            return $carry ?: (strtolower($item) === 'content-type');
29        }, false);
30
31        if (! $hasContentType) {
32            $headers['content-type'] = [$contentType];
33        }
34
35        return $headers;
36    }
37}
38