Skip to content

Commit

Permalink
#47: Add twig extensions to allow building of URLs from osid_Id objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfranco committed Oct 1, 2024
1 parent 7fdc42e commit 024361b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
24 changes: 0 additions & 24 deletions application/views/helpers/FormatScheduleInfo.php

This file was deleted.

8 changes: 8 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ services:
arguments:
$idAuthorityToShorten: '%app.osid.id.authority_to_shorten%'

App\Twig\OsidIdExtension:
tags:
- { name: 'twig.extension' }

App\Twig\NoWrapParentheticals:
tags:
- { name: 'twig.extension' }

# Service aliases.
osid.runtime:
class: App\Service\Osid\Runtime
Expand Down
21 changes: 21 additions & 0 deletions src/Twig/NoWrapParentheticals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class NoWrapParentheticals extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('noWrapParentheticals', [$this, 'addNoWrapHtml']),
];
}

public function addNoWrapHtml(string $input): string
{
return preg_replace('/\([^\)]+\)/', '<span style="white-space: nowrap">$0</span>', $input);
}
}
40 changes: 40 additions & 0 deletions src/Twig/OsidIdExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

// src/Twig/AppExtension.php
namespace App\Twig;

use App\Service\Osid\IdMap;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class OsidIdExtension extends AbstractExtension
{

/**
* @var \App\Service\Osid\IdMap
* The IdMap service.
*/
private $idMap;

/**
* Create a new instance of this service.
*
* @param \App\Service\Osid\IdMap
* The IdMap service.
*/
public function __construct(IdMap $idMap) {
$this->idMap = $idMap;
}

public function getFunctions(): array
{
return [
new TwigFunction('osidIdToString', [$this, 'osidIdToString']),
];
}

public function osidIdToString(\osid_id_Id $id): string
{
return $this->idMap->toString($id);
}
}

0 comments on commit 024361b

Please sign in to comment.