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\Stdlib\Hydrator\Strategy;
11
12final class ExplodeStrategy implements StrategyInterface
13{
14    /**
15     * @var string
16     */
17    private $valueDelimiter;
18
19    /**
20     * @var int|null
21     */
22    private $explodeLimit;
23
24    /**
25     * Constructor
26     *
27     * @param string   $delimiter    String that the values will be split upon
28     * @param int|null $explodeLimit Explode limit
29     */
30    public function __construct($delimiter = ',', $explodeLimit = null)
31    {
32        $this->setValueDelimiter($delimiter);
33
34        $this->explodeLimit = ($explodeLimit === null) ? null : (int) $explodeLimit;
35    }
36
37    /**
38     * Sets the delimiter string that the values will be split upon
39     *
40     * @param  string $delimiter
41     * @return self
42     */
43    private function setValueDelimiter($delimiter)
44    {
45        if (!is_string($delimiter)) {
46            throw new Exception\InvalidArgumentException(sprintf(
47                '%s expects Delimiter to be string, %s provided instead',
48                __METHOD__,
49                is_object($delimiter) ? get_class($delimiter) : gettype($delimiter)
50            ));
51        }
52
53        if (empty($delimiter)) {
54            throw new Exception\InvalidArgumentException('Delimiter cannot be empty.');
55        }
56
57        $this->valueDelimiter = $delimiter;
58    }
59
60    /**
61     * {@inheritDoc}
62     *
63     * Split a string by delimiter
64     *
65     * @param string|null $value
66     *
67     * @return string[]
68     *
69     * @throws Exception\InvalidArgumentException
70     */
71    public function hydrate($value)
72    {
73        if (null === $value) {
74            return array();
75        }
76
77        if (!(is_string($value) || is_numeric($value))) {
78            throw new Exception\InvalidArgumentException(sprintf(
79                '%s expects argument 1 to be string, %s provided instead',
80                __METHOD__,
81                is_object($value) ? get_class($value) : gettype($value)
82            ));
83        }
84
85        if ($this->explodeLimit !== null) {
86            return explode($this->valueDelimiter, $value, $this->explodeLimit);
87        }
88
89        return explode($this->valueDelimiter, $value);
90    }
91
92    /**
93     * {@inheritDoc}
94     *
95     * Join array elements with delimiter
96     *
97     * @param string[] $value The original value.
98     *
99     * @return string|null
100     */
101    public function extract($value)
102    {
103        if (!is_array($value)) {
104            throw new Exception\InvalidArgumentException(sprintf(
105                '%s expects argument 1 to be array, %s provided instead',
106                __METHOD__,
107                is_object($value) ? get_class($value) : gettype($value)
108            ));
109        }
110
111        return empty($value) ? null : implode($this->valueDelimiter, $value);
112    }
113}
114