Browscap\Data\DataCollection::addSourceFile PHP Метод

addSourceFile() публичный Метод

Load a JSON file, parse it's JSON and add it to our divisions list
public addSourceFile ( string $src ) : DataCollection
$src string Name of the file
Результат DataCollection
    public function addSourceFile($src)
    {
        $divisionData = $this->loadFile($src);
        if (!array_key_exists('division', $divisionData)) {
            throw new \UnexpectedValueException('required attibute "division" is missing in File ' . $src);
        }
        if (!array_key_exists('sortIndex', $divisionData)) {
            throw new \UnexpectedValueException('required attibute "sortIndex" is missing in File ' . $src);
        }
        if (!array_key_exists('lite', $divisionData)) {
            throw new \UnexpectedValueException('required attibute "lite" is missing in File ' . $src);
        }
        if (!array_key_exists('standard', $divisionData)) {
            throw new \UnexpectedValueException('required attibute "standard" is missing in File ' . $src);
        }
        if (isset($divisionData['versions']) && is_array($divisionData['versions'])) {
            $versions = $divisionData['versions'];
        } else {
            $versions = ['0.0'];
        }
        if (isset($divisionData['userAgents']) && is_array($divisionData['userAgents'])) {
            foreach ($divisionData['userAgents'] as $useragent) {
                if (!isset($useragent['userAgent'])) {
                    throw new \UnexpectedValueException('Name for Division is missing');
                }
                if (preg_match('/[\\[\\]]/', $useragent['userAgent'])) {
                    throw new \UnexpectedValueException('Name of Division "' . $useragent['userAgent'] . '" includes invalid characters');
                }
                if (false === strpos($useragent['userAgent'], '#') && in_array($useragent['userAgent'], $this->allDivision)) {
                    throw new \UnexpectedValueException('Division "' . $useragent['userAgent'] . '" is defined twice');
                }
                if ((false !== strpos($useragent['userAgent'], '#MAJORVER#') || false !== strpos($useragent['userAgent'], '#MINORVER#')) && ['0.0'] === $versions) {
                    throw new \UnexpectedValueException('Division "' . $useragent['userAgent'] . '" is defined with version placeholders, but no versions are set');
                }
                if (!isset($useragent['properties'])) {
                    throw new \UnexpectedValueException('the properties entry is missing for key "' . $useragent['userAgent'] . '"');
                }
                if (!is_array($useragent['properties'])) {
                    throw new \UnexpectedValueException('the properties entry has to be an array for key "' . $useragent['userAgent'] . '"');
                }
                if (!isset($useragent['properties']['Parent'])) {
                    throw new \UnexpectedValueException('the "Parent" property is missing for key "' . $useragent['userAgent'] . '"');
                }
                if ('DefaultProperties' !== $useragent['properties']['Parent']) {
                    throw new \UnexpectedValueException('the "Parent" property is not linked to the "DefaultProperties" for key "' . $useragent['userAgent'] . '"');
                }
                if (!isset($useragent['properties']['Comment'])) {
                    throw new \UnexpectedValueException('the "Comment" property is missing for key "' . $useragent['userAgent'] . '"');
                }
                if (isset($useragent['properties']['Version']) && ['0.0'] === $versions) {
                    throw new \UnexpectedValueException('the "Version" property is set for key "' . $useragent['userAgent'] . '", but no versions are defined');
                }
                if (!isset($useragent['properties']['Version']) && ['0.0'] !== $versions) {
                    throw new \UnexpectedValueException('the "Version" property is missing for key "' . $useragent['userAgent'] . '", but there are defined versions');
                }
                if (!isset($useragent['children'])) {
                    throw new \UnexpectedValueException('the children property is missing for key "' . $useragent['userAgent'] . '"');
                }
                if (!is_array($useragent['children'])) {
                    throw new \UnexpectedValueException('the children property has to be an array for key "' . $useragent['userAgent'] . '"');
                }
                if (isset($useragent['children']['match'])) {
                    throw new \UnexpectedValueException('the children property shall not have the "match" entry for key "' . $useragent['userAgent'] . '"');
                }
                $this->checkPlatformData($useragent['properties'], 'the properties array contains platform data for key "' . $useragent['userAgent'] . '", please use the "platform" keyword');
                $this->checkEngineData($useragent['properties'], 'the properties array contains engine data for key "' . $useragent['userAgent'] . '", please use the "engine" keyword');
                $this->checkDeviceData($useragent['properties'], 'the properties array contains device data for key "' . $useragent['userAgent'] . '", please use the "device" keyword');
                foreach ($useragent['children'] as $child) {
                    if (!is_array($child)) {
                        throw new \UnexpectedValueException('each entry of the children property has to be an array for key "' . $useragent['userAgent'] . '"');
                    }
                    if (!isset($child['match'])) {
                        throw new \UnexpectedValueException('each entry of the children property requires an "match" entry for key "' . $useragent['userAgent'] . '", missing for child data: ' . json_encode($child));
                    }
                    if (preg_match('/[\\[\\]]/', $child['match'])) {
                        throw new \UnexpectedValueException('key "' . $child['match'] . '" includes invalid characters');
                    }
                    if ((false !== strpos($child['match'], '#MAJORVER#') || false !== strpos($child['match'], '#MINORVER#')) && ['0.0'] === $versions) {
                        throw new \UnexpectedValueException('the key "' . $child['match'] . '" is defined with version placeholders, but no versions are set');
                    }
                    if (false !== strpos($child['match'], '#PLATFORM#') && !isset($child['platforms'])) {
                        throw new \UnexpectedValueException('the key "' . $child['match'] . '" is defined with platform placeholder, but no platforms are asigned');
                    }
                    if (isset($child['properties'])) {
                        if (!is_array($child['properties'])) {
                            throw new \UnexpectedValueException('the properties entry has to be an array for key "' . $child['match'] . '"');
                        }
                        if (isset($child['properties']['Parent'])) {
                            throw new \UnexpectedValueException('the Parent property must not set inside the children array for key "' . $child['match'] . '"');
                        }
                        if (isset($useragent['properties']['Version']) && isset($child['properties']['Version']) && $useragent['properties']['Version'] === $child['properties']['Version']) {
                            $this->logger->warning('the "Version" property is set for key "' . $child['match'] . '", but was already set for its parent "' . $useragent['userAgent'] . '"');
                        }
                        $this->checkPlatformData($child['properties'], 'the properties array contains platform data for key "' . $child['match'] . '", please use the "platforms" keyword');
                        $this->checkEngineData($child['properties'], 'the properties array contains engine data for key "' . $child['match'] . '", please use the "engine" keyword');
                        $this->checkDeviceData($child['properties'], 'the properties array contains device data for key "' . $child['match'] . '", please use the "device" keyword');
                    }
                }
                $this->allDivision[] = $useragent['userAgent'];
            }
            $userAgents = $divisionData['userAgents'];
        } else {
            $userAgents = [];
        }
        $this->divisions[] = new Division($divisionData['division'], (int) $divisionData['sortIndex'], $userAgents, (bool) $divisionData['lite'], (bool) $divisionData['standard'], $versions);
        $this->divisionsHaveBeenSorted = false;
        return $this;
    }

Usage Example

Пример #1
0
 /**
  * checks if a source file is added successful
  *
  * @group data
  * @group sourcetest
  */
 public function testAddSourceFileOkWithLiteAndVersions()
 {
     $this->object->addSourceFile(__DIR__ . '/../../fixtures/ua/test2.json');
     $divisions = $this->object->getDivisions();
     self::assertInternalType('array', $divisions);
     self::assertArrayHasKey(0, $divisions);
     self::assertInstanceOf('\\Browscap\\Data\\Division', $divisions[0]);
 }
All Usage Examples Of Browscap\Data\DataCollection::addSourceFile