1<?php
2/**
3 * Copyright since 2007 PrestaShop SA and Contributors
4 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
5 *
6 * NOTICE OF LICENSE
7 *
8 * This source file is subject to the Open Software License (OSL 3.0)
9 * that is bundled with this package in the file LICENSE.md.
10 * It is also available through the world-wide-web at this URL:
11 * https://opensource.org/licenses/OSL-3.0
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@prestashop.com so we can send you a copy immediately.
15 *
16 * DISCLAIMER
17 *
18 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
19 * versions in the future. If you wish to customize PrestaShop for your
20 * needs please refer to https://devdocs.prestashop.com/ for more information.
21 *
22 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
23 * @copyright Since 2007 PrestaShop SA and Contributors
24 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
25 */
26
27namespace PrestaShop\PrestaShop\Core\MailTemplate\Layout;
28
29use PrestaShop\PrestaShop\Core\Data\AbstractTypedCollection;
30
31class LayoutCollection extends AbstractTypedCollection implements LayoutCollectionInterface
32{
33    /**
34     * {@inheritdoc}
35     */
36    protected function getType()
37    {
38        return LayoutInterface::class;
39    }
40
41    /**
42     * {@inheritdoc}
43     */
44    public function merge(LayoutCollectionInterface $collection)
45    {
46        /** @var LayoutInterface $newLayout */
47        foreach ($collection as $newLayout) {
48            if (null !== ($oldLayout = $this->getLayout($newLayout->getName(), $newLayout->getModuleName()))) {
49                $this->replace($oldLayout, $newLayout);
50            } else {
51                $this->add($newLayout);
52            }
53        }
54    }
55
56    /**
57     * {@inheritdoc}
58     */
59    public function replace(LayoutInterface $oldLayout, LayoutInterface $newLayout)
60    {
61        if (!$this->contains($oldLayout)) {
62            return false;
63        }
64
65        $oldLayoutIndex = $this->indexOf($oldLayout);
66        $this->offsetSet($oldLayoutIndex, $newLayout);
67
68        return $this->contains($newLayout);
69    }
70
71    /**
72     * {@inheritdoc}
73     */
74    public function getLayout($layoutName, $moduleName)
75    {
76        /** @var LayoutInterface $layout */
77        foreach ($this as $layout) {
78            if ($layoutName === $layout->getName() && $moduleName === $layout->getModuleName()) {
79                return $layout;
80            }
81        }
82
83        return null;
84    }
85}
86