Skip to content

Commit

Permalink
Change process method to accept data
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdheiden committed Dec 11, 2019
1 parent 990bce5 commit d3f5d92
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 58 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2019-12-11

### Added
- Add this changelog to this package.

### Changed
- Change `setOpenDelimiter` and `setCloseDelimiter` to be fluent.
- Move the `data` parameter to the `process` method so multiple templates can be rendered with the same `Replacer`
instance.

## [1.0.0] - 2018-02-19

### Added
- Initial release
25 changes: 10 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Replacer

Dicms package to easily replace values in text for templating purposes, like email messages from the database.
This package allows you to easily replace values in text for templating purposes, like email messages from the database.

## Requirements

This package requires PHP 7.0+ but there's a [`php56`](https://github.com/vdhicts/dicms-replacer/tree/php56) branch available for users which need PHP 5.6 compatibility. The `php56` branch won't receive any new features.
This package requires PHP 7.0+.

## Installation

Provide code examples and explanations of how to get the project:
Install the package with composer:

`composer require vdhicts/replacer`

Expand All @@ -20,14 +20,8 @@ $data = [
'username' => 'World'
];

$replacer = new Replacer($data);
$replacer->process($text);
```

The data can also be provided after initialising:

```php
$replacer->setData($data);
$replacer = new Replacer();
$replacer->process($text, $data);
```

### Custom delimiters
Expand All @@ -40,15 +34,16 @@ $data = [
'username' => 'World'
];

$replacer = new Replacer($data, '%', '#');
$replacer->process($text);
$replacer = new Replacer('%', '#');
$replacer->process($text, $data);
```

The delimiters can also be provided after initialising:

```php
$replacer->setOpenDelimiter('%');
$replacer->setCloseDelimiter('#');
$replacer
->setOpenDelimiter('%')
->setCloseDelimiter('#');
```

## Tests
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vdhicts/replacer",
"description": "Dicms package to easily replace values in text for templating purposes.",
"description": "Package to easily replace values in text for templating purposes.",
"license": "MIT",
"authors": [
{
Expand Down
63 changes: 25 additions & 38 deletions src/Replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@

class Replacer
{

/**
* Holds the tokens and values which should be replaced.
* @var array
*/
private $data = [];

/**
* Holds the open delimiter for the tokens.
* @var string
Expand All @@ -25,35 +18,15 @@ class Replacer

/**
* Replacer constructor.
* @param array $data
* @param string $openDelimiter
* @param string $closeDelimiter
*/
public function __construct(array $data = [], string $openDelimiter = '[', string $closeDelimiter = ']')
public function __construct(string $openDelimiter = '[', string $closeDelimiter = ']')
{
$this->setData($data);
$this->setOpenDelimiter($openDelimiter);
$this->setCloseDelimiter($closeDelimiter);
}

/**
* Returns the tokens and values which should be replaced.
* @return array
*/
private function getData(): array
{
return $this->data;
}

/**
* Stores the tokens and values which should be replaced.
* @param array $data
*/
public function setData(array $data = [])
{
$this->data = $data;
}

/**
* Holds the open delimiter for the tokens.
* @return string
Expand All @@ -66,10 +39,13 @@ private function getOpenDelimiter(): string
/**
* Stores the open delimiter for the tokens.
* @param string $openDelimiter
* @return Replacer
*/
public function setOpenDelimiter(string $openDelimiter = '[')
public function setOpenDelimiter(string $openDelimiter = '['): self
{
$this->openDelimiter = $openDelimiter;

return $this;
}

/**
Expand All @@ -84,10 +60,13 @@ private function getCloseDelimiter(): string
/**
* Stores the close delimiter for the tokens.
* @param string $closeDelimiter
* @return Replacer
*/
public function setCloseDelimiter(string $closeDelimiter = ']')
public function setCloseDelimiter(string $closeDelimiter = ']'): self
{
$this->closeDelimiter = $closeDelimiter;

return $this;
}

/**
Expand All @@ -107,39 +86,47 @@ private function formatToken(string $token): string

/**
* Returns the tokens.
* @param array $data
* @return array
*/
private function getTokens(): array
private function getTokens(array $data = []): array
{
return array_map(
function ($token) {
return $this->formatToken($token);
},
array_keys($this->getData())
array_keys($data)
);
}

/**
* Returns the values of the tokens.
* @param array $data
* @return array
*/
private function getTokenValues(): array
private function getTokenValues(array $data = []): array
{
return array_values($this->getData());
return array_values($data);
}

/**
* Replaces the token with its values in the provided text.
* @param string $text
* @param array $data
* @return string
*/
public function process(string $text = ''): string
public function process(string $text = '', array $data = []): string
{
$tokens = $this->getTokens();
if (count($tokens) === 0) {
// Determine the tokens which should be replaced
$replaceTokens = $this->getTokens($data);
if (count($replaceTokens) === 0) {
return $text;
}

return str_replace($tokens, $this->getTokenValues(), $text);
// Determine the values with which the tokens should be replaced
$replaceValues = $this->getTokenValues($data);

// Replace the tokens with the values
return str_replace($replaceTokens, $replaceValues, $text);
}
}
8 changes: 4 additions & 4 deletions tests/unit/ReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public function testDefaultValues()

public function testWithValues()
{
$replacer = new Replacer();
$tokens = [
'test1' => 1,
'test2' => 2
];
$replacer = new Replacer($tokens);

$this->assertSame('1 en 2', $replacer->process('[TEST1] en [TEST2]'));
$this->assertSame('1 en 2', $replacer->process('[TEST1] en [TEST2]', $tokens));
}

public function testWithValuesWithCustomDelimiters()
{
$replacer = new Replacer('%', '#');
$tokens = [
'test1' => 1,
'test2' => 2
];
$replacer = new Replacer($tokens, '%', '#');

$this->assertSame('1 en 2', $replacer->process('%TEST1# en %TEST2#'));
$this->assertSame('1 en 2', $replacer->process('%TEST1# en %TEST2#', $tokens));
}
}

0 comments on commit d3f5d92

Please sign in to comment.