1 <?php
2 namespace MailChimp\Lists;
3
4 class MergeFields extends Lists
5 {
6
7 /**
8 * Get a list of all merge fields (formerly merge vars) for a list.
9 * Available query fields:
10 * array["fields"] array list of strings of response fields to return
11 * array["exclude_fields"] array list of strings of response fields to exclude (not to be used with "fields")
12 * array["count"] int number of records to return
13 * array["offset"] int number of records from a collection to skip.
14 * array["type"] string The merge field type.
15 * array["required"] boolean The boolean value if the merge field is required.
16 * @param string $list_id
17 * @param array $query (See Above) OPTIONAL associative array of query parameters.
18 * @return object
19 */
20 public function getMergeFields($list_id, array $query = [])
21 {
22 return self::execute("GET", "lists/{$list_id}/merge-fields", $query);
23 }
24
25 /**
26 * Get information about a specific merge field in a list.
27 * Available query fields:
28 * array["fields"] array list of strings of response fields to return
29 * array["exclude_fields"] array list of strings of response fields to exclude (not to be used with "fields")
30 * @param string $list_id
31 * @param int $merge_id
32 * @param array $query (See Above) OPTIONAL associative array of query parameters.
33 * @return object
34 */
35 public function getMergeField($list_id, $merge_id, array $query = [])
36 {
37 return self::execute("GET", "lists/{$list_id}/merge-fields/{$merge_id}", $query);
38 }
39
40 /**
41 * Create a new merge field for a specific list
42 * "name" string Required. The name of the merge field
43 * "type" string Required. The type for the merge field
44 * array["optional_settings"]
45 * ["tag"] string The tag used in MailChimp campaigns and for the /members endpoint.
46 * ["required"] boolean The boolaen value if the merge field is required
47 * ["default_value"] string The type for the merge field
48 * ["public"] boolean Whether the merge field is displayed on the signup form.
49 * ["display_order"] int The order the merge field displays on the signup form.
50 * ["options"] array Extra option for some merge field tidy_parse_string
51 * ["default_country"] int In an address field, the default country code if none supplied
52 * ["phone_format"] string In a phone field, the phone number tupe: US or International
53 * ["date_format"] string In a date or birthday field, the format of the date
54 * ["choices"] array In a radio or dropdown non-group field, the available options.
55 * ["size"] int In a text field, the default length fo the text field
56 * ["help_text"] string Extra text to help the subscrber fill out the form
57 * @param string $list_id
58 * @param string $name
59 * @param string $type
60 * @param array $optional_settings
61 * @return object
62 */
63 public function createMergeField($list_id, $name, $type, array $optional_settings = [])
64 {
65 $optional_fields = ["tag", "required", "default_value", "public", "display_order", "options", "help_text"];
66
67 $data = [
68 "name" => $name,
69 "type" => $type
70 ];
71
72 // If the optional fields are passed, process them against the list of optional fields.
73 if (isset($optional_settings)) {
74 $data = array_merge($data, self::optionalFields($optional_fields, $optional_settings));
75 }
76
77 return self::execute("POST", "lists/{$list_id}/merge-fields", $data);
78 }
79
80 /**
81 * Update a specific merge field in a list
82 * @param string $list_id
83 * @param string $merge_id
84 * @param array $data (See createMergeField() for structure)
85 * @return object
86 */
87 public function updateMergeField($list_id, $merge_id, array $data = [])
88 {
89 return self::execute("PATCH", "lists/{$list_id}/merge-fields/{$merge_id}", $data);
90 }
91
92
93 /**
94 * Delete a specific merge field in a list.
95 * @param string $list_id
96 * @param int $merge_id
97 */
98 public function deleteMergeField($list_id, $merge_id)
99 {
100 return self::execute("DELETE", "lists/{$list_id}/merge-fields/{$merge_id}");
101 }
102
103 }
104