Skip to content

Commit

Permalink
Updated deps and fixed code accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 22, 2020
1 parent 4ad7302 commit 91ec62d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
push: ~
pull_request: ~
schedule:
- cron: 5 8 * * 3
- cron: 5 8 * * 2
jobs:
checks:
name: 'PHP ${{ matrix.php-versions }} with composer args: ${{ matrix.composer-args }}'
Expand Down
26 changes: 10 additions & 16 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "setono/tag-bag-gtag",
"type": "library",
"description": "PHP tag and renderer for the tag bag library",
"description": "Gtag extension for the tag bag library",
"license": "MIT",
"authors": [
{
Expand All @@ -12,20 +12,14 @@
"require": {
"php": "^7.3",
"ext-json": "*",
"setono/php-templates": "^1.0@dev",
"setono/tag-bag": "^1.0@dev",
"setono/tag-bag-php": "^1.0@dev",
"setono/php-templates": "^1.0",
"setono/tag-bag": "^1.0",
"setono/tag-bag-php-templates": "^1.0",
"thecodingmachine/safe": "^1.0"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.5.1",
"korbeil/phpstan-generic-rules": "^0.2.4",
"phpstan/extension-installer": "^1.0.4",
"phpstan/phpstan": "^0.12.23",
"phpstan/phpstan-strict-rules": "^0.12.2",
"phpunit/phpunit": "^9.1",
"sylius-labs/coding-standard": "^3.1.2",
"thecodingmachine/phpstan-safe-rule": "^1.0"
"setono/code-quality-pack": "^1.0"
},
"config": {
"sort-packages": true
Expand All @@ -38,7 +32,10 @@
"autoload": {
"psr-4": {
"Setono\\TagBag\\": "src/"
}
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
Expand All @@ -50,9 +47,6 @@
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon -l max src",
"check-style": "vendor/bin/ecs check --ansi src/ tests/",
"fix-style": "vendor/bin/ecs check --fix --ansi src/ tests/",
"phpunit": "vendor/bin/phpunit",
"test": [
"@phpunit"
]
"phpunit": "vendor/bin/phpunit"
}
}
16 changes: 13 additions & 3 deletions src/Tag/Gtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

namespace Setono\TagBag\Tag;

abstract class Gtag extends PhpTag implements GtagInterface
abstract class Gtag extends PhpTemplatesTag implements GtagInterface
{
/** @var array */
protected $parameters = [];
protected $parameters;

public function __construct(string $template, array $parameters = [])
{
parent::__construct($template);

$this->parameters = $parameters;
}

public function getParameters(): array
{
Expand Down Expand Up @@ -39,5 +46,8 @@ public function getContext(): array
/**
* Returns the properties that will be returned as the context for the template
*/
abstract protected function getPropertiesForContext(): array;
protected function getPropertiesForContext(): array
{
return ['parameters'];
}
}
10 changes: 3 additions & 7 deletions src/Tag/GtagConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ class GtagConfig extends Gtag implements GtagConfigInterface
/** @var string */
protected $target;

public function __construct(string $key, string $target, array $parameters = [])
public function __construct(string $target, array $parameters = [])
{
parent::__construct($key, '@SetonoTagBagGtag/config');
parent::__construct('@SetonoTagBagGtag/config', $parameters);

$this->target = $target;

foreach ($parameters as $k => $v) {
$this->addParameter($k, $v);
}
}

public function getTarget(): string
Expand All @@ -27,6 +23,6 @@ public function getTarget(): string

protected function getPropertiesForContext(): array
{
return ['target', 'parameters'];
return array_merge(['target'], parent::getPropertiesForContext());
}
}
10 changes: 3 additions & 7 deletions src/Tag/GtagEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ class GtagEvent extends Gtag implements GtagEventInterface
/** @var string */
protected $event;

public function __construct(string $key, string $event, array $parameters = [])
public function __construct(string $event, array $parameters = [])
{
parent::__construct($key, '@SetonoTagBagGtag/event');
parent::__construct('@SetonoTagBagGtag/event', $parameters);

$this->event = $event;

foreach ($parameters as $k => $v) {
$this->addParameter($k, $v);
}
}

public function getEvent(): string
Expand All @@ -27,6 +23,6 @@ public function getEvent(): string

protected function getPropertiesForContext(): array
{
return ['event', 'parameters'];
return array_merge(['event'], parent::getPropertiesForContext());
}
}
5 changes: 4 additions & 1 deletion src/Tag/GtagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Setono\TagBag\Tag;

interface GtagInterface extends PhpTagInterface
interface GtagInterface extends PhpTemplatesTagInterface
{
public function getParameters(): array;

/**
* Allows the user to add parameters to the existing parameters.
* NOTICE that if the key exists, it will be overwritten
*
* @param mixed $value
*/
public function addParameter(string $key, $value): self;
Expand Down
13 changes: 2 additions & 11 deletions src/Tag/GtagSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@

class GtagSet extends Gtag implements GtagSetInterface
{
public function __construct(string $key, array $parameters)
public function __construct(array $parameters)
{
parent::__construct($key, '@SetonoTagBagGtag/set');

foreach ($parameters as $k => $v) {
$this->addParameter($k, $v);
}
}

protected function getPropertiesForContext(): array
{
return ['parameters'];
parent::__construct('@SetonoTagBagGtag/set', $parameters);
}
}
21 changes: 21 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Setono\TagBag;

use const JSON_INVALID_UTF8_IGNORE;
use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_PRETTY_PRINT;
use const JSON_THROW_ON_ERROR;
use const JSON_UNESCAPED_SLASHES;

if (!function_exists('Setono\TagBag\encode')) {
/**
* @param mixed $val
*/
function encode($val): string
{
return json_encode($val, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION | JSON_INVALID_UTF8_IGNORE);
}
}
2 changes: 1 addition & 1 deletion src/templates/SetonoTagBagGtag/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
<?php if (count($parameters) > 0): ?>
gtag('config', '<?=$target?>', <?=json_encode($parameters, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_PRESERVE_ZERO_FRACTION | \JSON_INVALID_UTF8_IGNORE)?>);
gtag('config', '<?=$target?>', <?=\Setono\TagBag\encode($parameters)?>);
<?php else: ?>
gtag('config', '<?=$target?>');
<?php endif; ?>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/SetonoTagBagGtag/event.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
<?php if (count($parameters) > 0): ?>
gtag('event', '<?=$event?>', <?=json_encode($parameters, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_PRESERVE_ZERO_FRACTION | \JSON_INVALID_UTF8_IGNORE)?>);
gtag('event', '<?=$event?>', <?=\Setono\TagBag\encode($parameters)?>);
<?php else: ?>
gtag('event', '<?=$event?>');
<?php endif; ?>
Expand Down
2 changes: 1 addition & 1 deletion src/templates/SetonoTagBagGtag/set.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<script>
gtag('set', <?=json_encode($parameters, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_PRESERVE_ZERO_FRACTION | \JSON_INVALID_UTF8_IGNORE)?>);
gtag('set', <?=\Setono\TagBag\encode($parameters)?>);
</script>
25 changes: 12 additions & 13 deletions tests/Tag/GtagConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Setono\PhpTemplates\Engine\Engine;
use Setono\TagBag\Renderer\PhpRenderer;
use Setono\TagBag\Renderer\PhpTemplatesRenderer;

final class GtagConfigTest extends TestCase
{
Expand All @@ -15,8 +15,9 @@ final class GtagConfigTest extends TestCase
*/
public function it_creates(): void
{
$tag = new GtagConfig('key', 'target');
$tag = new GtagConfig('target');
$this->assertInstanceOf(TagInterface::class, $tag);
$this->assertInstanceOf(PhpTemplatesTagInterface::class, $tag);
$this->assertInstanceOf(GtagInterface::class, $tag);
$this->assertInstanceOf(GtagConfigInterface::class, $tag);
}
Expand All @@ -26,15 +27,13 @@ public function it_creates(): void
*/
public function it_has_default_values(): void
{
$tag = new GtagConfig('key', 'target');
$tag = new GtagConfig('target');

$this->assertSame('key', $tag->getKey());
$this->assertSame('@SetonoTagBagGtag/config', $tag->getTemplate());
$this->assertNull($tag->getSection());
$this->assertSame(TagInterface::SECTION_BODY_END, $tag->getSection());
$this->assertSame(0, $tag->getPriority());
$this->assertIsArray($tag->getDependents());
$this->assertCount(0, $tag->getDependents());
$this->assertTrue($tag->willReplace());
$this->assertIsArray($tag->getDependencies());
$this->assertCount(0, $tag->getDependencies());
$this->assertIsArray($tag->getContext());
$this->assertCount(2, $tag->getContext()); // the target and parameter keys
$this->assertIsArray($tag->getParameters());
Expand All @@ -47,7 +46,7 @@ public function it_has_default_values(): void
*/
public function it_adds_parameters(): void
{
$tag = new GtagConfig('key', 'target', [
$tag = new GtagConfig('target', [
'param1' => 'value1',
]);
$tag->addParameter('param2', 'value2');
Expand All @@ -59,9 +58,9 @@ public function it_adds_parameters(): void
*/
public function it_renders(): void
{
$tag = new GtagConfig('key', 'target');
$tag = new GtagConfig('target');

$renderer = new PhpRenderer(new Engine([__DIR__ . '/../../src/templates']));
$renderer = new PhpTemplatesRenderer(new Engine([__DIR__ . '/../../src/templates']));

$expected = <<<SCRIPT
<script>
Expand All @@ -79,11 +78,11 @@ public function it_renders(): void
*/
public function it_renders_with_parameters(): void
{
$tag = new GtagConfig('key', 'target', [
$tag = new GtagConfig('target', [
'param1' => 'value1',
]);

$renderer = new PhpRenderer(new Engine([__DIR__ . '/../../src/templates']));
$renderer = new PhpTemplatesRenderer(new Engine([__DIR__ . '/../../src/templates']));

$expected = <<<SCRIPT
<script>
Expand Down
25 changes: 12 additions & 13 deletions tests/Tag/GtagEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPUnit\Framework\TestCase;
use Setono\PhpTemplates\Engine\Engine;
use Setono\TagBag\Renderer\PhpRenderer;
use Setono\TagBag\Renderer\PhpTemplatesRenderer;

final class GtagEventTest extends TestCase
{
Expand All @@ -15,8 +15,9 @@ final class GtagEventTest extends TestCase
*/
public function it_creates(): void
{
$tag = new GtagEvent('key', 'event');
$tag = new GtagEvent('event');
$this->assertInstanceOf(TagInterface::class, $tag);
$this->assertInstanceOf(PhpTemplatesTagInterface::class, $tag);
$this->assertInstanceOf(GtagInterface::class, $tag);
$this->assertInstanceOf(GtagEventInterface::class, $tag);
}
Expand All @@ -26,15 +27,13 @@ public function it_creates(): void
*/
public function it_has_default_values(): void
{
$tag = new GtagEvent('key', 'event');
$tag = new GtagEvent('event');

$this->assertSame('key', $tag->getKey());
$this->assertSame('@SetonoTagBagGtag/event', $tag->getTemplate());
$this->assertNull($tag->getSection());
$this->assertSame(TagInterface::SECTION_BODY_END, $tag->getSection());
$this->assertSame(0, $tag->getPriority());
$this->assertIsArray($tag->getDependents());
$this->assertCount(0, $tag->getDependents());
$this->assertTrue($tag->willReplace());
$this->assertIsArray($tag->getDependencies());
$this->assertCount(0, $tag->getDependencies());
$this->assertIsArray($tag->getContext());
$this->assertCount(2, $tag->getContext()); // the event and parameter keys
$this->assertIsArray($tag->getParameters());
Expand All @@ -47,7 +46,7 @@ public function it_has_default_values(): void
*/
public function it_adds_parameters(): void
{
$tag = new GtagEvent('key', 'event', [
$tag = new GtagEvent('event', [
'param1' => 'value1',
]);
$tag->addParameter('param2', 'value2');
Expand All @@ -59,9 +58,9 @@ public function it_adds_parameters(): void
*/
public function it_renders(): void
{
$tag = new GtagEvent('key', 'event');
$tag = new GtagEvent('event');

$renderer = new PhpRenderer(new Engine([__DIR__ . '/../../src/templates']));
$renderer = new PhpTemplatesRenderer(new Engine([__DIR__ . '/../../src/templates']));

$expected = <<<SCRIPT
<script>
Expand All @@ -79,11 +78,11 @@ public function it_renders(): void
*/
public function it_renders_with_parameters(): void
{
$tag = new GtagEvent('key', 'event', [
$tag = new GtagEvent('event', [
'param1' => 'value1',
]);

$renderer = new PhpRenderer(new Engine([__DIR__ . '/../../src/templates']));
$renderer = new PhpTemplatesRenderer(new Engine([__DIR__ . '/../../src/templates']));

$expected = <<<SCRIPT
<script>
Expand Down
Loading

0 comments on commit 91ec62d

Please sign in to comment.