Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 5 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2019 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License and the CiviCRM Licensing Exception along | | |
22 | | with this program; if not, contact CiviCRM LLC | | |
23 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
24 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
25 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
26 | +--------------------------------------------------------------------+ | |
27 | */ | |
28 | ||
29 | /** | |
30 | * | |
31 | * @package CRM | |
32 | * @copyright CiviCRM LLC (c) 2004-2019 | |
33 | * $Id$ | |
34 | * | |
35 | */ | |
36 | ||
37 | ||
19b53e5b C |
38 | namespace Civi\Api4\Service\Spec; |
39 | ||
40 | class RequestSpec { | |
41 | ||
42 | /** | |
43 | * @var string | |
44 | */ | |
45 | protected $entity; | |
46 | ||
47 | /** | |
48 | * @var string | |
49 | */ | |
50 | protected $action; | |
51 | ||
52 | /** | |
53 | * @var FieldSpec[] | |
54 | */ | |
55 | protected $fields = []; | |
56 | ||
57 | /** | |
58 | * @param string $entity | |
59 | * @param string $action | |
60 | */ | |
61 | public function __construct($entity, $action) { | |
62 | $this->entity = $entity; | |
63 | $this->action = $action; | |
64 | } | |
65 | ||
66 | public function addFieldSpec(FieldSpec $field) { | |
67 | $this->fields[] = $field; | |
68 | } | |
69 | ||
70 | /** | |
71 | * @param $name | |
72 | * | |
73 | * @return FieldSpec|null | |
74 | */ | |
75 | public function getFieldByName($name) { | |
76 | foreach ($this->fields as $field) { | |
77 | if ($field->getName() === $name) { | |
78 | return $field; | |
79 | } | |
80 | } | |
81 | ||
82 | return NULL; | |
83 | } | |
84 | ||
85 | /** | |
86 | * @return array | |
87 | * Gets all the field names currently part of the specification | |
88 | */ | |
89 | public function getFieldNames() { | |
90 | return array_map(function(FieldSpec $field) { | |
91 | return $field->getName(); | |
92 | }, $this->fields); | |
93 | } | |
94 | ||
95 | /** | |
96 | * @return array|FieldSpec[] | |
97 | */ | |
98 | public function getRequiredFields() { | |
99 | return array_filter($this->fields, function (FieldSpec $field) { | |
100 | return $field->isRequired(); | |
101 | }); | |
102 | } | |
103 | ||
104 | /** | |
105 | * @return array|FieldSpec[] | |
106 | */ | |
107 | public function getConditionalRequiredFields() { | |
108 | return array_filter($this->fields, function (FieldSpec $field) { | |
109 | return $field->getRequiredIf(); | |
110 | }); | |
111 | } | |
112 | ||
113 | /** | |
114 | * @param array $fieldNames | |
115 | * Optional array of fields to return | |
116 | * @return FieldSpec[] | |
117 | */ | |
118 | public function getFields($fieldNames = NULL) { | |
119 | if (!$fieldNames) { | |
120 | return $this->fields; | |
121 | } | |
122 | $fields = []; | |
123 | foreach ($this->fields as $field) { | |
124 | if (in_array($field->getName(), $fieldNames)) { | |
125 | $fields[] = $field; | |
126 | } | |
127 | } | |
128 | return $fields; | |
129 | } | |
130 | ||
131 | /** | |
132 | * @return string | |
133 | */ | |
134 | public function getEntity() { | |
135 | return $this->entity; | |
136 | } | |
137 | ||
138 | /** | |
139 | * @return string | |
140 | */ | |
141 | public function getAction() { | |
142 | return $this->action; | |
143 | } | |
144 | ||
145 | } |