Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / src / SectionStorage / SectionStorageTrait.php
1 <?php
2
3 namespace Drupal\layout_builder\SectionStorage;
4
5 use Drupal\layout_builder\Section;
6
7 /**
8  * Provides a trait for storing sections on an object.
9  *
10  * @internal
11  *   Layout Builder is currently experimental and should only be leveraged by
12  *   experimental modules and development releases of contributed modules.
13  *   See https://www.drupal.org/core/experimental for more information.
14  */
15 trait SectionStorageTrait {
16
17   /**
18    * Stores the information for all sections.
19    *
20    * Implementations of this method are expected to call array_values() to rekey
21    * the list of sections.
22    *
23    * @param \Drupal\layout_builder\Section[] $sections
24    *   An array of section objects.
25    *
26    * @return $this
27    */
28   abstract protected function setSections(array $sections);
29
30   /**
31    * {@inheritdoc}
32    */
33   public function count() {
34     return count($this->getSections());
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function getSection($delta) {
41     if (!$this->hasSection($delta)) {
42       throw new \OutOfBoundsException(sprintf('Invalid delta "%s"', $delta));
43     }
44
45     return $this->getSections()[$delta];
46   }
47
48   /**
49    * Sets the section for the given delta on the display.
50    *
51    * @param int $delta
52    *   The delta of the section.
53    * @param \Drupal\layout_builder\Section $section
54    *   The layout section.
55    *
56    * @return $this
57    */
58   protected function setSection($delta, Section $section) {
59     $sections = $this->getSections();
60     $sections[$delta] = $section;
61     $this->setSections($sections);
62     return $this;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function appendSection(Section $section) {
69     $delta = $this->count();
70
71     $this->setSection($delta, $section);
72     return $this;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function insertSection($delta, Section $section) {
79     if ($this->hasSection($delta)) {
80       // @todo Use https://www.drupal.org/node/66183 once resolved.
81       $start = array_slice($this->getSections(), 0, $delta);
82       $end = array_slice($this->getSections(), $delta);
83       $this->setSections(array_merge($start, [$section], $end));
84     }
85     else {
86       $this->appendSection($section);
87     }
88     return $this;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function removeSection($delta) {
95     $sections = $this->getSections();
96     unset($sections[$delta]);
97     $this->setSections($sections);
98     return $this;
99   }
100
101   /**
102    * Indicates if there is a section at the specified delta.
103    *
104    * @param int $delta
105    *   The delta of the section.
106    *
107    * @return bool
108    *   TRUE if there is a section for this delta, FALSE otherwise.
109    */
110   protected function hasSection($delta) {
111     return isset($this->getSections()[$delta]);
112   }
113
114 }