2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace System\Traits;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Property container trait
|
|
|
|
*
|
2017-04-24 13:38:19 +02:00
|
|
|
* Adds properties and methods for classes that could define properties,
|
2014-05-14 23:24:20 +10:00
|
|
|
* like components or report widgets.
|
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
|
|
|
|
|
|
|
trait PropertyContainer
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array Contains the object property values.
|
|
|
|
*/
|
2014-08-03 11:57:51 +10:00
|
|
|
protected $properties = [];
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates the properties against the defined properties of the class.
|
|
|
|
* This method also sets default properties.
|
|
|
|
* @param array $properties The supplied property values.
|
|
|
|
* @return array The validated property set, with defaults applied.
|
|
|
|
*/
|
|
|
|
public function validateProperties(array $properties)
|
|
|
|
{
|
2016-10-19 07:21:09 +11:00
|
|
|
$definedProperties = $this->defineProperties() ?: [];
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
// Determine and implement default values
|
|
|
|
$defaultProperties = [];
|
|
|
|
foreach ($definedProperties as $name => $information) {
|
|
|
|
if (array_key_exists('default', $information)) {
|
|
|
|
$defaultProperties[$name] = $information['default'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$properties = array_merge($defaultProperties, $properties);
|
|
|
|
|
|
|
|
// @todo Check required properties
|
|
|
|
|
|
|
|
return $properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-22 20:27:44 +10:00
|
|
|
* Defines the properties used by this class.
|
2014-05-14 23:24:20 +10:00
|
|
|
* This method should be used as an override in the extended class.
|
|
|
|
*/
|
|
|
|
public function defineProperties()
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets multiple properties.
|
2016-01-23 15:37:15 -05:00
|
|
|
* @param array $properties
|
2017-01-08 10:25:23 +11:00
|
|
|
* @return void
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function setProperties($properties)
|
|
|
|
{
|
2016-02-17 17:49:52 +11:00
|
|
|
$this->properties = $this->validateProperties($properties);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a property value
|
2016-01-23 15:37:15 -05:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
* @return void
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function setProperty($name, $value)
|
|
|
|
{
|
2016-01-23 15:37:15 -05:00
|
|
|
$this->properties[$name] = $value;
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all properties.
|
2016-01-23 15:37:15 -05:00
|
|
|
* @return array
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
|
|
|
public function getProperties()
|
|
|
|
{
|
|
|
|
return $this->properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a defined property value or default if one is not set.
|
2014-06-06 21:38:34 +10:00
|
|
|
* @param string $name The property name to look for.
|
|
|
|
* @param string $default A default value to return if no name is found.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @return string The property value or the default specified.
|
|
|
|
*/
|
|
|
|
public function property($name, $default = null)
|
|
|
|
{
|
2014-08-22 18:45:05 +11:00
|
|
|
return array_key_exists($name, $this->properties)
|
2014-05-14 23:24:20 +10:00
|
|
|
? $this->properties[$name]
|
|
|
|
: $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns options for multi-option properties (drop-downs, etc.)
|
|
|
|
* @param string $property Specifies the property name
|
|
|
|
* @return array Return an array of option values and descriptions
|
|
|
|
*/
|
|
|
|
public function getPropertyOptions($property)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2014-10-18 11:58:50 +02:00
|
|
|
}
|