1
0
Fork
You've already forked php-gesetze
0
Linking german legal norms with gesetze-im-internet.de, dejure.org, buzer.de & lexparency.de - dependency-free & GDPR-friendly. https://packagist.org/packages/s1syphos/php-gesetze
  • PHP 100%
Martin Folkers fd8c56fe6b
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Bump version
2022年11月27日 12:18:31 +01:00
data Update data format 2022年11月27日 12:16:40 +01:00
src Update data format 2022年11月27日 12:16:40 +01:00
tests Update data format 2022年11月27日 12:16:40 +01:00
.editorconfig Add missing information 2022年04月11日 22:27:08 +02:00
.gitattributes Initial commit 2021年10月27日 16:07:27 +02:00
.gitignore Update '.gitignore' 2022年06月19日 12:11:07 +02:00
.php_cs Initial commit 2021年10月27日 16:07:27 +02:00
.woodpecker.yml Simplify 'woodpecker' script 2022年04月11日 22:28:52 +02:00
composer.json Bump version 2022年11月27日 12:18:31 +01:00
LICENSE Initial commit 2021年10月27日 16:07:27 +02:00
phpunit.xml Initial commit 2021年10月27日 16:07:27 +02:00
psalm.xml Add vimeo/psalm & fix initial errors & warnings 2022年06月20日 19:19:39 +02:00
README.md Fix URL of license badge in 'README' 2022年11月27日 11:48:45 +01:00

php-gesetze

License Packagist Build

Linking german legal norms, dependency-free & GDPR-friendly. php-gesetze automatically transforms legal references into a tags - batteries included.

There's also a Python port of this library, called py-gesetze.

For API documentation (powered by phpDocumentor), see here.

Installation

It's available for Composer:

composer require s1syphos/php-gesetze

Getting started

Upon invoking the main class, you may specify your preferred provider (or 'driver'), like this:

$object = new \S1SYPHOS\Gesetze\Gesetz('dejure');

It's also possible to specify selected of two or more drivers as array:

$object = new \S1SYPHOS\Gesetze\Gesetz(['dejure', 'buzer']);

Note: This defaults to all available drivers, which is a good overall choice, simply because of the vast array of supported laws. Possible values are gesetze, 'dejure', 'buzer' and 'lexparency'.

Out of the box, php-gesetze cycles through all known drivers until a match is found.

Usage

From there, the following functions are available:

analyze(string $string): array

Analyzes a single legal norm:

$result = \S1SYPHOS\Gesetze\Gesetz::analyze('Art. 1 II GG');
var_dump($result);
# array(6) {
# ["norm"]=>
# string(1) "1"
# ["absatz"]=>
# string(2) "II"
# ["satz"]=>
# string(0) ""
# ["nr"]=>
# string(0) ""
# ["lit"]=>
# string(0) ""
# ["gesetz"]=>
# string(2) "GG"
# }

validate(string $string): bool

Validates a single legal norm (across all selected providers:

$obj = new \S1SYPHOS\Gesetze\Gesetz();
var_dump($obj->validate('§ 433 II BGB'));
# bool(true)

foreach ($obj->extract('While § 433 II BGB exists, Art. 4c GG does not!') as $match) {
 var_dump($obj->validate($match);
}
# bool(true)
# bool(false)

Note: In the context of this library, being valid means linkable by at least one provider, as in to be found in their database.

roman2arabic(string $string): string

Converts roman numerals to arabic numerals:

echo \S1SYPHOS\Gesetze\Gesetz::roman2arabic('IX');
# 9

extract(string $string): array

Extracts legal norms as array of strings:

$obj = new \S1SYPHOS\Gesetze\Gesetz();
$result = $obj->extract('This string contains Art. 12 Abs. 1 GG and Art. 2 Abs. 2 DSGVO - for educational purposes only.')
var_dump($result);
# array(2) {
# [0]=>
# string(17) "Art. 12 Abs. 1 GG"
# [1]=>
# string(19) "Art. 2 Abs. 2 DSGVO"
# }

gesetzify(string $string, callable $callback): string

Transforms legal references into HTML link tags:

$obj = new \S1SYPHOS\Gesetze\Gesetz();
echo $obj->gesetzify('This is a simple text, featuring § 1 I Nr. 1 BGB as well as Art. 4c GG');
# This is a simple text, featuring <a href="https://www.gesetze-im-internet.de/bgb/__1.html" title="§ 1 Beginn der Rechtsfähigkeit">§ 1 I Nr. 1 BGB</a> as well as Art. 4c GG

Note: For more flexibility, you may use your own callback method as second parameter of gesetzify. Callbacks are being passed arrays representing matched legal norms. This way, you could highlight them using <strong> tags instead of converting them into a tags. Default: (private) method linkify

Example

include_once 'vendor/autoload.php';
# Insert test string
$text = '<div>';
$text .= 'This is a <b>simple</b> HTML text.';
$text .= 'It contains legal norms, like Art. 12 I GG.';
$text .= '.. or § 433 II nr. 2 BGB!';
$text .= '</div>';
# Initialize object
$obj = new \S1SYPHOS\Gesetze\Gesetz();
# Transform text
echo $obj->gesetzify($text);
# <div>This is a <b>simple</b> HTML text.
# It contains legal norms, like <a href="https://www.gesetze-im-internet.de/gg/art_12.html" target="_blank">Art. 12 I GG</a>.
# .. or <a href="https://www.gesetze-im-internet.de/bgb/__433.html" target="_blank">§ 433 II nr. 2 BGB</a>!
# </div>

Note: Caching the result (to avoid repeated lookups & save resources) is beyond the scope of this library and therefore totally up to you!

Configuration

There are several settings you may use in order to change the behavior of the library:

$object->drivers (array)

Associative array, holding all available drivers (already initialized), where the corresponding keys are 'gesetze', 'dejure', 'buzer' & 'lexparency' (default).

$object->pattern (string)

The regex responsible for detecting legal norms. For reference, it amounts to this:

'/(?:§+|Art\.?|Artikel)\s*(\d+(?:\w\b)?)\s*(?:(?:Abs(?:atz|\.)\s*)?((?:\d+|[XIV]+)(?:\w\b)?))?\s*(?:(?:S\.|Satz)\s*(\d+))?\s*(?:(?:Nr\.|Nummer)\s*(\d+(?:\w\b)?))?\s*(?:(?:lit\.|litera|Buchst\.|Buchstabe)\s*([a-z]?))?.{0,10}?(\b[A-Z][A-Za-z]*[A-Z](?:(?:\s|\b)[XIV]+)?)/'

Note: Well, more or less - for the latest revision, please refer to src/Traits/Regex.php!

$object->attributes (array)

Other HTML attributes to be applied globally:

$object->attributes = [
 'attr1' => 'some-value',
 'attr2' => 'other-value',
];
# .. would generate links like this:

<a href="https://example.com/some-law" attr1="some-value" attr2="other-value">§ 1 SomeLaw</a>

$object->title (false|string)

Controls title attribute:

Option Description
false No title attribute (default)
'light' abbreviated law (eg 'GG')
'normal' complete law (eg 'Grundgesetz')
'full' official heading (eg 'Artikel 12')

Credits

The regular expression used in this library is based on the jura_regex regex package by Philipp Kiersch (originally written in Python).

Special Thanks

I'd like to thank everybody that's making free & open source software - you people are awesome. Also I'm always thankful for feedback and bug reports :)