-
Notifications
You must be signed in to change notification settings - Fork 2
/
AttachmentFactory.php
104 lines (86 loc) · 3.13 KB
/
AttachmentFactory.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
/**
* A factory class for statically creating importable attachment objects.
*/
namespace Geniem\Oopi\Factory\Importable;
use Geniem\Oopi\Exception\TypeException;
use Geniem\Oopi\Importable\AttachmentImportable;
use Geniem\Oopi\Interfaces\ImportableFactory;
use Geniem\Oopi\Interfaces\Importable;
use Geniem\Oopi\Util;
/**
* Class AttachmentImportableFactory
*
* @package Geniem\Oopi\Factory\Importable
*/
class AttachmentFactory implements ImportableFactory {
/**
* Creates a post importable object from the data.
*
* @param string $oopi_id A unique id for the importable.
* @param mixed $data The importable data.
*
* @return AttachmentImportable
*/
public static function create( string $oopi_id, $data ) : Importable {
$importer = Util::get_prop( $data, 'importer', null );
$error_handler = Util::get_prop( $data, 'error_handler', null );
$importable = new AttachmentImportable( $oopi_id, $importer, $error_handler );
$src = Util::get_prop( $data, 'src', null );
$title = Util::get_prop( $data, 'title', null );
$alt = Util::get_prop( $data, 'alt', null );
$caption = Util::get_prop( $data, 'caption', null );
$description = Util::get_prop( $data, 'description', null );
$parent_wp_id = Util::get_prop( $data, 'parent_wp_id', null );
$parent_oopi_id = Util::get_prop( $data, 'parent_oopi_id', null );
$is_thumbnail = Util::get_prop( $data, 'is_thumbnail', false );
$meta = Util::get_prop( $data, 'meta', null );
$language = Util::get_prop( $data, 'language', null );
$acf = Util::get_prop( $data, 'acf', null );
if ( $title ) {
$importable->set_title( $title );
}
if ( $src ) {
$importable->set_src( $src );
}
if ( $alt ) {
$importable->set_alt( $alt );
}
if ( $caption ) {
$importable->set_caption( $caption );
}
if ( $description ) {
$importable->set_description( $description );
}
if ( $parent_wp_id ) {
$importable->set_parent_wp_id( $parent_wp_id );
}
if ( $parent_oopi_id ) {
$importable->set_parent_oopi_id( $parent_oopi_id );
}
// Defaults to false.
$importable->set_is_thumbnail( $is_thumbnail );
if ( is_array( $meta ) ) {
try {
$importable->set_meta( $meta );
}
catch ( TypeException $e ) {
$importable->get_error_handler()->set_error( $e->getMessage(), $e );
}
}
// Language
if ( ! empty( $language ) ) {
try {
$importable->set_language( $language );
}
catch ( TypeException $e ) {
$importable->get_error_handler()->set_error( $e->getMessage(), $e );
}
}
// Advanced Custom Fields
if ( is_array( $acf ) ) {
$importable->set_acf( $acf );
}
return $importable;
}
}