1. Go to this page and download the library: Download 10quality/php-data-model 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/ */
10quality / php-data-model example snippets
use TenQuality\Data\Model;
class Product extends Model
{
}
$product = new Product;
// Set data
$product->price = 19.99;
$product->name = 'My product';
$product->brandName = '10 Quality';
// Get data
echo $product->price; // Will echo 19.99
echo $product->name; // Will echo My product
echo $product->brandName; // Will echo 10 Quality
use TenQuality\Data\Model;
class Product extends Model
{
/**
* Method for alias property `displayPrice`.
* Return `price` property concatenated with the $ (dollar) symbol
*/
protected function getDisplayPriceAlias()
{
return '$'.$this->price;
}
/**
* Method for alias property `displayPrice`.
* Sets a the price value if the alias is used.
*/
protected function setDisplayPriceAlias($value)
{
$this->price = floatval(str_replace('$', '', $value));
}
}
$product->price = 19.99;
// Get alias property
echo $product->displayPrice; // Will echo $19.99
// Set alias property
$product->displayPrice = 29.99;
// Echo property
echo $product->price; // Will echo 29.99
echo $product->displayPrice; // Will echo $29.99
$product = new Product(['price' => 19.99]);
// Echo property
echo $product->price; // Will echo 19.99
use TenQuality\Data\Model;
class Product extends Model
{
protected $properties = [
'name',
'price',
'displayPrice',
];
}
var_dump($model->toArray()); // To array
var_dump($model->__toArray()); // To array
echo (string)$model; // To json encoded string
echo $model->toJSON(); // To json encoded string
echo $model->__toJSON(); // To json encoded string
// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation
// http://php.net/manual/en/function.json-encode.php
echo $model->toJSON(JSON_NUMERIC_CHECK, 600);
use TenQuality\Data\Collection;
$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = $product;
$collection[] = $product;
echo count($collection); // Thrown count
var_dump($collection[0]); // Dumps first model added
// Loop a collection
foreach ($collection as $product) {
// Do your stuff
}
use TenQuality\Data\Collection;
$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = new Product(['price' => 99.99]);
$collection[] = new Product(['price' => 2.99]);
// Loop a collection
foreach ($collection->sortBy('price') as $product) {
echo $product->price; // 2.99 will display first and then 99.99
}
// Change sorting criteria
// http://php.net/manual/en/array.constants.php
print_r($collection->sortBy('price', SORT_NUMERIC));
use TenQuality\Data\Collection;
$collection = new Collection;
// Add your models as you would normally do with arrays
$collection[] = new Fruit(['name' => 'Apple', 'color' => 'red']);
$collection[] = new Fruit(['name' => 'Banana', 'color' => 'yellow']);
$collection[] = new Fruit(['name' => 'Strawberry', 'color' => 'red']);
$collection[] = new Fruit(['name' => 'Orange', 'color' => 'orange']);
// Loop a collection
foreach ($collection->groupBy('color') as $color => $fruits) {
// This will group the data in sub arrays based on colors
echo $color;
foreach ($fruits as $fruit) {
echo $fruit; // Fruit in the color grouped by.
}
}
var_dump($collection->toArray()); // To array
var_dump($collection->__toArray()); // To array
echo (string)$collection; // To json encoded string
echo $collection->toJSON(); // To json encoded string
echo $collection->__toJSON(); // To json encoded string
// You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation
// http://php.net/manual/en/function.json-encode.php
echo $collection->toJSON(JSON_NUMERIC_CHECK, 600);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.