Comic::getFilters()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
c 0
b 0
f 0
rs 8.8571
cc 1
eloc 31
nc 1
nop 0
1
<?php
2
3
namespace Chadicus\Marvel\Api\Entities;
4
5
use Chadicus\Marvel\Api;
6
use DominionEnterprises\Util;
7
8
/**
9
 * Represents a Marvel API Comic Entity
10
 *
11
 * @property-read integer $id The unique ID of the comic resource.
12
 * @property-read integer $digitalId The ID of the digital comic representation of this comic. Will be if the comic is
13
 *                                   not available digitally.
14
 * @property-read string $title The canonical title of the comic.
15
 * @property-read string $issueNumber The number of the issue in the series (will generally be for collection formats).
16
 * @property-read string $variantDescription If the issue is a variant (e.g. an alternate cover, second printing, or
17
 *                                           director's cut), a text description of the variant.
18
 * @property-read string $description The preferred description of the comic.
19
 * @property-read DateTime $modified The date the resource was most recently modified.
20
 * @property-read string $isbn The ISBN for the comic (generally only populated for collection formats).
21
 * @property-read string $upc The UPC barcode number for the comic (generally only populated for periodical formats).
22
 * @property-read string $diamondCode The Diamond code for the comic.
23
 * @property-read string $ean The EAN barcode for the comic.
24
 * @property-read string $issn The ISSN barcode for the comic.
25
 * @property-read string $format The publication format of the comic e.g. comic, hardcover, trade paperback.
26
 * @property-read integer $pageCount The number of story pages in the comic.
27
 * @property-read TextObject[] $textObjects A set of descriptive text blurbs for the comic.
28
 * @property-read string $resourceURI The canonical URL identifier for this resource.
29
 * @property-read Url[] $urls A set of public web site URLs for the resource.
30
 * @property-read Summary $series A summary representation of the series to which this comic belongs.
31
 * @property-read Summary[] $variants A list of variant issues for this comic (includes the "original" issue if the
32
 *                                    current issue is a variant).
33
 * @property-read Summary[] $collections A list of collections which include this comic (will generally be empty if the
34
 *                                       comic's format is a collection).
35
 * @property-read Summary[] $collectedIssues A list of issues collected in this comic (will generally be empty for
36
 *                                           periodical formats such as "comic" or "magazine").
37
 * @property-read Date[] $dates A list of key dates for this comic.
38
 * @property-read Price[] $prices A list of prices for this comic.
39
 * @property-read Image $thumbnail The representative image for this comic.
40
 * @property-read Image[] $images A list of promotional images associated with this comic.
41
 * @property-read ResourceList $creators A resource list containing the creators associated with this comic.
42
 * @property-read ResourceList $characters A resource list containing the characters which appear in this comic.
43
 * @property-read ResourceList $stories A resource list containing the stories which appear in this comic.
44
 * @property-read ResourceList $events A resource list containing the events in which this comic appears.
45
 */
46
class Comic extends AbstractEntity
47
{
48
    /**
49
     * The name of the comic API resource
50
     *
51
     * @const string
52
     */
53
    const API_RESOURCE = 'comics';
54
55
    /**
56
     * @see AbstractEntity::getFilters()
57
     *
58
     * @return array
59
     */
60
    final protected function getFilters() : array
61
    {
62
        return [
63
            'id' => [['int', true]],
64
            'digitalId' => [['int', true]],
65
            'title' => [['string', true, 0]],
66
            'issueNumber' => [['strval'], ['string', true, 0]],
67
            'variantDescription' => [['string', true, 0]],
68
            'description' => [['string', true, 0]],
69
            'modified' => [['date', true]],
70
            'isbn' => [['string', true, 0]],
71
            'upc' => [['string', true, 0]],
72
            'diamondCode' => [['string', true, 0]],
73
            'ean' => [['string', true, 0]],
74
            'issn' => [['string', true, 0]],
75
            'format' => [['string', true, 0]],
76
            'pageCount' => [['int', true]],
77
            'textObjects' => ['default' => [], ['text-objects']],
78
            'resourceURI' => [['string', true, 0]],
79
            'urls' => ['default' => [], ['_urls']],
80
            'series' => ['default' => new Summary(), ['summary']],
81
            'variants' => ['default' => [], ['summaries']],
82
            'collections' => ['default' => [], ['summaries']],
83
            'collectedIssues' => ['default' => [], ['summaries']],
84
            'dates' => ['default' => [], ['_dates']],
85
            'prices' => ['default' => [], ['prices']],
86
            'thumbnail' => ['default' => new Image(), ['image']],
87
            'images' => ['default' => [], ['images']],
88
            'creators' => ['default' => new ResourceList(), ['resource-list']],
89
            'characters' => ['default' => new ResourceList(), ['resource-list']],
90
            'stories' => ['default' => new ResourceList(), ['resource-list']],
91
            'events' => ['default' => new ResourceList(), ['resource-list']],
92
        ];
93
    }
94
95
    /**
96
     * Returns a collection containing all Comics which match the given criteria.
97
     *
98
     * @param Api\Client $client   The API Client.
99
     * @param array      $criteria The criteria for searching.
100
     *
101
     * @return Api\Collection
102
     */
103
    final public static function findAll(Api\Client $client, array $criteria = []) : Api\Collection
104
    {
105
        $filters = [
106
            'format' => [
107
                [
108
                    'in',
109
                    [
110
                        'comic', 'hardcover', 'trade paperback', 'magazine', 'digest',
111
                        'graphic novel', 'digital comic', 'infinite comic',
112
                    ]
113
                ],
114
            ],
115
            'formatType' => [['in', ['comic', 'collection']]],
116
            'noVariants' => [['bool'], ['bool-convert']],
117
            'dateDescriptor' => [['in', ['lastWeek', 'thisWeek', 'nextWeek', 'thisMonth']]],
118
            'fromDate' => [['date', true]],
119
            'toDate' => [['date', true]],
120
            'hasDigitalIssue' => [['bool'], ['bool-convert']],
121
            'modifiedSince' => [['date', true], ['date-format', 'c']],
122
            'creators' => [['ofScalars', [['uint']]], ['implode', ',']],
123
            'characters' => [['ofScalars', [['uint']]], ['implode', ',']],
124
            'series' => [['ofScalars', [['uint']]], ['implode', ',']],
125
            'events' => [['ofScalars', [['uint']]], ['implode', ',']],
126
            'stories' => [['ofScalars', [['uint']]], ['implode', ',']],
127
            'sharedAppearances' => [['ofScalars', [['uint']]], ['implode', ',']],
128
            'collaborators' => [['ofScalars', [['uint']]], ['implode', ',']],
129
            'orderBy' => [
130
                [
131
                    'in',
132
                    [
133
                        'focDate', 'onsaleDate', 'title', 'issueNumber', 'modified',
134
                        '-focDate', '-onsaleDate', '-title', '-issueNumber', '-modified',
135
                    ],
136
                ]
137
            ],
138
139
        ];
140
141
        list($success, $filteredCriteria, $error) = Api\Filterer::filter($filters, $criteria);
142
        Util::ensure(true, $success, $error);
143
        $toDate = Util\Arrays::get($filteredCriteria, 'toDate');
144
        $fromDate = Util\Arrays::get($filteredCriteria, 'fromDate');
145
        if ($toDate !== null && $fromDate !== null) {
146
            unset($filteredCriteria['toDate'], $filteredCriteria['fromDate']);
147
            $filteredCriteria['dateRange'] = "{$fromDate->format('c')},{$toDate->format('c')}";
148
        }
149
150
        return new Api\Collection($client, self::API_RESOURCE, $filteredCriteria);
151
    }
152
}
153