1+ <?php
2+ namespace Addon \Dcim ;
3+
4+ use Core as C ;
5+
6+ abstract class Api_Abstract extends C \Addon \Api_Abstract
7+ {
8+ const FIELD_ID = 'entity_id ' ;
9+ const FIELD_NAME = 'name ' ;
10+
11+ const WILDCARD = '* ' ;
12+ const SEPARATOR_PATH = ', ' ;
13+
14+ /**
15+ * @var string
16+ */
17+ protected static $ _parentAdapter = __NAMESPACE__ .'\Main ' ;
18+
19+ /**
20+ * @var Addon\Dcim\Main
21+ */
22+ protected static $ _DCIM = null ; // Global DCIM (enabled)
23+
24+ /**
25+ * @var Addon\Dcim\Main[]
26+ */
27+ protected static $ _aDCIM = array (); // a = all/array/available DCIM
28+
29+ /**
30+ * @var Addon\Dcim\Main
31+ */
32+ protected $ _DCIM_ = null ; // Local DCIM (for this instance)
33+
34+
35+ public function __construct ($ objectId = null )
36+ {
37+ parent ::__construct ($ objectId );
38+ $ this ->_DCIM_ = &$ this ->_ownerAdapter ; // /!\ A executer avant _setObjectId
39+ $ this ->_setObjectId ($ objectId ); // @todo temp
40+ }
41+
42+ public static function objectIdIsValid ($ objectId )
43+ {
44+ return C \Tools::is ('int&&>0 ' , $ objectId );
45+ }
46+
47+ public function hasObjectId ()
48+ {
49+ return ($ this ->_objectId !== null );
50+ }
51+
52+ public function getObjectId ()
53+ {
54+ return $ this ->_objectId ;
55+ }
56+
57+ public function objectExists ()
58+ {
59+ if ($ this ->_objectExists !== null ) {
60+ return $ this ->_objectExists ;
61+ }
62+ elseif ($ this ->hasObjectId ()) {
63+ $ this ->_objectExists = ($ this ->_getObject () !== false );
64+ return $ this ->_objectExists ;
65+ }
66+ else {
67+ return false ;
68+ }
69+ }
70+
71+ protected function _setObjectLabel ($ objectLabel )
72+ {
73+ if (!$ this ->objectExists () && C \Tools::is ('string&&!empty ' , $ objectLabel )) {
74+ $ this ->_objectLabel = $ objectLabel ;
75+ return true ;
76+ }
77+ else {
78+ return false ;
79+ }
80+ }
81+
82+ public function hasObjectLabel ()
83+ {
84+ return ($ this ->getObjectLabel () !== false );
85+ }
86+
87+ public function getObjectLabel ()
88+ {
89+ if ($ this ->_objectLabel !== null ) { // /!\ Ne pas appeler hasObjectLabel sinon boucle infinie
90+ return $ this ->_objectLabel ;
91+ }
92+ elseif ($ this ->hasObjectId ()) { // /!\ Ne pas appeler objectExists sinon boucle infinie
93+ $ objectDatas = $ this ->_getObject ();
94+ $ this ->_objectLabel = ($ objectDatas !== false ) ? ($ objectDatas [static ::FIELD_NAME ]) : (false );
95+ return $ this ->_objectLabel ;
96+ }
97+ else {
98+ return false ;
99+ }
100+ }
101+
102+ /**
103+ * Méthode courte comme getPath
104+ */
105+ public function getLabel ()
106+ {
107+ return $ this ->getObjectLabel ();
108+ }
109+
110+ public function getTemplateName ()
111+ {
112+ if ($ this ->objectExists ()) {
113+ $ result = $ this ->_DCIM ->resolvToTemplate (static ::OBJECT_TYPE , $ this ->getObjectId ());
114+ return ($ this ->_DCIM ->isValidReturn ($ result )) ? ($ result ) : (false );
115+ }
116+ else {
117+ return false ;
118+ }
119+ }
120+
121+ /**
122+ * @param string $category
123+ * @param null|string $attribute
124+ * @return false|string
125+ */
126+ public function getUserAttrField ($ category , $ attribute = null )
127+ {
128+ if (!C \Tools::is ('string&&!empty ' , $ attribute )) {
129+ $ attribute = $ category ;
130+ $ category = 'default ' ;
131+ $ noCateg = true ;
132+ }
133+
134+ $ attrField = $ this ->_DCIM ->getUserAttrName ($ category , $ attribute );
135+ return ($ attrField === false && isset ($ noCateg )) ? ($ attribute ) : ($ attrField );
136+ }
137+
138+ /**
139+ * @param string $category
140+ * @param null|string $attribute
141+ * @return false|string
142+ */
143+ public function getUserAttribute ($ category , $ attribute = null )
144+ {
145+ if ($ this ->objectExists ())
146+ {
147+ $ attrField = $ this ->getUserAttrField ($ category , $ attribute );
148+
149+ if ($ attrField !== false ) {
150+ $ result = $ this ->_DCIM ->getUserAttrById (static ::OBJECT_TYPE , $ this ->getObjectId (), $ attrField );
151+ return ($ this ->_DCIM ->isValidReturn ($ result )) ? ($ result ) : (false );
152+ }
153+ }
154+
155+ return false ;
156+ }
157+
158+ public function __get ($ name )
159+ {
160+ switch ($ name )
161+ {
162+ case 'dcim ' :
163+ case '_DCIM ' : {
164+ return self ::$ _DCIM ;
165+ }
166+ case 'id ' : {
167+ return $ this ->getObjectId ();
168+ }
169+ case 'label ' : {
170+ return $ this ->getObjectLabel ();
171+ }
172+ case 'templateName ' : {
173+ return $ this ->getTemplateName ();
174+ }
175+ default : {
176+ throw new Exception ("This attribute ' " .$ name ."' does not exist " , E_USER_ERROR );
177+ }
178+ }
179+ }
180+
181+ public function __call ($ method , $ parameters = null )
182+ {
183+ if (substr ($ method , 0 , 3 ) === 'get ' )
184+ {
185+ $ name = substr ($ method , 3 );
186+ $ name = mb_strtolower ($ name );
187+
188+ switch ($ name )
189+ {
190+ case 'label ' : {
191+ return $ this ->getObjectLabel ();
192+ }
193+ }
194+ }
195+
196+ throw new Exception ("Method ' " .$ method ."' does not exist " , E_USER_ERROR );
197+ }
198+
199+ /**
200+ * @param string $section
201+ * @param array $attributes
202+ * @return false|string
203+ */
204+ protected static function _getReportName ($ section , array $ attributes )
205+ {
206+ if (array_key_exists ($ section , static ::REPORT_NAMES ))
207+ {
208+ $ reportNames = static ::REPORT_NAMES [$ section ];
209+ $ attributes = implode ('& ' , $ attributes );
210+
211+ if (is_string ($ reportNames )) {
212+ return $ reportNames ;
213+ }
214+ elseif (is_array ($ reportNames ) && array_key_exists ($ attributes , $ reportNames )) {
215+ return $ reportNames [$ attributes ];
216+ }
217+ }
218+
219+ return false ;
220+ }
221+
222+ /**
223+ * @param string $reportName
224+ * @param null|array $args
225+ * @param false|string $fieldNameFilter
226+ * @throw Addon\Dcim\Exception
227+ * @return array
228+ */
229+ protected static function _getObjectsFromReport ($ reportName , array $ args = null , $ fieldNameFilter = false )
230+ {
231+ if ($ args !== null )
232+ {
233+ array_walk ($ args , function (&$ item )
234+ {
235+ if (!C \Tools::is ('human ' , $ item )) {
236+ $ item = static ::WILDCARD ;
237+ }
238+ });
239+ }
240+
241+ // @todo use _getReportName
242+ $ results = self ::$ _DCIM ->getReportResults (static ::REPORT_NAMES [$ reportName ], $ args );
243+
244+ if (C \Tools::is ('array&&count>0 ' , $ results ))
245+ {
246+ if ($ fieldNameFilter !== false ) {
247+ $ results = array_column ($ results , $ fieldNameFilter );
248+ }
249+
250+ return $ results ;
251+ }
252+ else {
253+ throw new Exception ("Unable to retrieve objects from report ' " .$ reportName ."' " , E_USER_ERROR );
254+ }
255+ }
256+
257+ /**
258+ * @param Addon\Dcim\Main|Addon\Dcim\Main[] $DCIM
259+ * @return bool
260+ */
261+ public static function setDcim ($ DCIM )
262+ {
263+ return self ::setAdapter ($ DCIM );
264+ }
265+
266+ /**
267+ * @param Addon\Dcim\Main|Addon\Dcim\Main[] $adapter
268+ * @throw Core\Exception
269+ * @return bool
270+ */
271+ public static function setAdapter ($ adapter )
272+ {
273+ $ status = parent ::setAdapter ($ adapter );
274+
275+ if ($ status ) {
276+ self ::$ _DCIM = &self ::$ _adapter ;
277+ self ::$ _aDCIM = &self ::$ _allAdapters ;
278+ }
279+
280+ return $ status ;
281+ }
282+
283+ /**
284+ * @return null|Addon\Dcim\Main|Addon\Dcim\Main[]
285+ */
286+ public static function getDcim ()
287+ {
288+ return self ::getAdapter ();
289+ }
290+
291+ /**
292+ * @param string $key
293+ * @return bool
294+ */
295+ public static function enableDcim ($ key )
296+ {
297+ return self ::enableAdapter ($ key );
298+ }
299+
300+ /**
301+ * @return null|Addon\Dcim\Main
302+ */
303+ public static function getDcimEnabled ()
304+ {
305+ return self ::getAdapterEnabled ();
306+ }
307+ }
0 commit comments