Asset::getValueLabel()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Phpsa\Datastore;
4
5
use ReflectionClass;
6
use Illuminate\Support\Facades\View;
7
8
use Phpsa\Datastore\DatastoreException;
9
use Phpsa\Datastore\Datastore;
10
use Phpsa\Datastore\Models\DatastorePages;
11
use Phpsa\Datastore\Ams\AutoCallBackAdderAsset;
12
use Phpsa\Datastore\Ams\AutoCallBackAsset;
13
use Phpsa\Datastore\Ams\AutoCompleteAsset;
14
use Phpsa\Datastore\Ams\BooleanAsset;
15
use Phpsa\Datastore\Ams\DatepickerAsset;
16
use Phpsa\Datastore\Ams\DropdownAsset;
17
use Phpsa\Datastore\Ams\FileAsset;
18
use Phpsa\Datastore\Ams\HeadingAsset;
19
use Phpsa\Datastore\Ams\HtmlAsset;
20
use Phpsa\Datastore\Ams\IdentityAsset;
21
use Phpsa\Datastore\Ams\ImageAsset;
22
use Phpsa\Datastore\Ams\MetatextAsset;
23
use Phpsa\Datastore\Ams\StringAsset;
24
use Phpsa\Datastore\Ams\SubHeadingAsset;
25
use Phpsa\Datastore\Ams\TextAsset;
26
27
28
use Illuminate\Support\Str;
29
30
/**
31
 * Asset class - this is an abstract basis for the assets that shou
32
 *
33
 * @author Craig Smith <[email protected]>
34
 */
35
class Asset{
36
37
	/**
38
	 * Constants for the different asset types!
39
	 */
40
	const ASSET              = "asset";
41
	const PROP               = "property";
42
43
44
	const AUTOCALLBACKADDER  = AutoCallBackAdderAsset::class; // ajax powered autocomplete backed on a callback
45
	const AUTOCALLBACK       = AutoCallBackAsset::class;      // ajax powered autocomplete backed on a callback
46
	const AUTOCOMPLETE       = AutoCompleteAsset::class;
47
	const BOOL               = BooleanAsset::class;
48
	const DATEPICKER         = DatepickerAsset::class;
49
	const DROPDOWN           = DropdownAsset::class;
50
	const FILE               = FileAsset::class;
51
	const HEADING            = HeadingAsset::class;
52
	const HTML               = HtmlAsset::class;
53
	const IDENTITY           = IdentityAsset::class;
54
	const IMG                = ImageAsset::class;
55
	const METATEXT			 = MetatextAsset::class;
56
	const STRING             = StringAsset::class;
57
	const SUBHEADING         = SubHeadingAsset::class;
58
	const TEXT               = TextAsset::class;
59
60
61
	const FOLDER             = "amsFolderAsset";
62
63
	/**
64
	 * help tips ypou may want to include properties
65
	 *
66
	 * @var string
67
	 */
68
	public $help             = null;
69
70
	/**
71
	 * Sets the required rule for a property
72
	 *
73
	 * @var bool
74
	 */
75
	public $required         = true;
76
77
	/**
78
	 * which property is the actual value name for your asset - target one of the properties
79
	 *
80
	 * @var string
81
	 */
82
	public $value_equals     = null;
83
84
	/**
85
	 * Assets namespace, most often extended as an asset, however may be a property if adding a new input type.
86
	 *
87
	 * @var string
88
	 */
89
	public $namespace        = self::ASSET;
90
91
	/**
92
	 * Generally left alone unless overrideing to be a property then should have the instance class
93
	 *
94
	 * @var string
95
	 */
96
	public $type             = 'asset';
97
98
	/**
99
	 * Properties for thie asset - used to define the property assets to use to build up the asset
100
	 *
101
	 * @var array
102
	 */
103
	public $properties       = array();
104
105
	/**
106
	 * Public name - This is used to identify
107
	 *
108
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
109
	 */
110
	public $name             = null;
111
112
	/**
113
	 *  just a shortname to use instead of the full $name above
114
	 *
115
	 * @var string
116
	 */
117
	public $shortname        = null;
118
119
	/**
120
	 * the max number of instances, 0 means unlimited - normally used for presets like the robots.txt
121
	 *
122
	 * @var int
123
	 */
124
	public $max_instances    = 0;
125
126
	/**
127
	 * IE if private is true, we do not show the page link block... and idsable the meta
128
	 * but if private is false, we show the page link and let meta decide for itself:
129
	 * @var bool
130
	 */
131
	public $private          = false;
132
133
	/**
134
	 * @deprecated Initial -
135
	 * @TODO - Remove from here and from teh database object
136
	 *
137
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
138
	 */
139
	public $embedded         = null;
140
141
	/**
142
	 * @deprecated version
143
	 * * @TODO - Remove from here and from teh database object
144
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
145
	 */
146
	public $theme            = null;
147
148
	/**
149
	 * set to 'off' to turn it off
150
	 *
151
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
152
	 */
153
	public $meta_description = null;
154
155
	 /**
156
	 * set to 'off' to turn it off
157
	 *
158
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
159
	 */
160
	public $meta_keywords    = null;
161
162
	/**
163
	 * set to 'off' to turn it off
164
	 *
165
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
166
	 */
167
	public $page_css         = null;
168
169
	/**
170
	 * set to 'off' to turn it off
171
	 *
172
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
173
	 */
174
	public $page_js          = null;
175
176
177
	public $options          = false;						// Options for the element -- should actually be removed from the database table most likely
178
	public $status_equals    = null;                        // does the asset has a status linked to a prop
179
	public $start_date       = null;                        // does the asset has a start date linked to a prop
180
	public $end_date         = null;                        // does the asset has an end date linked to a prop
181
	public $comment_equals   = null;                        // does the asset allow comments (linked to a specific property - similar to status_equals)
182
	public $warning          = null;                        // field for warnings - deprected in favour of validation_messages
183
	//public $published		= null;							//published status
184
	//public $validation_messages = null					//validation messages
185
186
	public $key              = null;
187
	public $module           = null;
188
	public $value            = null;
189
	public $status           = null;
190
	public $meta             = false;                       // ancillary meta info/data - similar to options
191
	public $callback         = null;
192
193
	/**
194
	 * Link a child asset in on teh form, this will alllwo you to create a set of children on the form.
195
	 *
196
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
197
	 */
198
	public $children         = null;
199
200
	/**
201
	 * Is this a child only asset? if marked as true does not auto display on menu options and can only be linked in as a child property to an asset.
202
	 *
203
	 * @var bool
204
	 */
205
	public $is_child         = false;
206
207
	/**
208
	 * Asset that can be a child, ie an article belonging to a category
209
	 *
210
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
211
	 */
212
	public $accept           = null;
213
214
	/**
215
	 * assets that can be children, a value means it is limited to that many children, -1 means unlimited
216
	 * @TodO update this to better display with a selct / multiSelect null / 0 / -1 or any falsy is unlimited, else it is limited to that number...
217
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
218
	 */
219
	public $accept_limit     = null;
220
221
222
	/**
223
	 * generate the route for this asset
224
	 * Overwrite this in your own assets to generate your own route
225
	 */
226
	public static function route(DatastorePages $record, $route = null){
227
		if(null === $route){
228
			$route = 'frontend.ams.page.slug';
229
		}
230
		$page = $record->page;
0 ignored issues
show
Bug introduced by
The property page does not seem to exist on Phpsa\Datastore\Models\DatastorePages. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
231
		return route($route, ['slug' => $page->slug]);
232
	}
233
234
	/**
235
	 * holds the about information - should be overwritten by most Assets
236
	 * @abstract - should be extended in children
237
	 */
238
	public static function about()
239
	{
240
		throw new DatastoreException("All Assets require an about method to describe them");
241
	}
242
243
	/**
244
	 * this is an overridable method used to create the value equals based on the field to use
245
	 * @param string $value
246
	 * @return string
247
	 */
248
	public static function valueEquals($value)
249
	{
250
		return $value;
251
	}
252
253
	/**
254
	 * Get our Default properties for this asset
255
	 *
256
	 * @return array
257
	 */
258
	public static function getDefaultProperties(){
259
		$obj	 = new ReflectionClass(get_called_class());
260
		return $obj->getDefaultProperties();
261
	}
262
263
	/**
264
	 * returns the label for the asset public identifier
265
	 *
266
	 * @todo - test if we still actually use this!!!
267
	 * @deprecated initially ?
268
	 * @return string
269
	 */
270
	public static function getValueLabel()
271
	{
272
		$props = self::getDefaultProperties();
273
		if(empty($props['value_equals']) || empty($props['properties'][$props['value_equals']]['name'])){
274
			throw new DatastoreException("value_equals is a required parameter");
275
		}
276
277
		$out = $props['properties'][$props['value_equals']]['name'];
278
		unset($obj, $props);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj seems to be never defined.
Loading history...
279
		return $out;
280
	}
281
282
	/**
283
	 * Gets information on the asset
284
	 *
285
	 * @param mixed $lookup - property to lookup or false for all
286
	 * @return mixed - array of properties, string or false.
287
	 */
288
	public static function getInfo($lookup = false)
289
	{
290
		$props = self::getDefaultProperties();
291
292
		if ($lookup)
293
		{
294
			if (isset($props[$lookup]))
295
			{
296
				$out = $props[$lookup];
297
				unset($obj, $props);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $obj seems to be never defined.
Loading history...
298
				return $out;
299
			}
300
			else
301
			{
302
				unset($obj, $props);
303
				return false;
304
			}
305
		}
306
		unset($obj);
307
		return $props;
308
	}
309
310
	/**
311
	 * Returns the Assets view path syntax
312
	 *
313
	 * @param string $type
314
	 *
315
	 * @return string
316
	 */
317
	public static function getAssetView(string $type = 'render') : string
318
	{
319
		$className = class_basename(get_called_class());
320
		$ns = str_replace('\\', '', __NAMESPACE__);
321
		return Str::kebab($ns) . "::{$type}." . Str::kebab($className);
322
	}
323
324
	/**
325
	 * Creates the asset form
326
	 *
327
	 * @param array $args - form data
328
	 * @param boolean $injectedform - if is a sub-form of another asset
329
	 * @return string - the form html
330
	 * @TODO :: todo not workin gyet
331
	 * Should most likely parse throug to blade or laravel somewhere
332
	 */
333
	public static function form($args, $injectedform = false)
334
	{
335
		$template = self::getAssetView('form');
336
		if (empty($args['unique_id']))
337
		{
338
			$args['unique_id'] = uniqid();
339
		}
340
		$data['data']			 = $args;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.
Loading history...
341
		$data['asset_classname'] = $injectedform;
342
		return View::make($template, $data)->render();
343
	}
344
345
	/**
346
	 * Renders the asset's markup
347
	 *
348
	 * @param array $args
349
	 * @param string $params
350
	 * @return string
351
	 * * Should most likely parse throug to blade or laravel somewhere
352
	 */
353
	public static function render($args = [], $params = false)
354
	{
355
356
		if(is_string($params)){
357
			$template = $params;
358
		} else {
359
			$template = is_array($params) && !empty($params['view']) ? $params['view'] : self::getAssetView();
0 ignored issues
show
introduced by
The condition is_array($params) is always false.
Loading history...
360
		}
361
362
		$params['assetClass'] = get_called_class();
363
		$args['params'] = $params;
364
365
		return View::exists($template) ? View::make($template, $args)->render() : static::html($args);
0 ignored issues
show
Bug introduced by
The method html() does not exist on Phpsa\Datastore\Asset. It seems like you code against a sub-type of said class. However, the method does not exist in Phpsa\Datastore\Ams\BlockAsset or Phpsa\Datastore\Ams\TestAsset or Phpsa\Datastore\Ams\ContentAsset or Phpsa\Datastore\Ams\Article\ItemAsset or Phpsa\Datastore\Ams\Article\CategoryAsset or Phpsa\Datastore\Ams\RobotsTxtAsset or Phpsa\Datastore\Ams\TabAsset or Phpsa\Datastore\Ams\TabsAsset. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

365
		return View::exists($template) ? View::make($template, $args)->render() : static::/** @scrutinizer ignore-call */ html($args);
Loading history...
366
	}
367
368
	/**
369
	 * Retrieve the current assets namespace
370
	 * @return string - current namespace
371
	 */
372
	public static function getNamespace()
373
	{
374
		return self::getInfo('namespace');
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::getInfo('namespace') also could return the type array|false which is incompatible with the documented return type string.
Loading history...
375
	}
376
377
	/**
378
	 * Creates a new Asset Instance
379
	 * @param string $className
380
	 * @param array $args
381
	 * @return Asset
382
	 * @TODO Use Namespacing to autoload!!!
383
	 */
384
	public static function factory($className, $args = array())
385
	{
386
		$ref = new ReflectionClass($className);
387
		return $ref->newInstanceArgs($args);
388
	}
389
390
	/**
391
	 * gets the url path for the asset
392
	 *
393
	 * @param [type] $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
394
	 *
395
	 * @return void
396
	 * @author Craig Smith <[email protected]>
397
	 */
398
	public static function getPath($className = null){
399
		if(null === $className) {
400
			$className = get_called_class();
401
		}
402
		return Helpers::getPath($className);
0 ignored issues
show
Bug introduced by
Are you sure the usage of Phpsa\Datastore\Helpers::getPath($className) targeting Phpsa\Datastore\Helpers::getPath() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
403
	}
404
405
}