/
HardwareGroup.swift
78 lines (57 loc) · 2.01 KB
/
HardwareGroup.swift
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
//
// HardwareGroup.swift
// JamfKit
//
// Copyright © 2017-present JamfKit. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
@objc(JMFKHardwareGroup)
public class HardwareGroup: BaseObject {
// MARK: - Constants
static let IsSmartKey = "is_smart"
static let CriteriaKey = "criteria"
static let SiteKey = "site"
// MARK: - Properties
@objc
public var isSmart = false
@objc
public var criteria = [HardwareGroupCriterion]()
@objc
public var site: Site?
// MARK: - Initialization
public required init?(json: [String: Any], node: String = "") {
isSmart = json[HardwareGroup.IsSmartKey] as? Bool ?? false
criteria = HardwareGroup.parseCriteria(json: json)
site = HardwareGroup.parseSite(from: json)
super.init(json: json)
}
override init?(identifier: UInt, name: String) {
super.init(identifier: identifier, name: name)
}
// MARK: - Functions
public override func toJSON() -> [String: Any] {
var json = super.toJSON()
json[MobileDeviceGroup.IsSmartKey] = isSmart
if !criteria.isEmpty {
let criterions = criteria.map { criterion -> [String: [String: Any]] in
return ["criterion": criterion.toJSON()]
}
json[MobileDeviceGroup.CriteriaKey] = criterions
}
if let site = site {
json[MobileDeviceGroup.SiteKey] = site.toJSON()
}
return json
}
private static func parseCriteria(json: [String: Any]) -> [HardwareGroupCriterion] {
return BaseObject.parseElements(from: json, nodeKey: MobileDeviceGroup.CriteriaKey, singleNodeKey: "criterion")
}
private static func parseSite(from json: [String: Any]) -> Site? {
guard
let rawSite = json[MobileDeviceGroup.SiteKey] as? [String: Any],
let site = Site(json: rawSite) else {
return nil
}
return site
}
}