Kgapi

agerangetype.cpp
1/*
2 * SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
3 * SPDX-FileCopyrightText: 2022 Claudio Cambra <claudio.cambra@kde.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 * SPDX-License-Identifier: LGPL-3.0-only
7 * SPDX-License-Identifier: LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "agerangetype.h"
11
12#include "fieldmetadata.h"
13#include "peopleservice.h"
14
15#include <QJsonValue>
16#include <QSharedData>
17
18#include <algorithm>
19
20namespace KGAPI2::People
21{
22class AgeRangeType::Private : public QSharedData
23{
24public:
25 explicit Private() = default;
26 Private(const Private &) = default;
27 Private(Private &&) noexcept = delete;
28 Private &operator=(const Private &) = delete;
29 Private &operator=(Private &&) noexcept = delete;
30 ~Private() = default;
31
32 bool operator==(const Private &other) const
33 {
34 return ageRange == other.ageRange && metadata == other.metadata;
35 }
36
37 bool operator!=(const Private &other) const
38 {
39 return !(*this == other);
40 }
41
42 AgeRangeType::AgeRange ageRange{};
43 FieldMetadata metadata{};
44};
45
47 : d(new Private)
48{
49}
50
52AgeRangeType::AgeRangeType(AgeRangeType &&) noexcept = default;
53AgeRangeType &AgeRangeType::operator=(const AgeRangeType &) = default;
54AgeRangeType &AgeRangeType::operator=(AgeRangeType &&) noexcept = default;
55AgeRangeType::~AgeRangeType() = default;
56
57bool AgeRangeType::operator==(const AgeRangeType &other) const
58{
59 return *d == *other.d;
60}
61
62bool AgeRangeType::operator!=(const AgeRangeType &other) const
63{
64 return !(*this == other);
65}
66
67AgeRangeType::AgeRangeType::AgeRange AgeRangeType::ageRange() const
68{
69 return d->ageRange;
70}
71
73{
74 d->ageRange = value;
75}
77{
78 return d->metadata;
79}
80
82{
83 d->metadata = value;
84}
85
86AgeRangeType AgeRangeType::fromJSON(const QJsonObject &obj)
87{
88 AgeRangeType ageRangeType;
89
90 if(!obj.isEmpty()) {
91 const auto val = obj.value(QStringLiteral("ageRange")).toString();
92
93 if (val == QLatin1StringView("LESS_THAN_EIGHTEEN")) {
95 } else if (val == QLatin1StringView("EIGHTEEN_TO_TWENTY")) {
97 } else if (val == QLatin1StringView("TWENTY_ONE_OR_OLDER")) {
99 } else {
101 }
102 }
103
104 return ageRangeType;
105}
106
107QJsonValue AgeRangeType::toJSON() const
108{
109 QJsonObject obj;
110
111 switch (d->ageRange) {
113 PeopleUtils::addValueToJsonObjectIfValid(obj, "ageRange", QStringLiteral("AGE_RANGE_UNSPECIFIED"));
114 break;
116 PeopleUtils::addValueToJsonObjectIfValid(obj, "ageRange", QStringLiteral("LESS_THAN_EIGHTEEN"));
117 break;
119 PeopleUtils::addValueToJsonObjectIfValid(obj, "ageRange", QStringLiteral("EIGHTEEN_TO_TWENTY"));
120 break;
122 PeopleUtils::addValueToJsonObjectIfValid(obj, "ageRange", QStringLiteral("TWENTY_ONE_OR_OLDER"));
123 break;
124 }
125 // Skip, field metadata is only useful for receiving -> PeopleUtils::addValueToJsonObjectIfValid(obj, "metadata", d->metadata.toJSON());
126 return obj;
127}
128
129QList<AgeRangeType> AgeRangeType::fromJSONArray(const QJsonArray &data)
130{
132
133 for(const auto &ageRangeObj : data) {
134 if(ageRangeObj.isObject()) {
135 const auto objectifiedAgeRange = ageRangeObj.toObject();
136 if(objectifiedAgeRange.contains(QStringLiteral("ageRange"))) {
137 ageRanges.append(fromJSON(objectifiedAgeRange));
138 }
139 }
140 }
141
142 return ageRanges;
143}
144
145} // namespace KGAPI2::People
A person's age range.
FieldMetadata metadata() const
Metadata about the age range.
void setMetadata(const FieldMetadata &value)
Sets value of the metadata property.
void setAgeRange(AgeRangeType::AgeRange value)
Sets value of the ageRange property.
AgeRangeType()
Constructs a new AgeRangeType.
@ TWENTY_ONE_OR_OLDER
Twenty-one and older.
@ EIGHTEEN_TO_TWENTY
Between eighteen and twenty.
@ LESS_THAN_EIGHTEEN
Younger than eighteen.
AgeRangeType::AgeRange ageRange() const
The age range.
Metadata about a field.
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
void append(QList< T > &&value)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:36:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.