Writing own property classes

From Sunhill Framework Documentation

When writing an own property class first check if you could use one of the Types or Semantics.

Human readable format / storage format

Internally the storage only uses php basic types. These are not always in a pretty format for the human eye. So there are some methods to prettify the output.

formatForHumans(mixed $input): mixed

Takes the raw data from storage and outputs a friendly format for the human eye.

formatForStorage(mixed $input): mixed

Sometimes the internal storage format is different from that the values are assigned to the property. So every data is passed through this method before passing to the storage.

formatFromStorage(mixed $input): mixed

Sometimes the internal storage format is different from that the values are assigned to the property. So every data is passed through this method before passing from the storage.

formatFromInput(mixed $input): mixed

Sometimes a property can take different data types. The data is passed through this method to unify it to a datatype that can be processed further.

InfoMarket interaction

requestTerminalItem(string $name)

Sometimes a property has a pseudo element that can be accessed. So all unknown requests are passed through this method.

passItemRequest(string $name,array $path)

If a property has sub elements, this method passed a request to one of those sub elements.

Value validation

isValid(mixed $input): bool

Checks if the given input is valid for this property.

handleNullValue()

Is called whenever null is assigned as value for this property.

Information

::setupInfos()

Is a static method of this property that is called whenever an information is requested. It can be overwritten by a property to return certain informations.

parent::setupInfos() should be called as first intruction.

::addInfo(string $key, $value, bool $translatable = false)

Is called out of setupInfos to add a single information atom. $key is the name of this information atom $value is the value of this information atom $translatable indicated if the information value should be passed through __() function when requested.

Relations

Properties are bound to queries. Due the fact that the comparison of for example integers is always the same, there is a relation handling.

::$allowed_relations

Static array that lists all relations that are allowed for this property. The array can be defined for every property or inherit its values from its parent. There are some predefined constants with some typical relations:

  • EQUALITY = for properties that have no size comparison like "greater". Only "=" for equality, "<>" and "!=" for inequality, "in" for test if the value is in a list of values and "notin" for the test if a value is not in a list.
  • SIZE = for properties that have a comparison like "greater". "<" for smaller, "<=" for smaller or equal, ">" for greater, ">=" for greater or equal and "between" if a value is between two given values.
  • WITHNULL = for properties that can be null. "isnull" for the test if this value is null and "isnotnull" for the test if this value is not null.

::getAllowedRelation(): array

Just returns the static variable ::$allowed_relations (see above).Normally you shouldn't overwrite this method (instead overwrite $allowed_relations).

::isAllowedRelation(string $relation): bool

Returns true if the given relation is in the list of allowed relations. Normally you shouldn't overwrite this method (instead overwrite $allowed_relations).

testRelation(string $relation, $compare): bool

Tests if the relation is allowed and than calls doTestRelation(). Normally you shouldn't overwrite this method, overwrite doTestRelation() instead.

doTestRelation(string $relation, $compare): ?bool

Tests if the value of this property matches the given relation and value. This method returns true, if the condition matches, false if it doesn't match and null if the relation is not defined. With the latter it is possible to pass the test to a child class.