-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathUIDField.inc
More file actions
83 lines (78 loc) · 4.31 KB
/
UIDField.inc
File metadata and controls
83 lines (78 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace RESTAPI\Fields;
require_once 'RESTAPI/autoloader.inc';
use RESTAPI;
use RESTAPI\Core\Field;
/**
* Defines a Field that contains a unique ID. This field will automatically populate a unique ID that is immutable.
*/
class UIDField extends Field {
/**
* Defines the UIDField object and sets its options.
* @param string $prefix A specific string to prefix to the generated UID.
* @param bool $sensitive Mask this field when displayed in a Form.
* @param bool $representation_only Set to `true` to make this field only present in its representation form. This
* effectively prevents the Field from being converted to an internal value which is saved to the pfSense config.
* This should only be used for Fields that do not relate directly to a configuration value.
* @param string $verbose_name The detailed name for this Field. This name will be used in non-programmatic areas
* like web pages and help text. This Field will default to property name assigned to the parent Model with
* underscores converted to spaces.
* @param string $verbose_name_plural The plural form of $verbose_name. This defaults to $verbose_name with `s`
* suffixed or `es` suffixes to strings already ending with `s`.
* @param string $internal_name Assign a different field name to use when referring to the internal field as it's
* stored in the pfSense configuration.
* @param string $internal_namespace Sets the namespace this field belongs to internally. This can be used to nest
* the Fields internal value under a specific namespace as an associative array. This only applies to the internal
* value, not the representation value.
* @param array $referenced_by An array that specifies other Models and Field's that reference this Field's parent
* Model using this Field's value. This will prevent the parent Model object from being deleted while it is actively
* referenced by another Model object. The array key must be the name of the Model class that references this Field,
* and the value must be a Field within that Model. The framework will automatically search for any existing Model
* objects that have the referenced Field assigned a value that matches this Field's value.
* @param array $conditions An array of conditions the field must meet to be included. This allows you to specify
* conditions of other Fields within the parent Model context. For example, if the parent Model context has two
* Fields, one field named `type` and the other being this field; and you only want this field to be included if
* `type` is equal to `type1`, you could assign ["type" => "type1"] to this parameter.
* @param array $validators An array of Validator objects to run against this field.
* @param string $help_text Set a description for this field. This description will be used in API documentation.
*/
public function __construct(
string $prefix = '',
bool $sensitive = false,
bool $representation_only = false,
string $verbose_name = '',
string $verbose_name_plural = '',
string $internal_name = '',
string $internal_namespace = '',
array $referenced_by = [],
array $conditions = [],
array $validators = [],
string $help_text = '',
) {
parent::__construct(
type: 'string',
unique: true,
default: uniqid(prefix: $prefix),
editable: false,
read_only: true,
sensitive: $sensitive,
representation_only: $representation_only,
verbose_name: $verbose_name,
verbose_name_plural: $verbose_name_plural,
internal_name: $internal_name,
internal_namespace: $internal_namespace,
referenced_by: $referenced_by,
conditions: $conditions,
validators: $validators,
help_text: $help_text,
);
}
/**
* Converts the field value to its representation form from it's internal pfSense configuration value.
* @param mixed $internal_value The internal value from the pfSense configuration.
* @return string The field value in its representation form.
*/
protected function _from_internal(mixed $internal_value): string {
return strval($internal_value);
}
}