1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category  Horde
9 * @copyright 2012-2017 Horde LLC
10 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @package   Imap_Client
12 */
13
14/**
15 * Object representation of an IMAP parenthesized list (RFC 3501 [4.4]).
16 *
17 * @author    Michael Slusarz <slusarz@horde.org>
18 * @category  Horde
19 * @copyright 2012-2017 Horde LLC
20 * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
21 * @package   Imap_Client
22 */
23class Horde_Imap_Client_Data_Format_List extends Horde_Imap_Client_Data_Format implements Countable, IteratorAggregate
24{
25    /**
26     * @see add()
27     */
28    public function __construct($data = null)
29    {
30        parent::__construct(array());
31
32        if (!is_null($data)) {
33            $this->add($data);
34        }
35    }
36
37    /**
38     * Add an element to the list.
39     *
40     * @param mixed $data     The data element(s) to add. Either a
41     *                        Horde_Imap_Client_Data_Format object, a string
42     *                        value that will be treated as an IMAP atom, or
43     *                        an array (or iterable object) of objects to add.
44     * @param boolean $merge  Merge the contents of any container objects,
45     *                        instead of adding the objects themselves?
46     *
47     * @return Horde_Imap_Client_Data_Format_List  This object to allow for
48     *                                             chainable calls (since
49     *                                             2.10.0).
50     */
51    public function add($data, $merge = false)
52    {
53        if (is_array($data) || ($merge && ($data instanceof Traversable))) {
54            foreach ($data as $val) {
55                $this->add($val);
56            }
57        } elseif (is_object($data)) {
58            $this->_data[] = $data;
59        } elseif (!is_null($data)) {
60            $this->_data[] = new Horde_Imap_Client_Data_Format_Atom($data);
61        }
62
63        return $this;
64    }
65
66    /**
67     */
68    public function __toString()
69    {
70        $out = '';
71
72        foreach ($this as $val) {
73            if ($val instanceof $this) {
74                $out .= '(' . $val->escape() . ') ';
75            } elseif (($val instanceof Horde_Imap_Client_Data_Format_String) &&
76                      $val->literal()) {
77                /* ERROR: Requires literal output. */
78                return '';
79            } else {
80                $out .= $val->escape() . ' ';
81            }
82        }
83
84        return rtrim($out);
85    }
86
87    /* Countable methods. */
88
89    /**
90     */
91    public function count()
92    {
93        return count($this->_data);
94    }
95
96    /* IteratorAggregate method. */
97
98    /**
99     * Iterator loops through the data elements contained in this list.
100     */
101    public function getIterator()
102    {
103        return new ArrayIterator($this->_data);
104    }
105
106}
107