1: <?php
2: /**
3: * ContactList entity
4: *
5: * @author Yannick Huerre <dev@sheoak.fr>
6: * @copyright 2015 (c) Oasiswork
7: * @license https://opensource.org/licenses/MIT MIT
8: */
9: namespace Crunchmail\Entities;
10:
11: /**
12: * Message entity class
13: */
14: class ContactListEntity extends \Crunchmail\Entities\GenericEntity
15: {
16: /**
17: * Resource mapping
18: *
19: * @var array
20: */
21: protected static $resources = [
22: 'merge' => 'ContactList'
23: ];
24:
25: /**
26: * To string
27: *
28: * @return string
29: */
30: public function __toString()
31: {
32: return $this->name;
33: }
34:
35: /**
36: * Merge contact list
37: *
38: * @param array $list list of urls or entities to merge
39: *
40: * @return ContactListEntity
41: */
42: public function merge(array $list)
43: {
44: $ids = [];
45:
46: foreach ($list as $row)
47: {
48: // array of urls or entities
49: $ids[] = is_string($row) ? $row : $row->url;
50: }
51:
52: return $this->merge->post($ids);
53: }
54:
55: /**
56: * Import CSV
57: * By default, keep all fields
58: * If fields is specified, only keep those ones
59: *
60: * @param string $content CSV content
61: * @param array $fields Keep only thoses fields
62: *
63: * @return ContactListEntity
64: */
65: public function import($content, array $fields = null)
66: {
67: $resource = $this->_resource->client->contacts;
68:
69: // keep only specified fields
70: // ?fields=[a,b,c]
71: if (!is_null($fields))
72: {
73: $fields = '[' . implode(',', $fields) . ']';
74: $resource = $resource->filter(['fields' => $fields]);
75: }
76:
77: return $resource->contentType('text/csv; charset=utf-8')->post($content);
78: }
79: }
80: