Download the PHP package earc/cast without Composer

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

eArc-cast

earc/cast is a lightweight zero dependencies flat data mapper to cast data represented by objects/arrays onto other objects/arrays. Thus helping to implement the adapter pattern in nearly no time.

It uses property reflection to access the objects and supports user defined mappings.

earc/cast can be used everywhere without dependency injection even in vanilla functions.

table of contents

install

bootstrap

Place the following code snippets in the section where your script/framework is bootstrapped.

  1. Make use of the composer namespace driven autoloading.

  2. Then bootstrap earc/cast to declare the cast, cast_simple and cast_reverse functions.

basic usage

earc/cast supports four types of casting: array to object, object to object.

array to array

If the mapping is omitted, values with identical keys (property names) are assigned from the origin to the target.

In the case of a supplied mapping the keys of the mapping are used for the origin, and the values are used for the target.

Hint: If the value is null, the key is used as value.

Only if a mapping is supplied not existing keys will be created in the target array.

Hint: If a mapping is supplied identical keys (property names) that are not present in the mapping are not used to cast values.

array to object

If the target is an object you can either supply a real object or a class name. In the case of a class name the object is created via reflection without calling the constructor.

object to array

If there is an overwritten private property in the origin object, the property that is nearest to the current class is used.

object to object

All that was said about objects in the array to object and object to array cases applies.

reverse casting

Sometimes you need to map some properties apply a service or two and map the object back to the original data representation.

If the target is an object, you can use reverse casting to keep track of the origin and the mapping. All you have to do is use cast instead of simple_cast.

Hint: If you call cast multiple times with the same target object, the origin and mapping of the last call is used by cast_reverse.

generate mapping

A common use case is to use earc/cast to generate a json representation for a frontend component using all public, protected and private properties. Unfortunately earc/cast uses the target to auto generate a mapping, thus casting an object to an empty array leaves the array empty. The generate_mapping function is the solution.

advanced usage

To overwrite oder rewrite any logic, you have to pass a class implementing the CastServiceInterface to the Initializer. You may extend the original CastService.

architectural considerations

If you follow the dependency inversion principle in your software design, application modules does not depend directly on one another, but on abstractions. This is better than hard coupled components but leaves the problem that the modules have to know and use the same abstraction. You cannot develop components independently, and you cannot reuse components freely.

Think of a slider in a shop that slides product boxes. Let the slider be a part of a content management system that integrates in a shop platform. Then the product box slider introduces a dependency of the content management system from the product selling platform of the shop. That is why you cannot evolve the two big structures cms and product selling platform independently of another. If you cannot break big structures, you should not even consider breaking small parts into independent applications. You end up with a monolithic architecture and thousands of (abstract) dependencies right through the whole application. A nightmare to evolve and maintain.

earc/cast provides an easy way to implement the adapter pattern. This gives you the freedom that comes with runtime interface polymorphism. The product box slider can use a different interface for the product than the selling platform. All that matters is the underlying data. If the product data of the selling platform is sufficient to the product box sliders product interface, an adapter can be written and thus it can be mapped. Now we have true decoupling and can evolve and maintain the cms independently of the selling platform. Moreover, you can break these two applications into smaller parts. All that has to be kept in sync is the adapter which is reduced in the case of earc/cast to a simple one-dimensional array (mapping).

releases

release 1.1

release 1.0

release 0.0


All versions of cast with dependencies

PHP Build Version
Package Version
Requires php Version >=8.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 earc/cast contains the following files

Loading the files please wait ....