Issues (446)

src/integrations/RecipeFeedMeField.php (19 issues)

1
<?php
2
/**
3
 * Recipe plugin for Craft CMS 3.x
4
 *
5
 * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial
6
 * conversion, portion calculation, and JSON-LD microdata support
7
 *
8
 * @link      https://nystudio107.com
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2020 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\recipe\integrations;
13
14
use Cake\Utility\Hash;
15
use craft\feedme\base\Field;
16
use craft\feedme\base\FieldInterface;
17
use craft\feedme\helpers\DataHelper;
18
19
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
20
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
21
 * @package   Recipe
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
22
 * @since     1.2.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
23
 *
24
 * @property-read string $mappingTemplate
25
 */
0 ignored issues
show
Missing @link tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @category tag in class comment
Loading history...
26
class RecipeFeedMeField extends Field implements FieldInterface
27
{
28
    // Properties
29
    // =========================================================================
30
31
    public static $name = 'Recipe';
32
    public static $class = 'nystudio107\recipe\fields\Recipe';
33
34
35
    // Templates
36
    // =========================================================================
37
38
    public function getMappingTemplate(): string
0 ignored issues
show
You must use "/**" style comments for a function comment
Loading history...
39
    {
40
        return 'recipe/_integrations/feed-me';
41
    }
42
43
44
    // Public Methods
45
    // =========================================================================
46
47
    public function parseField(): mixed
0 ignored issues
show
You must use "/**" style comments for a function comment
Loading history...
48
    {
49
        $preppedData = [];
50
51
        $fields = Hash::get($this->fieldInfo, 'fields');
52
53
        if (!$fields) {
54
            return null;
55
        }
56
57
        foreach ($fields as $subFieldHandle => $subFieldInfo) {
58
            // Check for sub-sub fields - bit dirty...
59
            $subfields = Hash::get($subFieldInfo, 'fields');
60
61
            if ($subfields) {
62
                foreach ($subfields as $subSubFieldHandle => $subSubFieldInfo) {
63
                    // Handle array data, man I hate Feed Me's data mapping now...
64
                    $content = DataHelper::fetchArrayValue($this->feedData, $subSubFieldInfo);
65
66
                    if (is_array($content)) {
67
                        foreach ($content as $key => $value) {
68
                            $preppedData[$subFieldHandle][$key][$subSubFieldHandle] = $value;
69
                        }
70
                    } else {
71
                        $preppedData[$subFieldHandle][$subSubFieldHandle] = $content;
72
                    }
73
                }
74
            } else {
75
                $preppedData[$subFieldHandle] = DataHelper::fetchValue($this->feedData, $subFieldInfo);
76
            }
77
        }
78
79
        // Protect against sending an empty array
80
        if (!$preppedData) {
81
            return null;
82
        }
83
84
        return $preppedData;
85
    }
86
}
87