PHP code example of isakzhanov-r / laravel-value-object
1. Go to this page and download the library: Download isakzhanov-r/laravel-value-object 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/ */
isakzhanov-r / laravel-value-object example snippets
use IsakzhanovR\ValueObject\ValueObject;
class Temperature extends ValueObject
{
....
}
use IsakzhanovR\ValueObject\ValueObject;
class Temperature extends ValueObject
{
protected function transformInput($value)
{
return $value;
}
protected function rules(): array
{
return [];
}
}
$temperature = new Temperature(25);
$temperature = Temperature::create(25);
use IsakzhanovR\ValueObject\ValueObject;
class Temperature extends ValueObject
{
....
protected function rules(): array
{
return [
$this->key => ['
use IsakzhanovR\ValueObject\ValueObject;
class Address extends ValueObject
{
....
protected function rules(): array
{
return [
$this->key => [' $this->key . '.number' => ['
use IsakzhanovR\ValueObject\ValueObject;
class Temperature extends ValueObject
{
....
protected function messages(): array
{
return [
$this->key.'.between' => 'The range of tmp temperatures should be from -100 to +100',
];
}
}
use Illuminate\Database\Eloquent\Model;
class Weather extends Model
{
....
protected $casts = [
...
'temperature' => Temperature::class,
...
];
....
}
use IsakzhanovR\ValueObject\ValueObject;
class Temperature extends ValueObject
{
....
public function inCelsius()
{
return (float) $this->value();
}
public function inKelvin()
{
return (float) $this->value() + 273.15;
}
public function inFahrenheit()
{
return (float) $this->value() * 1.8 + 32;
}
}
$weather = new Weather;
$weather->temperature = new Temperature(9);
echo $weather->temperature; // Prints '9'
echo $weather->temperature->value(); // Prints '9'
echo $weather->temperature->inKelvin(); // Prints 282.15
echo $weather->temperature->inFahrenheit(); // Prints 48.2
$weather = new Weather;
$weather->temperature = new Temperature(9);
$weather->save()
//Or
$temperature = new Temperature(9);
$weather = Weather::create(compact('temperature'));
//Or
$weather = Weather::create(Temperature::create(9)->toDTO());
class Weather extends Model
{
....
protected $casts = [
...
'temperature' => Temperature::class,
...
];
protected $appends = ['celsius','kelvin','fahrenheit']
public function getCelsiusAttribute()
{
return $this->temperature->inCelsius();
}
public function getKelvinAttribute()
{
return $this->temperature->inKelvin();
}
public function getFahrenheitAttribute()
{
return $this->temperature->inFahrenheit();
}
}
use IsakzhanovR\ValueObject\ValueObject;
class Title extends ValueObject
{
protected function transformInput($value)
{
$value = trim(e($value));
return mb_ucfirst(Str::lower($value));
}
...
}
class User extends Model
{
protected $casts = [
'address' => Address::class
];
}
use IsakzhanovR\ValueObject\ValueObject;
class Address extends ValueObject
{
protected function transformInput($value)
{
return $value;
}
protected function rules(): array
{
return [
$this->key => [';
}
}
class Address Extends ValueObject
{
....
public static function unserialize($value)
{
return json_decode($value, true);
}
public static function serialize($value)
{
return json_encode($value);
}
....
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.