Skip to content

Laravel model attribute and/or string token replacements

License

Notifications You must be signed in to change notification settings

MobileXCo/laravel-tokenizer

Repository files navigation

Laravel Tokenizer

Laravel tokenizer replaces predefined attributes or a string passed to a method.

Installation

Require the package using composer:

composer require mobilexco/tokenizer

Usage

Add HasTokens trait

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Mobilexco\Tokenizer\HasTokens;

class Post extends Model
{
    use HasTokens;
}

Optionally add model attributes to make token replacements

protected $tokenize = ['title', 'body'];

Call method to add token(s) and call method(s) to make replacements

$post = Post::create(['body' => ':greeting:, welcome to Tokenizer!']);

// Option 1, addition via model
$post->addToken(new Token(['key' => 'greeting', 'content' => 'Hello']));

// Option 2, addition via key and content
$post->addToken('greeting', 'Hello');

// Option 3, addition via array
$post->addToken(['key' => 'greeting', 'content' => 'Hello']);

// Make replacements to predefined attributes
// on the model `protected $tokenize = ['title', 'body'];`
$post->replaceTokens();

echo $post->body; // Hello, welcome to tokenizer!

// Make replacements to passed string
$post->replaceTokensInString('What is another greeting besides ":greeting:"'); // What is another greeting besides "Hello"

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

About

Laravel model attribute and/or string token replacements

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages