Skip to content

Commit

Permalink
Ported initial Reference implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Jan 19, 2024
1 parent 52f619d commit 84a0ca2
Show file tree
Hide file tree
Showing 30 changed files with 1,924 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# https://EditorConfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
block_comment_start = /*
block_comment = *
block_comment_end = */

[*.yml]
indent_size = 2

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.editorconfig export-ignore
.gitattributes export-ignore
.github/ export-ignore
.gitignore export-ignore
CHANGELOG.md export-ignore
ecs.php export-ignore
phpstan.neon export-ignore
phpunit.xml.dist export-ignore
docs/ export-ignore
tests/ export-ignore
stubs/ export-ignore
130 changes: 130 additions & 0 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow

name: "Integrate"

on:
push:
branches:
- "develop"
pull_request: null

env:
PHP_EXTENSIONS: "intl"

jobs:
file_consistency:
name: "1️⃣ File consistency"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
extensions: "${{ env.PHP_EXTENSIONS }}"
ini-values: "post_max_size=256M"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: Install Effigy
run: |
composer global config --no-plugins allow-plugins.phpstan/extension-installer true
composer global require decodelabs/effigy
- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"

- name: "Check file permissions"
run: |
composer global exec effigy check-executable-permissions
- name: "Check exported files"
run: |
composer global exec effigy check-git-exports
- name: "Find non-printable ASCII characters"
run: |
composer global exec effigy check-non-ascii
- name: "Check source code for syntax errors"
run: |
composer global exec effigy lint
static_analysis:
name: "3️⃣ Static Analysis"
needs:
- "file_consistency"
runs-on: "ubuntu-latest"
strategy:
matrix:
php-version:
- "8.1"
- "8.2"
- "8.3"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
ini-values: "post_max_size=256M"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: Install Effigy
run: |
composer global config --no-plugins allow-plugins.phpstan/extension-installer true
composer global require decodelabs/effigy
- name: "Validate Composer configuration"
run: "composer validate --strict"

- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"

- name: "Execute static analysis"
run: |
composer global exec effigy analyze -- --headless
coding_standards:
name: "4️⃣ Coding Standards"
needs:
- "file_consistency"
runs-on: "ubuntu-latest"
steps:
- name: "Set up PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8.1"
extensions: "${{ env.PHP_EXTENSIONS }}"
ini-values: "post_max_size=256M"

- name: "Checkout code"
uses: "actions/checkout@v3"

- name: "Check EditorConfig configuration"
run: "test -f .editorconfig"

- name: "Check adherence to EditorConfig"
uses: "greut/eclint-action@v0"

- name: Install Effigy
run: |
composer global config --no-plugins allow-plugins.phpstan/extension-installer true
composer global require decodelabs/effigy
- name: "Install dependencies"
uses: "ramsey/composer-install@v2"
with:
dependency-versions: "highest"

- name: "Check coding style"
run: |
composer global exec effigy format -- --headless
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/vendor
composer.phar
composer.lock
.DS_Store
Thumbs.db
/phpunit.xml
/.idea
/.env
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## v0.1.0 (2024-01-19)
* Ported initial Reference implementation
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Referential

[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/referential?style=flat)](https://packagist.org/packages/decodelabs/referential)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/referential.svg?style=flat)](https://packagist.org/packages/decodelabs/referential)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/referential.svg?style=flat)](https://packagist.org/packages/decodelabs/referential)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/referential/integrate.yml?branch=develop)](https://github.com/decodelabs/referential/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/referential?style=flat)](https://packagist.org/packages/decodelabs/referential)

### A framework for finding, parsing, inspecting and formatting reference IDs

Referential provides a generalist approach to handling ID references. It enables a consistent way of working with keys across multiple data sources and formats.

_Get news and updates on the [DecodeLabs blog](https://blog.decodelabs.com)._

---

## Installation

Install via Composer:

```bash
composer require decodelabs/referential
```

## Usage

Coming soon...

## Licensing

Referential is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "decodelabs/referential",
"description": "A framework for finding, parsing, inspecting and formatting reference IDs",
"type": "library",
"keywords": [ ],
"license": "MIT",
"authors": [ {
"name": "Tom Wright",
"email": "[email protected]"
} ],
"require": {
"php": "^8.1",
"decodelabs/exceptional": "^0.4.4"
},
"require-dev": {
"decodelabs/glitch": "^0.18.11",
"decodelabs/tagged": "^0.14.12",
"decodelabs/phpstan-decodelabs": "^0.6.7",
"decodelabs/guidance": "^0.1.8"
},
"suggest": {
"decodelabs/tagged": "For formatting as HTML"
},
"autoload": {
"psr-4": {
"DecodeLabs\\Referential\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-develop": "0.1.x-dev"
}
}
}
13 changes: 13 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// ecs.php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([__DIR__.'/src']);
$ecsConfig->sets([SetList::CLEAN_CODE, SetList::PSR_12]);
};
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
paths:
- src
level: max
77 changes: 77 additions & 0 deletions src/Reference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @package Referential
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Referential;

use Closure;
use DecodeLabs\Tagged\Markup;

interface Reference
{
/**
* @return static
*/
public static function instantiate(
?string $value
): Reference;

/**
* @return static|null
*/
public static function tryInstantiate(
?string $value
): ?Reference;

public static function isValid(
?string $value
): bool;

public static function isCanonical(
?string $value
): bool;

public static function canonicalize(
?string $value
): ?string;

public static function normalize(
?string $value
): ?string;

public static function format(
?string $value
): ?Markup;

public static function getCanonicalPattern(
bool $wrapped
): string;

public static function getCanonicalMaxLength(): int;

public static function getNormalPattern(
bool $wrapped
): string;

public static function getNormalMaxLength(): int;
public static function getExample(): static;
public static function getSanitizer(): Closure;
public static function isGeneric(): bool;

public function __construct(
?string $value
);

public function validate(): bool;
public function getRaw(): string;
public function getCanonical(): ?string;

public function __toString(): string;
public function getString(): string;
public function getHtml(): Markup;
}
58 changes: 58 additions & 0 deletions src/Reference/AtHandle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @package Referential
* @license http://opensource.org/licenses/MIT
*/

declare(strict_types=1);

namespace DecodeLabs\Referential\Reference;

use DecodeLabs\Referential\Reference;
use DecodeLabs\Referential\ReferenceTrait;
use DecodeLabs\Tagged as Html;
use DecodeLabs\Tagged\Markup;

class AtHandle implements Reference
{
use ReferenceTrait;

public const CANONICAL_PATTERN = '/^@([^@\s]+)$/';
public const CANONICAL_MAX_LENGTH = 256;
public const NORMAL_PATTERN = self::CANONICAL_PATTERN;
public const NORMAL_MAX_LENGTH = self::CANONICAL_MAX_LENGTH;
public const EXAMPLE = '@username';

/**
* Prepare canonical string
*/
protected function prepareCanonicalString(
string $value
): string {
return strtolower($value);
}

/**
* Convert canonical to formatted value
*/
protected function prepareNormalized(
string $value
): string {
return trim($value);
}

/**
* Convert canonical to formatted value
*/
protected function prepareHtml(
string $value
): Markup {
return Html::{'span.handle'}([
Html::{'span.grammar'}('@'),
$this->prepareNormalized($value)
], [
'title' => $this->canonical
]);
}
}
Loading

0 comments on commit 84a0ca2

Please sign in to comment.