class Incident in Radioactivity 4.0.x
Same name and namespace in other branches
- 8.3 src/Incident.php \Drupal\radioactivity\Incident
- 8.2 src/Incident.php \Drupal\radioactivity\Incident
Data class for Radioactivity Incident.
@package Drupal\radioactivity
Hierarchy
- class \Drupal\radioactivity\Incident implements IncidentInterface
Expanded class hierarchy of Incident
4 files declare their use of Incident
- EmitController.php in src/
Controller/ EmitController.php - IncidentTest.php in tests/
src/ Unit/ IncidentTest.php - RadioactivityEmitter.php in src/
Plugin/ Field/ FieldFormatter/ RadioactivityEmitter.php - RadioactivityReferenceEmitter.php in src/
Plugin/ Field/ FieldFormatter/ RadioactivityReferenceEmitter.php
File
- src/
Incident.php, line 13
Namespace
Drupal\radioactivityView source
class Incident implements IncidentInterface {
/**
* The incident field name.
*
* @var string
*/
private $fieldName;
/**
* The incident entity type.
*
* @var string
*/
private $entityType;
/**
* The incident entity id.
*
* @var string|int
*/
private $entityId;
/**
* The id of the referenced radioactivity entity.
*
* @var string|int
*/
private $targetId;
/**
* The incident energy.
*
* @var int|float
*/
private $energy;
/**
* The incident hash.
*
* @var string
*/
private $hash;
/**
* Constructor.
*
* @param string $field_name
* The field name from the incident.
* @param string $entity_type
* The entity type from the incident.
* @param string|int $entity_id
* The entity id from the incident.
* @param string|int $target_id
* The id from the referenced radioactivity entity.
* @param int|float $energy
* The energy from the incident.
* @param string $hash
* The hash from the incident.
*/
public function __construct($field_name, $entity_type, $entity_id, $target_id, $energy, $hash = NULL) {
$this->fieldName = $field_name;
$this->entityType = $entity_type;
$this->entityId = $entity_id;
$this->targetId = $target_id;
$this->energy = $energy;
$this->hash = $hash;
}
/**
* {@inheritdoc}
*/
public function isValid() {
return strcmp($this->hash, $this
->calculateHash()) === 0;
}
/**
* Calculate hash for this incident.
*
* @return string
* The calculated hash of this incident.
*/
private function calculateHash() {
return sha1(implode('##', [
$this->fieldName,
$this->entityType,
$this->entityId,
$this->targetId,
$this->energy,
Settings::getHashSalt(),
]));
}
/**
* {@inheritdoc}
*/
public function toJson() {
return Json::encode([
'fn' => $this->fieldName,
'et' => $this->entityType,
'id' => $this->entityId,
'ti' => $this->targetId,
'e' => $this->energy,
'h' => $this
->calculateHash(),
]);
}
/**
* Create an Incident from data received in an http request.
*
* @param array $data
* Associative array of incident data.
*
* @return \Drupal\radioactivity\IncidentInterface
* An Incident object.
*/
public static function createFromPostData(array $data) {
$data += [
'fn' => '',
'et' => '',
'id' => '',
'ti' => 0,
'e' => 0,
'h' => '',
];
return new Incident($data['fn'], $data['et'], $data['id'], $data['ti'], $data['e'], $data['h']);
}
/**
* Create an Incident from field items, an item within it and a formatter.
*
* @param object $items
* The items containing item.
* @param object $item
* The item in question.
* @param object $formatter
* The formatter in use.
*
* @return \Drupal\radioactivity\IncidentInterface
* The incident object.
*/
public static function createFromFieldItemsAndFormatter($items, $item, $formatter) {
return new Incident($items
->getName(), $item
->getEntity()
->getEntityTypeId(), $item
->getEntity()
->id(), isset($item->target_id) ? $item->target_id : 0, $formatter
->getSetting('energy'));
}
/**
* {@inheritdoc}
*/
public function getFieldName() {
return $this->fieldName;
}
/**
* {@inheritdoc}
*/
public function getEntityTypeId() {
return $this->entityType;
}
/**
* {@inheritdoc}
*/
public function getEntityId() {
return $this->entityId;
}
/**
* {@inheritdoc}
*/
public function getTargetId() {
return $this->targetId;
}
/**
* {@inheritdoc}
*/
public function getEnergy() {
return $this->energy;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Incident:: |
private | property | The incident energy. | |
Incident:: |
private | property | The incident entity id. | |
Incident:: |
private | property | The incident entity type. | |
Incident:: |
private | property | The incident field name. | |
Incident:: |
private | property | The incident hash. | |
Incident:: |
private | property | The id of the referenced radioactivity entity. | |
Incident:: |
private | function | Calculate hash for this incident. | |
Incident:: |
public static | function | Create an Incident from field items, an item within it and a formatter. | |
Incident:: |
public static | function | Create an Incident from data received in an http request. | |
Incident:: |
public | function |
Returns the incident energy. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Returns the incident entity id. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Returns the incident entity type. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Returns the incident field name. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Returns the id of the referenced radioactivity entity. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Test validity of the Incident. Overrides IncidentInterface:: |
|
Incident:: |
public | function |
Convert to JSON format. Overrides IncidentInterface:: |
|
Incident:: |
public | function | Constructor. |