connorvg/php-wtf

A PHP implementation of my WholeTextFunctions library

Installs: 33

Dependents: 0

Stars: 5

Watchers: 1

Language: PHP

1.1 2014-03-19 16:08 UTC

README

WTF is a PHP library for string-based commands. This is especially useful in situations where you expect the user to execute the command via a text based medium (E.G: Slack).

Licensing

Copyright (c) 2014, Connor S. Parks and Alexandru Maftei (extracted from Lua portion)
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of WholeTextFunctions (WTF) nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Connor S. Parks or Alexandru Maftei BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Syntax

Understanding the Syntax

An example command would be as so: commandName argName:argType [argName:argType]

Let's break this down.

  • commandName

    This is the name of the command and subsequently what is used to define which command to execute. For example, if a command with no arguments had a name of test you could simply execute said command via WholeTextFunctions::Execute(test).

    Note: During execution, commandName is case-insensitive!

  • argName:argType

    This is an argument for a command, this has two parts to it.

    The first part, described as argName, is exactly what you would expect (the name of the argument). This is useful because if the user tries to execute a command with incorrect syntax we can simply return the syntax and the argument name that the user messed up on.

    The second part, described as argType, is the data type of the argument (please refer to Argument Types for all available data types). This is used to check the syntax of a command and for ensuring the correct data type gets to the the final function call.

  • [argName:argType]

    This is an optional argument for a command (please refer to argName:argType for more information regarding arguments). The only difference between this and a regular argument is that this is optional therefore non-required for the user to pass syntax.

    If this argument is ignored by the user, a default value of null will be passed to the command's function.

Adding a Command

Adding a command is extremely simple, as-long as you understand the syntax (please refer to Understanding the Syntax to learn about the syntax).

All you need to do is:

  • Think of a command name, let's use print.
  • Think of which arguments we may require (plus if they should be optional), let's use [indentLevel:number] message:remaining.
  • Think of a function of which we want to call on execution of the command, let's use:
function($wtf, $executer, $args)
{
    echo str_repeat('\t', $args[0]) . $args[1];
}

To add this command, all we need to do is:

WTF::addCommand('print [indentLevel:number] message:remaining', function($wtf, $executer, $args)
{
    echo str_repeat('\t', $args[0]) . $args[1];
});

Note: The first variable passed to the command's function is always the instance of WTF used to execute said function!

Adding an Alias

Adding an alias is even more simple than adding a command.

All you need to do is:

  • Think of an alias name, let's use p.
  • Think of the command OR alias you wish to create an alias for, let's use print (see Adding a Command for more information on this command).

To add this alias, all we need to do is:

WholeTextFunctions::addAlias('print', 'p')

Executing a Command

The method of which you decide to execute a command will be entirely based on your implementation of WTF. The only thing all implementations will have in common is the base command execution.

To execute a command, all we need to do is (where command is your command string. For example: print Hello!):

WholeTextFunctions::Execute(command)

Argument Types

Currently, WTF only has support for a few data types. This list will grow once I create an optional 'less-light' version of WTF's parsing system.

The current data types are as follows:

  • source: The whole command string (without the commandName included) as a string.
  • remaining: The rest of the command string left (source without any used arguments included) as a string.
  • string: A simple string.
  • number: Any number.
  • bool: Simply put, booleans (true or false).

Composer setup

In the require key of composer.json file add the following

"connorvg/php-wtf": "dev-master"

Run the Composer update comand

$ composer update

Laravel

If you're using laravel, add this service provider:

'ConnorVG\WholeTextFunctions\WholeTextFunctionsServiceProvider'

Also, this Facade:

'WholeTextFunctions' => 'ConnorVG\WholeTextFunctions\WholeTextFunctionsFacade'

I recommend:

'WTF' => 'ConnorVG\WholeTextFunctions\WholeTextFunctionsFacade'