Download the PHP package dkd/php-populate without Composer

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

PHP Trait: Populate

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads Latest Unstable Version License

A simple Trait for PHP classes enabling properties to be populated and exported using the object's getters and setters but through a single method.

PopulateTrait is great because it:

Usage

Implemented as follows:

The PopulateInterface is optional - it is included to enable you to use type hinting in methods, and to be able to pass other instances as source data when populating. If you plan to pass your objects as data source to other objects, you must either implement the interface or manually call $source->exportGettableProperties(); to extract the values before you pass them to populate(). The following examples all illustrate usages where $source implements the interface.

Implemented in the class, the Trait allows the following to populate data:

And the following to export data to a simple array:

Note that in both examples when no mapping of properties' names is desired, the array acts as a list of names and not a map as such. Both methods will intelligently detect if you used a numerically indexed array or a string indexed array and behave accordingly; given that no PHP version allows class properties whose names are an integer value. When encountering a numerically indexed array, a new property name "map" (in quotes) is created using array_combine with the input as both keys and values.

Because of this the two arrays array('before' => 'before') and array('before') have the same meaning when populating or exporting.

Populating with objects: reference or clone?

When you populate an object and the input data contains other object instances, your expectation may be that clone is used on each object in order to populate with a clone, not a reference.

However, the default behavior of Populate is to populate any object values with references. If you wish to have objects cloned instead, switch to the alternative method:

This causes all object-type values to be cloned before being set on $copy.

If your requirement is that some properties be populated with clones and others with references, there are two ways to reach your goal:

Naturally, you would select the method that requires the least number of property names to be passed in the second populate operation.

The other, more obvious way is outside the scope of Populate because it manually post-processes the values to use clones/references, but it works just the same:

You can use whichever method fits your application design best. Populate provides methods which are suitable for generic usage but does not prevent you from using the existing setters and getters in any way.

Common pitfall

When populating objects with other objects as source and these other objects contain nested objects, the cloning that is performed by Populate does not happen recursively. To gain control over the cloning behavior for each object you are advised to define __clone methods on each object you need to control.

See the official PHP documentation, cloning chapter

Supported input types

The populate() method supports the following input types:

Note that the final type, an object implementing ArrayAccess but not implementing Iterator, is only supported when using a manual list of property names along with the "only selected properties" flag.

Supported getters and setters

PopulateTrait does not care about the value types you are setting, but it does care about how your getters and setters are constructed.

In order to properly use your getters and setters, PopulateTrait requires the following to be fulfilled:

  1. Getters must have no mandatory arguments
  2. Setters must have no more than one mandatory argument
  3. Valid getter names are (if property is name):
    1. getName
    2. isName
    3. getIsName
    4. name
  4. Valid setter names are (if property is name):
    1. setName
    2. setIsName

The additional is-style method names are only attemted if the standard methods do not exist - this is done in order to accommodate the standard PHP pattern of naming boolean getters/setters using is. The getter can also be just the property name. This could be useful if the property is named like $isFinished.

Special requirements

There must be only one getter for each property. If multiple getters are found for a property, based on the rules above, an exception is thrown. This is required to ensure that the getter for a property always acts the same way.

Handling an error from PopulateTrait

Errors from PopulateTrait methods are thrown as Dkd\Populate\Exception with a unique exception code for error types, which are:

And using a more specific Dkd\Populate\AccessException for:

The Exception type is caught as usual:

Edge cases

Being very compact and not using Reflection or any configuration of any kind, there are some edge cases that Populate cannot handle.

The edge cases and their workarounds:


All versions of php-populate with dependencies

PHP Build Version
Package Version
No informations.
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 dkd/php-populate contains the following files

Loading the files please wait ....