1. Go to this page and download the library: Download piggly/php-payload library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
piggly / php-payload example snippets
namespace Piggly\Dev\Payload;
use Piggly\Payload\Exceptions\InvalidDataException;
use Piggly\Payload\PayloadArray;
use Respect\Validation\Validator as v;
class Person extends PayloadArray
{
/**
* Import $input data to payload.
*
* @param array $input
* @since 1.0.0
* @return self
* @throws InvalidDataException
*/
public function import ( $input )
{
$input = is_array( $input ) ? $input : json_decode($input, true);
$_map = [
'name' => 'setName',
'email' => 'setEmail',
'phone' => 'setPhone',
'address' => 'setAddress'
];
return $this->_importArray($_map, $input);
}
/**
* Validate all data from payload.
* Throw an exception when cannot validate.
*
* @since 1.0.0
* @return void
* @throws InvalidDataException
*/
public function validate ()
{
$ InvalidDataException::invalid($this, 'phone', $phone, 'Invalid phone.'); }
$this->add('phone', $phone);
return $this;
}
/**
* Get e-mail.
*
* @since 1.0.0
* @return string
*/
public function getEmail ()
{ return $this->get('email'); }
/**
* Set e-mail.
*
* @param string $email E-mail.
* @since 1.0.0
* @return self
* @throws InvalidDataException
*/
public function setEmail ( string $email )
{
if ( v::email()->validate($email) === false )
{ throw InvalidDataException::invalid($this, 'email', $email, 'Invalid e-mail.'); }
$this->add('email', $email);
return $this;
}
/**
* Get address.
*
* @since 1.0.0
* @return Address
*/
public function getAddress ()
{ return $this->get('address'); }
/**
* Set address.
*
* @param Address|array $address Address.
* @since 1.0.0
* @return self
*/
public function setAddress ( $address )
{
if ( !($address instanceof Address) )
{ $address = (new Address())->import($address); }
$this->add('address', $address);
return $this;
}
}
namespace Piggly\Dev\Payload;
use Piggly\Payload\Exceptions\InvalidDataException;
use Piggly\Payload\PayloadArray;
use Respect\Validation\Validator as v;
class Address extends PayloadArray
{
/**
* Import $input data to payload.
*
* @param array $input
* @since 1.0.0
* @return self
* @throws InvalidDataException
*/
public function import ( $input )
{
$input = is_array( $input ) ? $input : json_decode($input, true);
$_map = [
'address' => 'setAddress',
'number' => 'setNumber',
'complement' => 'setComplement',
'district' => 'setDistrict',
'city' => 'setCity',
'country_id' => 'setCountry',
'postal_code' => 'setPostalCode'
];
return $this->_importArray($_map, $input);
}
/**
* Validate all data from payload.
* Throw an exception when cannot validate.
*
* @since 1.0.0
* @return void
* @throws InvalidDataException
*/
public function validate ()
{
$lic function getNumber ()
{ return $this->get('number'); }
/**
* Set number.
*
* @param string $number Number.
* @since 1.0.0
* @return self
*/
public function setNumber ( string $number )
{
$this->add('number', $number);
return $this;
}
/**
* Get district name.
* @since 1.0.0
* @return string
*/
public function getDistrict ()
{ return $this->get('district'); }
/**
* Set district name.
*
* @param string $district District name.
* @since 1.0.0
* @return self
*/
public function setDistrict ( string $district )
{
$this->add('district', \ucwords($district));
return $this;
}
/**
* Get city name.
*
* @since 1.0.0
* @return string
*/
public function getCity ()
{ return $this->get('city'); }
/**
* Set city name.
*
* @param string $city City name.
* @since 1.0.0
* @return self
*/
public function setCity ( string $city )
{
$this->add('city', \ucwords($city));
return $this;
}
/**
* Get country ID.
*
* @since 1.0.0
* @return string
*/
public function getCountry ()
{ return $this->get('country_id'); }
/**
* Set country ID.
*
* @param string $country_id Country ID.
* @since 1.0.0
* @return self
* @throws InvalidDataException
*/
public function setCountry ( string $country_id )
{
if ( v::countryCode()->validate($country_id) === false )
{ throw InvalidDataException::invalid($this, 'country_id', $country_id, 'Invalid country code.'); }
$this->add('country_id', \strtoupper($country_id));
return $this;
}
/**
* Get postal Code.
*
* @since 1.0.0
* @return string
*/
public function getPostalCode ()
{ return $this->get('postal_code'); }
/**
* Set postal Code.
*
* @param string $postal_code Postal Code.
* @since 1.0.0
* @return self
* @throws InvalidDataException
*/
public function setPostalCode ( string $postal_code )
{
if ( v::postalCode( $this->get('country_id', 'US') )->validate($postal_code) === false )
{ throw InvalidDataException::invalid($this, 'postal_code', $postal_code, 'Invalid postal code.'); }
$this->add('postal_code', \preg_replace('/[^\d]/', '', $postal_code));
return $this;
}
}
use Piggly\Dev\Payload\Person;
$person = [
'name' => 'John Connor',
'email' => '[email protected]',
'phone' => '+1-202-555-0172',
'address' => [
'address' => 'Future Avenue',
'number' => '2047',
'complement' => 'High Tech World',
'district' => 'Nobody\'s Alive',
'city' => 'Unknown',
'country_id' => 'US',
'postal_code' => '55372'
]
];
// Import from an array
$person = (new Person())->import($person);
// Validate (throw an exception if can't)
$person->validate();
// Payload object to array
$_array = $person->toArray();
// Payload object to json
$_json = $person->toJson();
// Alternative way to converto to json
$_json = json_encode($person);
// Serialize
$_serialized = serialize($person);
// Unserialize
$_unserialized = unserialize($_serialized);
namespace Piggly\Dev\Payload;
use Piggly\Payload\Exceptions\InvalidDataException;
use Piggly\Payload\PayloadMap;
use Respect\Validation\Validator as v;
class PersonMap extends PayloadMap
{
/**
* This method is called at constructor.
* You should use it to setup fields map
* with add method.
*
* @since 1.0.5
* @return void
*/
protected function _map ()
{
$this
->add('name')
->port ( $input, $ignoreInvalid = true )
{
$input = is_array( $input ) ? $input : json_decode($input, true);
return $this->_importArray($input, $ignoreInvalid);
}
/**
* Mutator for address.
*
* @param AddressMap|array $address Address.
* @since 1.0.0
* @return AddressMap
*/
protected function setterAddress ( $address ) : AddressMap
{
if ( !($address instanceof AddressMap) )
{ $address = (new AddressMap())->import($address); }
return $address;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.