Download the PHP package schlaus/schange without Composer

On this page you can find all versions of the php package schlaus/schange. 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 schange

Build Status Latest Stable Version Total Downloads License

Schange

An extendable type juggler.

Installation

Either via Composer by requiring "schlaus/schange": "dev-master"

Or just download and include src/schange.php

Usage

What Schange does is to try to cast a variable to a given type by doing some conversions. The conversion logic is based on my usecase, and could be somewhat off or completely ludicrous in different situations. Parental discretion is adviced.

Syntax is:

Once a custom casting function has been loaded, it can be used in the exact same fashion:

The code and tests are pretty straightforward, and should give a pretty good idea of what kind of conversions are done. Supported types are boolean, integer, string, array, float, and object. Here's a few examples:

Furthermore, associative arrays can be converted to objects (instances of stdClass) with each key => value converted into property => value. When converting from objects to arrays, only public properties are preserved. When converting an object to string, the conversion is first attempted via the magic __toString() method, and if one doesn't exist, the object is first converted to an array, and then to string. Null is only ever returned if given variable can't be cast to requested type, for example when trying to convert a boolean value to an array. If the original variable was null, it's converted to an empty instance of the requested type.

For examining whether a variable can be converted to a type, use schange::canCastTo($type, $var); For a list (as an array) of possible target types, use schange::castable($var);

Using custom casters

Casting functions receive two parameters: the variable to be cast, and the current type of the variable. Thus each function can only handle casting to one target type. For an example of how a casting function should look like, let's look at the castToArr function:

Casting functions should return null only when the requested conversion can't be done, and in case the input was null to begin with, an empty instance of the target type. There are no separate functions for evaluating whether a variable can be cast to another type, but instead the testing is done using the actual casting function, and a result returned based on whether the result was null or not.

Custom casters can be loaded either one function at a time, or as an array of casters:

Once loaded, custom functions can be used just in the same way as the default ones. Custom casters take precedence over the default ones, so for example if you register a function for casting to string, that function will be used instead of the default one.

One thing to note when writing custom functions is that if you want to support PHP 5.3 you can't use self::... in your function, since in PHP 5.3 the closure is not run in the correct scope. Instead, use the full namespaced class name.

DISCLAIMER:

I made this class for my own specific purposes, and don't expect it to be immensely useful to other people. However, I'm happy to help if you do find some use for it and run into problems.


All versions of schange with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.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 schlaus/schange contains the following files

Loading the files please wait ....