Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: handling null values in source yaml file #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 3.4.3
- Bugfix: handling null values in source yaml file

## 3.4.2
- Update uap-core

Expand Down
18 changes: 9 additions & 9 deletions src/Util/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public function generateArray(array $array): string
return static function(string $source, array $element) use ($indentation, $multi, $createReducer) {
[$key, $value] = $element;

if (is_scalar($value)) {
if ($multi) {
$source .= self::indent($indentation);
}
$source .= self::generateKey($key) . var_export($value, true);
if ($multi) {
$source .= self::indent($indentation);
}

$source .= self::generateKey($key);

if (is_scalar($value) || is_null($value)) {
$source .= var_export($value, true);
if ($multi) {
$source .= ",\n";
}

return $source;
}

if ($multi) {
$source .= self::indent($indentation);
}
$source .= self::generateKey($key) . "[";
$source .= "[";
$nextMulti = count($value) > 1;
if ($nextMulti) {
$source .= "\n";
Expand Down
53 changes: 53 additions & 0 deletions tests/Util/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace UAParser\Test\Util;

use Symfony\Component\Yaml\Yaml;
use UAParser\Test\AbstractTestCase;
use UAParser\Util\CodeGenerator;

class CodeGeneratorTest extends AbstractTestCase
{
public function testCodeGeneratorScalarAndNull(): void
{
$cg = new CodeGenerator();

$yamlString = <<<YML
user_agent_parsers:
- regex: 'UaRegEx'

os_parsers:
- regex: '(Windows 10)'
os_replacement: 'Windows'
os_v1_replacement: '10'

device_parsers:
- regex: 'SomeDevice/'
brand_replacement: 'Some'
device_replacement: 'Device'
model_replacement: null
YML;

$expected = [
'user_agent_parsers' => [[
'regex' => 'UaRegEx',
]],
'os_parsers' => [[
'regex' => '(Windows 10)',
'os_replacement' => 'Windows',
'os_v1_replacement' => '10',
]],
'device_parsers' => [[
'regex' => 'SomeDevice/',
'brand_replacement' => 'Some',
'device_replacement' => 'Device',
'model_replacement' => null,
]]
];

$rawPhp = $cg->generateArray(Yaml::parse($yamlString));
$evaluated = eval('return ' . $rawPhp);

$this->assertSame($expected, $evaluated);
}
}