Vano
Новичок
Не будет ли лишним такой подход? 1) Наполнять модели после выборки такими обьектами :
И пример одного из (Country, City, Street...)
На детали обработки строк и __toString() не обращайте внимания. 2) Вопрос в том, стоит ли игра свечь? 3) И правильно ли я уловил суть? 4) Второй вопрос, стоит ли для каждого поля что в БД создавать обьект? 5) Походу стоит сделать обьект String, который будет иметь в себе весь функционал для работы со строками, а при гидрации обьекта поднастроить его.
Пронумеровал все вопросы, чтобы вы могли удобно отвечать))
PHP:
<?php
namespace App\Models\Values;
use App\Models\Values\Country;
use App\Models\Values\City;
use App\Models\Values\Street;
use App\Models\Values\SuperValueTrait;
use App\Models\Values\SuperValueInterface;
class Address implements SuperValueInterface
{
use SuperValueTrait;
protected $value;
/**
* @var Country
*/
protected $country;
/**
* @var City
*/
protected $city;
/**
* @var Street
*/
protected $street;
public function __construct(Country $country, City $city, Street $street)
{
$this->setCountry($country);
$this->setCity($city);
$this->setStreet($street);
}
/*
* Setters bundle
*/
public function setCountry(Country $country)
{
$this->country = $country;
}
public function setCity(City $city)
{
$this->city = $city;
}
public function setStreet(Street $street)
{
$this->street = $street;
}
/*
* Getters bundle
*/
public function getCountry()
{
return $this->country;
}
public function getCity()
{
return $this->city;
}
public function getStreet()
{
return $this->street;
}
public function getValue($default = '')
{
$value = $this->innerGetValue();
if (empty($value)) {
return $default;
}
return $value;
}
protected function innerGetValue()
{
$valueArray = [];
if (!empty($this->country)) {
$valueArray['country'] = "{$this->country}";
}
if (!empty($this->city)) {
$valueArray['city'] = "г. {$this->city}";
}
if (!empty($this->street)) {
$valueArray['street'] = "{$this->street}";
}
return implode(", ", $valueArray);
}
}
PHP:
<?php
namespace App\Models\Values;
trait SuperValueTrait
{
public function __toString()
{
return $this->getValue();
}
public function __get($attribute)
{
// TODO: if not isset throw exception
return $this->$attribute->getValue();
}
}
PHP:
<?php
namespace App\Models\Values;
class City implements SuperValueInterface
{
use SuperValueTrait;
protected $value;
public function __construct($value)
{
$this->setValue($value);
}
public function setValue($value)
{
$this->value = strtolower((string)$value);
}
public function getValue()
{
return ucfirst($this->value);
}
}
Пронумеровал все вопросы, чтобы вы могли удобно отвечать))