✨ Ajoute des vues
This commit is contained in:
99
app/Deal.php
Normal file
99
app/Deal.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use function GuzzleHttp\json_decode;
|
||||
use Shikiryu\LBCReposter\Categories;
|
||||
|
||||
class Deal
|
||||
{
|
||||
/** @var Folder */
|
||||
protected $folder;
|
||||
/** @var array */
|
||||
protected $data;
|
||||
/** @var string[] */
|
||||
protected $images = [];
|
||||
/** @var int */
|
||||
protected $id;
|
||||
|
||||
/**
|
||||
* Deal constructor.
|
||||
* @param $folder
|
||||
* @param int $id
|
||||
*/
|
||||
public function __construct($folder, $id)
|
||||
{
|
||||
$this->folder = $folder;
|
||||
$this->id = $id;
|
||||
foreach (new \DirectoryIterator(sprintf('%s/%s/%s', config('app.deals_dire'), $folder, $id)) as $file) {
|
||||
if ($file->getExtension() === 'json') {
|
||||
$content = file_get_contents($file->getPathname());
|
||||
$this->data = json_decode($content, true);
|
||||
} elseif ($file->getExtension() === 'jpg') {
|
||||
$data = file_get_contents($file->getPathname());
|
||||
$base64 = 'data:image/jpg;base64,' . base64_encode($data);
|
||||
$this->images[] = $base64;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getDateCreation(): \DateTime
|
||||
{
|
||||
$date_creation = \DateTime::createFromFormat('Y-m-d H:i:s.u', $this->data['datecreation']['date']);
|
||||
return $date_creation;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \string[]
|
||||
*/
|
||||
public function getImages(): array
|
||||
{
|
||||
return $this->images;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->data['subject'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBody(): string
|
||||
{
|
||||
return $this->data['body'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return float
|
||||
*/
|
||||
public function getPrice(): float
|
||||
{
|
||||
return $this->data['price'];
|
||||
}
|
||||
|
||||
public function getCategory(): string
|
||||
{
|
||||
return Categories::$categories[$this->data['category']];
|
||||
}
|
||||
|
||||
}
|
11
app/Folder.php
Normal file
11
app/Folder.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
|
||||
class Folder
|
||||
{
|
||||
const FOLDER_NEW = 'new';
|
||||
const FOLDER_BACKUP = 'backup';
|
||||
const FOLDER_CURRENT = 'list';
|
||||
}
|
@@ -2,14 +2,15 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Deal;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -25,4 +26,27 @@ class HomeController extends Controller
|
||||
{
|
||||
return view('home');
|
||||
}
|
||||
|
||||
public function list($type)
|
||||
{
|
||||
$deals = [];
|
||||
$folder_path = sprintf('%s/%s', config('app.deals_dir'), $type);
|
||||
if (file_exists($folder_path)) {
|
||||
foreach (new \RecursiveDirectoryIterator($folder_path) as $folder) {
|
||||
if (file_exists($folder->getPathname().'/data.json')) {
|
||||
$deals[] = new Deal($folder_path, $folder->getBasename());
|
||||
}
|
||||
}
|
||||
}
|
||||
return view('list', ['deals' => $deals, 'type' => $type]);
|
||||
}
|
||||
|
||||
public function view($type, $id)
|
||||
{
|
||||
$folder_path = sprintf('%s/%s/%s', config('app.deals_dir'), $type, $id);
|
||||
if (file_exists($folder_path)) {
|
||||
$deal = new Deal(sprintf('%s/%s', config('app.deals_dir'), $type), $id);
|
||||
}
|
||||
return view('view', ['deal' => $deal, 'type' => $type]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user