Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (success)
80.00%
4 / 5
CRAP
92.86% covered (success)
92.86%
26 / 28
Data
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (success)
80.00%
4 / 5
18.12
92.86% covered (success)
92.86%
26 / 28
 get
0.00% covered (danger)
0.00%
0 / 1
13.17
90.00% covered (success)
90.00%
18 / 20
 getBundleType
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getEntityType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 setEntityType
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 isField
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace Drupal\data_api;
class Data extends \AKlump\Data\Data
{
    protected $entityType = null;
    /**
     * @inheritdoc
     */
    public function get($subject, $path, $defaultValue = null)
    {
        if (empty($subject)) {
            return $defaultValue;
        }
        // This will make sure $path is an array.
        $this->validate($subject, $path, $defaultValue);
        // When we know the entity type, we can use Drupal's field api functions.
        // This will take care of the translation.
        $processed = false;
        if (count($path) <= 3 && ($bundle = $this->getBundleType($subject))) {
            if (($field_name = reset($path)) && $this->isField($bundle, $field_name)) {
                array_shift($path);
                $processed = true;
                $items = field_get_items($this->getEntityType(), $subject, $field_name);
                if (count($path) === 0) {
                    return !empty($items) ? $items : $defaultValue;
                }
                $delta = array_shift($path);
                if (count($path) === 0) {
                    return array_key_exists($delta, $items) ? $items[$delta] : $defaultValue;
                }
                $key = array_shift($path);
                if (count($path) === 0) {
                    return array_key_exists($key, $items[$delta]) ? $items[$delta][$key] : $defaultValue;
                }
            }
        }
        // If we've processed per Drupal API functions then return, otherwise lean on parent class.
        return $processed ? $subject : parent::get($subject, $path, $defaultValue);
    }
    /**
     * Return the bundle type of the entity.
     *
     * @param string $subject
     *
     * @return null|bool
     */
    protected function getBundleType($subject)
    {
        if (empty($entity_type = $this->getEntityType())) {
            return null;
        }
        list(, , $bundle) = entity_extract_ids($entity_type, $subject);
        return $bundle;
    }
    /**
     * @return string|null
     */
    protected function getEntityType()
    {
        return $this->entityType;
    }
    /**
     * @param string $entity_type
     *
     * @return $this
     */
    public function setEntityType($entity_type)
    {
        $this->entityType = $entity_type;
        return $this;
    }
    /**
     * Tests if $field is a field instance on $bundle.
     *
     * @param string $bundle
     * @param string $field
     *
     * @return bool
     */
    protected function isField($bundle, $field)
    {
        return array_key_exists($field, field_info_instances($this->getEntityType(), $bundle));
    }
}