-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathFieldFactory.php
104 lines (91 loc) · 2.99 KB
/
FieldFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace Corcel\Acf;
use Corcel\Acf\Field\Boolean;
use Corcel\Acf\Field\DateTime;
use Corcel\Acf\Field\File;
use Corcel\Acf\Field\Gallery;
use Corcel\Acf\Field\Image;
use Corcel\Acf\Field\PageLink;
use Corcel\Acf\Field\PostObject;
use Corcel\Acf\Field\Repeater;
use Corcel\Acf\Field\FlexibleContent;
use Corcel\Acf\Field\Select;
use Corcel\Acf\Field\Term;
use Corcel\Acf\Field\Text;
use Corcel\Acf\Field\User;
use Corcel\Model;
use Illuminate\Support\Collection;
/**
* Class FieldFactory.
*
* @author Junior Grossi <juniorgro@gmail.com>
*/
class FieldFactory
{
protected static $defaultTypes = [
'text' => Text::class,
'textarea' => Text::class,
'number' => Text::class,
'email' => Text::class,
'url' => Text::class,
'password' => Text::class,
'wysiwyg' => Text::class,
'editor' => Text::class,
'oembed' => Text::class,
'embed' => Text::class,
'color_picker' => Text::class,
'select' => Text::class,
'checkbox' => Text::class,
'radio' => Text::class,
'image' => Image::class,
'img' => Image::class,
'file' => File::class,
'gallery' => Gallery::class,
'true_false' => Boolean::class,
'boolean' => Boolean::class,
'post_object' => PostObject::class,
'post' => PostObject::class,
'relationship' => PostObject::class,
'page_link' => PageLink::class,
'taxonomy' => Term::class,
'term' => Term::class,
'user' => User::class,
'date_picker' => DateTime::class,
'date_time_picker' => DateTime::class,
'time_picker' => DateTime::class,
'repeater' => Repeater::class,
'flexible_content' => FlexibleContent::class,
];
private function __construct()
{
}
/**
* @param string $name
* @param Post $post
* @param null|string $type
*
* @return FieldInterface|Collection|string
*/
public static function make($name, Model $post, $type = null)
{
if (null === $type) {
$fakeText = new Text($post);
$key = $fakeText->fetchFieldKey($name);
if ($key === null) { // Field does not exist
return null;
}
$type = $fakeText->fetchFieldType($key);
}
$customTypes = [];
if (class_exists('\Config') && \Config::has('corcel.acf')) {
$customTypes = \Config::get('corcel.acf')['custom_types'];
}
$types = array_merge(self::$defaultTypes, $customTypes);
if (!empty($types[$type])) {
$field = new $types[$type]($post);
$field->process($name);
return $field;
}
return null;
}
}