Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Class Master
- * @property integer id
- * @property string name
- * @property string city;
- * @property bool delivery
- * @property array services
- */
- class Master
- {
- private $id;
- private $name;
- private $city;
- private $delivery = false;
- private $services = [];
- public function __construct(int $id, string $name, string $city, bool $delivery = false)
- {
- $this->id = $id;
- $this->name = $name;
- $this->city = $city;
- $this->delivery = $delivery;
- }
- public function getId(): int
- {
- return $this->id;
- }
- public function setServices(array $services = [])
- {
- $this->services = $services;
- }
- public function getServices(): array
- {
- return $this->services;
- }
- public function formatServices(): array
- {
- return array_map(function ($service) {
- return \sprintf(
- '%s - %s - %d',
- $service['title'],
- $this->name,
- $service['price']
- );
- }, $this->getServices());
- }
- }
- $equalsRow = str_repeat('=', 10);
- $masters = [
- new Master(2, 'Nick', 'Moscow', true),
- new Master(10, 'Cheburashka', 'Chelyabinsk', false),
- ];
- $services = [
- ['master_id' => 2, 'title' => 'Concrete', 'price' => 200],
- ['master_id' => 2, 'title' => 'Tile', 'price' => 80],
- ['master_id' => 10, 'title' => 'Square', 'price' => 799],
- ];
- array_map(function (Master $master) use ($services) {
- $master->setServices(
- array_filter(
- $services,
- function ($service) use ($master) {
- return $service['master_id'] === $master->getId();
- }
- )
- );
- }, $masters);
- echo \sprintf('%s %s %s' . PHP_EOL, $equalsRow, 'Товары', $equalsRow);
- array_map(function (Master $master) {
- array_map(function ($service) {
- echo \sprintf("%s" . PHP_EOL, $service);
- }, $master->formatServices());
- }, $masters);
RAW Paste Data