|
| 1 | +# Win32Model |
| 2 | + |
| 3 | +The `Win32Model` is the base class for all of the models that represent the |
| 4 | +[Win32 provider classes](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider). Each model is |
| 5 | +composed of either another model or classes that represent |
| 6 | +[CIM providers](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/cim-wmi-provider). |
| 7 | + |
| 8 | +## Example Composition |
| 9 | +``` |
| 10 | +PHPWinTools\WmiScripting\Models\UserAccount |
| 11 | + → Models\Account |
| 12 | + → CIM\CimLogicalElement |
| 13 | + → CIM\CimManagedSystemElement |
| 14 | + → Models\Win32Model |
| 15 | +``` |
| 16 | + |
| 17 | +## Properties and Attributes |
| 18 | + |
| 19 | +While each model has all of its possible properties defined, the intended method to get the value of these properties |
| 20 | +is via [`getAttribute`](#getattribute). This allows for mutating or casting the value open retrieval via an |
| 21 | +[attribute method](#attribute-methods), as well as, defining calculated attributes. |
| 22 | + |
| 23 | +There are also a couple of properties that should be considered when extending from an existing model. |
| 24 | + |
| 25 | +### $connection |
| 26 | +#### `protected $connection = 'default'` |
| 27 | + |
| 28 | +This is the name of connection that should be used when calling [`query`](#query) and [`all`](#all) |
| 29 | +without providing a value for `$connection`. |
| 30 | + |
| 31 | +### $wmi_class_name |
| 32 | +#### `protected $wmi_class_name` |
| 33 | + |
| 34 | +This is an optional property, but only if the model you are calling extends an existing `Win32Model`. |
| 35 | + |
| 36 | +### $attribute_casting |
| 37 | +#### `protected $attribute_casting = []` |
| 38 | + |
| 39 | +Attributes will attempted to be casted to the given type if defined within this array. All definitions of |
| 40 | +`$attribute_casting` are merged from the models ancestors by default. This allows you to define `$attribute_casting` in |
| 41 | +both a parent and a child without risk of having the values overridden. |
| 42 | + |
| 43 | +```php |
| 44 | +Available castings |
| 45 | + |
| 46 | +protected $attribute_casting = [ |
| 47 | + 'attribute' => 'array', |
| 48 | + 'attribute' => 'bool', |
| 49 | + 'attribute' => 'int', |
| 50 | + 'attribute' => 'string', |
| 51 | +] |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +### $merge_parent_casting |
| 56 | +#### `protected $merge_parent_casting = true` |
| 57 | + |
| 58 | +If set to `false` then the `$attribute_casting` will not be merged from the ancestors and only the castings defined |
| 59 | +within the child's `$attribute_casting` will be considered. |
| 60 | + |
| 61 | +### $hidden_attributes |
| 62 | +#### `protected $hidden_attributes = []` |
| 63 | + |
| 64 | +Any attributes listed in this array will not be included when casting a [model to array](#toarray). Like |
| 65 | +[`$attribute_casting`](#attribute-casting), the values in this array are merged from the model's ancestors. |
| 66 | + |
| 67 | +### $merge_parent_hidden_attributes |
| 68 | +#### `protected $merge_parent_hidden_attributes = true` |
| 69 | + |
| 70 | +If set to `false` then the `$hidden_attributes` will not be merged from the ancestors and only the castings defined |
| 71 | +within the child's `$hidden_attributes` will be considered. |
| 72 | + |
| 73 | +### Calculated Attributes |
| 74 | + |
| 75 | +Calculated attributes are simply [attribute methods](#attribute-methods) without an associated property. |
| 76 | + |
| 77 | +## Methods |
| 78 | + |
| 79 | +Below are the most useful methods available to all classes that extend `Win32Model`. |
| 80 | + |
| 81 | +### Attribute Methods |
| 82 | +#### `getSomePropertyAttribute($value)` |
| 83 | +#### `getSomePropertyAttribute()` |
| 84 | + |
| 85 | +Attribute methods are getter methods that are named after the associated property sandwiched by `get` and `Attribute`. |
| 86 | +This is very similar and absolutely inspired by Laravel's Eloquent attribute mutator. |
| 87 | + |
| 88 | +```php |
| 89 | +function getSomePropertyAttribute($value) |
| 90 | +{ |
| 91 | + return strtoupper($value); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +If there is a property that matches the name within `get` `Attribute` then the value of that property will be passed to |
| 96 | +the method otherwise nothing is passed. This can be used to create calculated attributes that have may not have an |
| 97 | +associated property. |
| 98 | + |
| 99 | +These are evaluated during [`getAttribute`](#getattribute) calls and when calling [`toArray`](#toarray). |
| 100 | + |
| 101 | +### mapConstant |
| 102 | +#### `protected function mapConstant(string $mapping_string_class, $constant)` |
| 103 | + |
| 104 | +This method can be used with an [attribute method](#attribute-methods) to convert a property that typically returns |
| 105 | +an integer that is mappable to a string. |
| 106 | + |
| 107 | +The `$mapping_string_class` parameter expects a class name of a class that extends `MappingString\Mappings` and are |
| 108 | +meant to represent |
| 109 | +[MappingStrings](https://docs.microsoft.com/en-us/windows/win32/wmisdk/standard-qualifiers#mappingstrings). |
| 110 | + |
| 111 | +### query |
| 112 | +#### `Win32Model::query($connection = null)` |
| 113 | + |
| 114 | +This method allows you to query a model directly and returns an instance of `Query\Builder`. It accepts a `$connection` |
| 115 | +argument that can be provided in a number of ways. |
| 116 | + |
| 117 | +```php |
| 118 | +// Uses default model connection or falls back to default Config connection. |
| 119 | +Win32Model::query(); |
| 120 | + |
| 121 | +// Finds the connection by its name. |
| 122 | +Win32Model::('named_connection'); |
| 123 | + |
| 124 | +// Uses the connection as is. |
| 125 | +Win32Model::(Connection::simple('computer', 'user', 'password'); |
| 126 | +``` |
| 127 | + |
| 128 | +If you pass a `Connection` instance it will not be stored in the `Config` container. This can be useful if you have a |
| 129 | +number of connections you do not wish to persist. |
| 130 | + |
| 131 | +### all |
| 132 | +#### `Win32Model::all($connection = null)` |
| 133 | + |
| 134 | +This method will call `all` on the `Query\Builder` and return an instance of `ModelCollection`. You can pass a |
| 135 | +connection to this method in the same ways that are available to [`query`](#query). |
| 136 | + |
| 137 | +### getAttribute |
| 138 | +#### `$model->getAttribute($attribute, $default = null)` |
| 139 | + |
| 140 | +This is the preferred method for retrieving values from a model. When using this method the property will be evaluated |
| 141 | +for [castings](#attribute-casting), [attribute methods](#attribute-methods), and |
| 142 | +[calculated attributes](#attribute-methods). |
| 143 | + |
| 144 | +The second parameter, `$default`, allows you to set a default value if no value could be determined otherwise `null` is |
| 145 | +returned. |
| 146 | + |
| 147 | +### toArray |
| 148 | +#### `$model->toArray()` |
| 149 | + |
| 150 | +Transforms the model class into an array. |
| 151 | + |
| 152 | +This is accomplished by evaluating each property, [attribute method](#attribute-methods), [casting](#attribute-casting), |
| 153 | +and [hidden attribute](#hidden-attribute). |
0 commit comments