Browscap\Data\Factory\DeviceFactory::build PHP Method

build() public method

Load a engines.json file and parse it into the platforms data array
public build ( array $deviceData, array $json, string $deviceName ) : Device
$deviceData array
$json array
$deviceName string
return Browscap\Data\Device
    public function build(array $deviceData, array $json, $deviceName)
    {
        if (!isset($deviceData['properties'])) {
            $deviceData['properties'] = [];
        }
        if (!array_key_exists('standard', $deviceData)) {
            throw new \UnexpectedValueException('the value for "standard" key is missing for device "' . $deviceName . '"');
        }
        if (array_key_exists('inherits', $deviceData)) {
            $parentName = $deviceData['inherits'];
            if (!isset($json['devices'][$parentName])) {
                throw new \UnexpectedValueException('parent Device "' . $parentName . '" is missing for device "' . $deviceName . '"');
            }
            $parentEngine = $this->build($json['devices'][$parentName], $json, $parentName);
            $parentEngineData = $parentEngine->getProperties();
            $inheritedPlatformProperties = $deviceData['properties'];
            foreach ($inheritedPlatformProperties as $name => $value) {
                if (isset($parentEngineData[$name]) && $parentEngineData[$name] === $value) {
                    throw new \UnexpectedValueException('the value for property "' . $name . '" has the same value in the keys "' . $deviceName . '" and its parent "' . $deviceData['inherits'] . '"');
                }
            }
            $deviceData['properties'] = array_merge($parentEngineData, $inheritedPlatformProperties);
            if (!$parentEngine->isStandard()) {
                $deviceData['standard'] = false;
            }
        }
        return new Device($deviceData['properties'], $deviceData['standard']);
    }

Usage Example

Example #1
0
 /**
  * Load a devices.json file and parse it into the platforms data array
  *
  * @param string $src Name of the file
  *
  * @throws \RuntimeException             if the file does not exist or has invalid JSON
  * @throws \UnexpectedValueException     if the properties and the inherits kyewords are missing
  * @return \Browscap\Data\DataCollection
  */
 public function addDevicesFile($src)
 {
     $json = $this->loadFile($src);
     $deviceFactory = new Factory\DeviceFactory();
     foreach ($json['devices'] as $deviceName => $deviceData) {
         if (!isset($deviceData['properties']) && !isset($deviceData['inherits'])) {
             throw new \UnexpectedValueException('required attibute "properties" is missing');
         }
         $this->devices[$deviceName] = $deviceFactory->build($deviceData, $json, $deviceName);
     }
     $this->divisionsHaveBeenSorted = false;
     return $this;
 }
DeviceFactory