MultiAsset::offsetUnset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace Riclep\Storyblok\Fields;
5
6
7
use Illuminate\Support\Str;
8
use Riclep\Storyblok\Field;
9
use Riclep\Storyblok\Traits\HasChildClasses;
10
11
class MultiAsset extends Field implements \ArrayAccess, \Iterator, \Countable
12
{
13
	use HasChildClasses;
14
15
	/**
16
	 * @var int used for iterating over the array of assets
17
	 */
18
	private int $iteratorIndex = 0;
19
20
	/**
21
	 * Returns a comma delimited list of filenames
22
	 *
23
	 * @return string
24
	 */
25
	public function __toString(): string
26
	{
27
		return $this->content->map(function ($item) {
28
			if (is_object($item) && $item->has('filename')) {
29
				return $item->filename;
30
			}
31
		})->filter()->implode(',');
32
	}
33
34
	/**
35
	 * Attempts to determine the types of assets that have been linked
36
	 */
37
	public function init(): void
38
	{
39
		if ($this->hasFiles()) {
40
			$this->content = collect($this->content())->transform(function ($file) {
41
				if (Str::endsWith($file['filename'], ['.jpg', '.jpeg', '.png', '.gif', '.webp'])) {
42
					if ($class = $this->getChildClassName('Field', $this->block()->component() . 'Image')) {
43
						return new $class($file, $this->block());
44
					}
45
46
					return new Image($file, $this->block());
47
				}
48
49
				if ($class = $this->getChildClassName('Field', $this->block()->component() . 'Asset')) {
50
					return new $class($file, $this->block());
51
				}
52
53
				return new Asset($file, $this->block());
54
			});
55
		}
56
	}
57
58
	/**
59
	 * Checks if files are uploaded
60
	 *
61
	 * @return bool
62
	 */
63
	public function hasFiles(): bool
64
	{
65
		return (bool) $this->content();
66
	}
67
68
	/*
69
	 * Methods for ArrayAccess Trait - allows us to dig straight down to the content collection when calling a key on the Object
70
	 * */
71
	public function offsetSet($offset, $value): void
72
	{
73
		if (is_null($offset)) {
74
			$this->content[] = $value;
75
		} else {
76
			$this->content[$offset] = $value;
77
		}
78
	}
79
80
	public function offsetExists($offset): bool
81
	{
82
		return isset($this->content[$offset]);
83
	}
84
85
	public function offsetUnset($offset): void
86
	{
87
		unset($this->content[$offset]);
88
	}
89
90
	public function offsetGet($offset): mixed
91
	{
92
		return isset($this->content[$offset]) ? $this->content[$offset] : null;
93
	}
94
95
	/*
96
	 * Methods for Iterator trait allowing us to foreach over a collection of
97
	 * Blocks and return their content. This makes accessing child content
98
	 * in Blade much cleaner
99
	 * */
100
	public function current(): mixed
101
	{
102
		return $this->content[$this->iteratorIndex];
103
	}
104
105
	public function next(): void
106
	{
107
		$this->iteratorIndex++;
108
	}
109
110
	public function rewind(): void
111
	{
112
		$this->iteratorIndex = 0;
113
	}
114
115
	public function key(): int
116
	{
117
		return $this->iteratorIndex;
118
	}
119
120
	public function valid(): bool
121
	{
122
		return isset($this->content[$this->iteratorIndex]);
123
	}
124
125
	/*
126
	 * Countable trait
127
	 * */
128
	public function count(): int
129
	{
130
		return $this->content->count();
131
	}
132
}
133