Passed
Push — master ( a4e2d9...0bbf1f )
by Richard
03:41 queued 14s
created

FieldFactory::isStringImageField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Riclep\Storyblok;
4
5
use Illuminate\Support\Str;
6
use Riclep\Storyblok\Fields\Asset;
7
use Riclep\Storyblok\Fields\Image;
8
use Riclep\Storyblok\Fields\MultiAsset;
9
use Riclep\Storyblok\Fields\RichText;
10
use Riclep\Storyblok\Fields\Table;
11
12
class FieldFactory
13
{
14
	/**
15
	 * Works out what class should be used for the given block’s content
16
	 *
17
	 * @param $block
18
	 * @param $field
19
	 * @param $key
20
	 * @return \Illuminate\Support\Collection|mixed|Asset|Image|MultiAsset|RichText|Table
21
	 */
22
	public function build($block, $field, $key) {
23
		// does the Block assign any $_casts? This is key (field) => value (class)
24
		if (array_key_exists($key, $block->getCasts())) {
25
			$casts = $block->getCasts();
26
			return new $casts[$key]($field, $block);
27
		}
28
29
		// find Fields specific to this Block matching: BlockNameFieldName
30
		if ($class = $block->getChildClassName('Field', $block->component() . '_' . $key)) {
31
			return new $class($field, $block);
32
		}
33
34
		// auto-match Field classes
35
		if ($class = $block->getChildClassName('Field', $key)) {
36
			return new $class($field, $block);
37
		}
38
39
		// single item relations
40
		if (Str::isUuid($field) && ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations))) {
41
			return $block->getRelation(new RequestStory(), $field);
42
		}
43
44
		// complex fields
45
		if (is_array($field) && !empty($field)) {
46
			return $this->arrayField($block, $field, $key);
47
		}
48
49
		// legacy and string image fields
50
		if ($this->isStringImageField($field)) {
51
			return new Image($field, $block);
52
		}
53
54
		// strings or anything else - do nothing
55
		return $field;
56
	}
57
58
	/**
59
	 * If given an array field we need more processing to determine the class
60
	 *
61
	 * @param $block
62
	 * @param $field
63
	 * @param $key
64
	 * @return \Illuminate\Support\Collection|mixed|Asset|Image|MultiAsset|RichText|Table
65
	 */
66
	public function arrayField($block, $field, $key) {
67
		// match link fields
68
		if (array_key_exists('linktype', $field)) {
69
			$class = 'Riclep\Storyblok\Fields\\' . Str::studly($field['linktype']) . 'Link';
70
71
			return new $class($field, $block);
72
		}
73
74
		// match rich-text fields
75
		if (array_key_exists('type', $field) && $field['type'] === 'doc') {
76
			return new RichText($field, $block);
77
		}
78
79
		// match asset fields - detecting raster images
80
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'asset') {
81
82
			// legacy and string image fields
83
			if($this->isStringImageField($field['filename'])) {
84
				return new Image($field, $block);
85
			}
86
87
			return new Asset($field, $block);
88
		}
89
90
		// match table fields
91
		if (array_key_exists('fieldtype', $field) && $field['fieldtype'] === 'table') {
92
			return new Table($field, $block);
93
		}
94
95
		if (array_key_exists(0, $field)) {
96
			// it’s an array of relations - request them if we’re auto or manual resolving
97
			if (Str::isUuid($field[0])) {
98
				if ($block->_autoResolveRelations || in_array($key, $block->_resolveRelations)) {
99
					$relations = collect($field)->transform(function ($relation) use ($block) {
100
						return $block->getRelation(new RequestStory(), $relation);
101
					});
102
103
					if ($block->_filterRelations) {
104
						$relations = $relations->filter();
105
					}
106
107
					return $relations;
108
				}
109
			}
110
111
			// has child items - single option, multi option and Blocks fields
112
			if (is_array($field[0])) {
113
				// resolved relationships - entire story is returned, we just want the content and a few meta items
114
				if (array_key_exists('content', $field[0])) {
115
					return collect($field)->transform(function ($relation) use ($block) {
116
						$class = $block->getChildClassName('Block', $relation['content']['component']);
117
						$relationClass = new $class($relation['content'], $block);
118
119
						$relationClass->addMeta([
120
							'name' => $relation['name'],
121
							'published_at' => $relation['published_at'],
122
							'full_slug' => $relation['full_slug'],
123
						]);
124
125
						return $relationClass;
126
					});
127
				}
128
129
				// this field holds blocks!
130
				if (array_key_exists('component', $field[0])) {
131
					return collect($field)->transform(function ($childBlock) use ($block) {
132
						$class = $block->getChildClassName('Block', $childBlock['component']);
133
134
						return new $class($childBlock, $block);
135
					});
136
				}
137
138
				// multi assets
139
				if (array_key_exists('filename', $field[0])) {
140
					return new MultiAsset($field, $block);
141
				}
142
			}
143
		}
144
145
		// just return the array
146
		return $field;
147
	}
148
149
150
	/**
151
	 * Check if given string is a string image field
152
	 *
153
	 * @param  $filename
154
	 * @return boolean
155
	 */
156
	public function isStringImageField($filename){
157
		$allowed_extentions = ['.jpg', '.jpeg', '.png', '.gif', '.webp'];
158
159
		return is_string($filename) && Str::of($filename)->lower()->endsWith($allowed_extentions);
160
	}
161
}