Download the PHP package snscripts/result without Composer

On this page you can find all versions of the php package snscripts/result. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package result

Framework Agnostic Results Object

Author Source Code Latest Version Software License Build Status

Introduction

This Result Object is a PSR-2 Compliant Framework Agnostic helper designed for returning data in an object orientated way. It allows you do easily define the success or failure of the recent action as well as set easily checkable status strings, fuller messages, error strings and any extra data.

Requirements

Composer

Snscripts\Result requires the following:

And the following if you wish to run in dev mode and run tests.

Installation

Composer

Simplest installation is via composer.

composer require snscripts/result 1.*

or adding to your projects composer.json file.

{
    "require": {
        "snscripts/result": "1.*"
    }
}

Usage

In it's simplest form the package can be used in the following ways

// A successful result
$Result = \Snscripts\Result\Result::success();

// A failure result
$Result = \Snscripts\Result\Result::fail();

You would then need to return $Result from your function or library, within the code that called your library you can then check whether the result was a success or a fail depending on your needs

$Result = $MyLibrary->someAction(); // this returns Result::success();

// check for a success
var_dump($Result->isSuccess());

// check for a fail
var_dump($Result->isFail());

Status Codes

Sometimes you may wish to define a status code to go with the result, this can further describe the result. This can also be useful in multi-lingual situations where your code requires a fixed string / code to define exactly what happened.

You can set a code using $Result->setCode('myCode'); and retrieve the code from the result using $Result->getCode();.

As default the object comes with some defined constants for common results.

CREATED    = 'created',
UPDATED    = 'updated',
SAVED      = 'saved',
DELETED    = 'deleted',
VALIDATION = 'validation',
AUTH       = 'authorised',
NOT_AUTH   = 'not_authorised',
FOUND      = 'found',
NOT_FOUND  = 'not_found',
ERROR      = 'error',
FAILED     = 'failed',
PROCESSING = 'processing'

These are object constants so would be accessed in the following way.

use Snscripts\Result\Result;

var_dump(Result::CREATED);
var_dump(Result::AUTH);
var_dump(Result::NOT_FOUND);

This means you can use them in the following way in combination with setCode method above.

$Result->setCode(Result::ERROR);

echo $Result->getCode(); // 'error'

The error code can also be passed as the first parameter of the static success and fail methods.

Result::success(Result::CREATED);
Result::fail(Result::VALIDATION);

Message

Along with the status code you can also define a message to pass along. This could be the actual message that is displayed to the user, a language string for translation, etc...

This is set and retrieved in the following way.

$Result->setMessage('Your item was added successfully');
$Result->getMessage();

For convience, a message can also be passed as the second parameter of the static success and fail methods.

Result::success(
    Result::CREATED,
    'Your item was created successfully'
);
Result::fail(
    Result::VALIDATION,
    'There was a validation error, please try again'
);

Errors

If the result is a fail result, you may need to pass an array of errors. This will generally be from your systems validation methods.

You can set a full array of errors in one go by using the setErrors command.

$Result->setErrors([
    'field' => [
        'is required,
        'must be unique'
    ],
    'another_field' => 'is required'
]);

You can then retrieve the error list very simply by calling

var_dump($Result->getErrors());

In some cases you may be looping through your data and need to set error messages one at a time.

You can do this by using setError

$Result->setError('Field is required');
$Result->setError(['field' => 'is required']);

The setError method has been written so any strings set will be pushed onto the array as normal.

$Result->setError('Field is required');
$Result->setError('Another error occurred');

// var_dump($Result->getErrors()); Will result in
[
    'Field is required',
    'Another error occurred'
]

In the event of an array being passed, the method will try and merge the array in. This helps prevent an extra array level being created and also helps merge data if multiple errors occurred on the same key.

$Result->setError('Field is required');
$Result->setError(['field' => 'is required']);
// var_dump($Result->getErrors()); Will result in
[
    'Field is required',
    'field' => 'is required'
]

For convience, you can pass an array of errors into the third parameter of the success and fail static methods, this will perform the equivilant of the setErrors command and will set the complete array of errors.

Result::fail(
    Result::VALIDATION,
    'There was a validation error, please try again',
    [
        'field1' => 'is required',
        'field2' => [
            'is required',
            'should be unique'
        ]
    ]
);

Extras

With any result you may wish to passed some extra data back with the result, this could be a copy of the data just manipulated or a copy of the form array that just failed validation, with Result you can pass this simply with the following commands.

You can set a full array of extra data in one go by using the setExtras command.

$Result->setExtras([
    'foo' => 'bar',
    'bar' => 'foo',
    [
        'fizzbuzz',
        'buzzfizz'
    ]
]);

You can then retrieve the extra data list very simply by calling

var_dump($Result->getExtras());

In some cases you may be looping through your data and need to set data one piece at a time.

You can do this by using setExtra

$Result->setExtra('foobar');
$Result->setExtra(['foo' => 'bar']);

The setExtra method has been written so any strings set will be pushed onto the array as normal.

$Result->setExtra('foobar');
$Result->setExtra('barfoo');

// var_dump($Result->getExtras()); Will result in
[
    'foobar',
    'barfoo'
]

In the event of an array being passed, the method will try and merge the array in. This helps prevent an extra array level being created and also helps merge data if multiple items occurred on the same key.

$Result->setExtra('foobar');
$Result->setExtra(['foo' => 'bar']);
// var_dump($Result->getExtras()); Will result in
[
    'Foobar',
    'foo' => 'bar'
]

For convience, you can pass an array of extra data into the fourth parameter of the success and fail static methods, this will perform the equivilant of the setExtras command and will set the complete array of data.

Result::success(
    Result::CREATED,
    'Your item was successfully created',
    [],
    [
        'id' => 1,
        'name' => 'Foo Bar',
        'address' => 'fizzbuzz street'
    ]
);

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.


All versions of result with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package snscripts/result contains the following files

Loading the files please wait ....