From a319f3a419790925bed539ba141038c72a83e70f Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Mon, 10 Jan 2022 20:29:04 +0100 Subject: Initial commit --- .gitignore | 3 ++ cli-config.php | 10 ++++++ composer.json | 29 +++++++++++++++++ public/index.php | 28 ++++++++++++++++ src/AppException.php | 7 ++++ src/Controller/Card.php | 31 ++++++++++++++++++ src/Controller/Home.php | 22 +++++++++++++ src/DB.php | 55 +++++++++++++++++++++++++++++++ src/Model/Card.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ src/Model/CardMeta.php | 57 ++++++++++++++++++++++++++++++++ src/Router.php | 70 +++++++++++++++++++++++++++++++++++++++ src/Template.php | 23 +++++++++++++ templates/home.twig | 2 ++ 13 files changed, 424 insertions(+) create mode 100644 .gitignore create mode 100644 cli-config.php create mode 100644 composer.json create mode 100644 public/index.php create mode 100644 src/AppException.php create mode 100644 src/Controller/Card.php create mode 100644 src/Controller/Home.php create mode 100644 src/DB.php create mode 100644 src/Model/Card.php create mode 100644 src/Model/CardMeta.php create mode 100644 src/Router.php create mode 100644 src/Template.php create mode 100644 templates/home.twig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8504ffb --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +db.sqlite + diff --git a/cli-config.php b/cli-config.php new file mode 100644 index 0000000..917e29f --- /dev/null +++ b/cli-config.php @@ -0,0 +1,10 @@ + 'meta[name]', + 'converted mana cost' => 'meta[cmc]', + 'is uno reverse' => 'meta[is_uno_reverse]', +]; +?> + +
+ $field): ?> + + + +
+ +get('meta') as $key => $value) { + $meta = new CardMeta($key, $value); + $card->addMeta($meta); + DB::save($meta); + } + + DB::save($card); + + $response = new RedirectResponse('/'); + + return $response; + } +} + diff --git a/src/Controller/Home.php b/src/Controller/Home.php new file mode 100644 index 0000000..511b89b --- /dev/null +++ b/src/Controller/Home.php @@ -0,0 +1,22 @@ +getRepository(Card::class)->findOneBy([], ['id' => 'DESC']); + echo "
";
+    $c && var_dump(
+      array_map(fn ($item) => [$item->key, $item->value], $c->meta->toArray()),
+    );
+
+    return Template::render('home.twig');
+  }
+}
+
diff --git a/src/DB.php b/src/DB.php
new file mode 100644
index 0000000..c04d189
--- /dev/null
+++ b/src/DB.php
@@ -0,0 +1,55 @@
+setMetadataDriverImpl($config->newDefaultAnnotationDriver([__DIR__ . '/Model'], true));
+    #$config->setMetadataDriverImpl(new AttributeDriver([__DIR__ . '/Model']));
+
+    $conn = array(
+      'driver' => 'pdo_sqlite',
+      'path' => dirname(__DIR__) . '/db.sqlite',
+    );
+
+    self::$entityManager = EntityManager::create($conn, $config);
+  }
+
+  /**
+   * @param object $entity
+   * @param array $criteria
+   */
+  public static function save(object $entity, array $criteria = [])
+  {
+    $repository = self::$entityManager->getRepository(get_class($entity));
+    $exists = $repository->findOneBy($criteria);
+
+    if (! empty($criteria) && ! is_null($exists)) {
+      foreach (get_object_vars($entity) as $key => $value) {
+	$exists->$key = $value;
+      }
+    } else {
+      $exists = $entity;
+    }
+
+    self::$entityManager->persist($exists);
+    self::$entityManager->flush();
+  }
+}
+
diff --git a/src/Model/Card.php b/src/Model/Card.php
new file mode 100644
index 0000000..ab421ab
--- /dev/null
+++ b/src/Model/Card.php
@@ -0,0 +1,87 @@
+meta = new ArrayCollection();
+  }
+
+  /**
+   * @param CardMeta $meta
+   */
+  public function addMeta(CardMeta $meta)
+  {
+    $meta->card = $this;
+    $this->meta[] = $meta;
+  }
+
+  /**
+   * @param string $key
+   *
+   * @return string
+   */
+  public function getMeta(string $key): ?string
+  {
+    // if meta is already hydrated
+    if ($this->meta->isInitialized()) {
+      $meta = $this->meta->unwrap()
+	#->findFirst(fn (CardMeta $item) => $item->key === $key);
+	->filter(fn ($item) => $item->key === $key)->first();
+
+      return $meta->value ?? null;
+    }
+
+    // get directly from db otherwise
+    $result = DB::$entityManager
+      ->createQuery(
+	'SELECT cm.value
+	FROM Elements\Model\CardMeta cm
+	WHERE cm.key = :key AND cm.card = :card'
+      )
+      ->setParameter('key', $key)
+      ->setParameter('card', $this)
+      ->getOneOrNullResult();
+
+      return $result['value'] ?? null;
+  }
+}
+
diff --git a/src/Model/CardMeta.php b/src/Model/CardMeta.php
new file mode 100644
index 0000000..2ec0da9
--- /dev/null
+++ b/src/Model/CardMeta.php
@@ -0,0 +1,57 @@
+key = $key;
+    $this->value = $value;
+  }
+}
+
diff --git a/src/Router.php b/src/Router.php
new file mode 100644
index 0000000..377c00b
--- /dev/null
+++ b/src/Router.php
@@ -0,0 +1,70 @@
+ [
+      '/' => [Home::class, 'index'],
+    ],
+
+    'POST' => [
+      '/card/add' => [Card::class, 'add'],
+    ],
+  ];
+
+  /**
+   * Router constructor.
+   */
+  public function __construct()
+  {
+    $request = Request::createFromGlobals();
+    $response = new Response();
+
+    $method = $request->getMethod();
+    $path = $request->getPathInfo();
+
+    // if the route is defined
+    if (isset($this->routes[$method][$path])) {
+      try {
+	// run controller function
+	$content = call_user_func($this->routes[$method][$path], $request);
+
+	// set response to new response
+	if ($content instanceof Response) {
+	  $response = $content;
+	}
+
+	// set content directly otherwise
+	else {
+	  $response->setContent($content);
+	}
+      }
+
+      // catch any errors
+      catch (AppException $exception) {
+	$response->setStatusCode($exception->getCode());
+	$response->setContent($exception->getMessage());
+      } catch (\Exception $exception) {
+	$response->setStatusCode(Response::HTTP_BAD_REQUEST);
+	$response->setContent($exception->getMessage());
+      }
+    }
+
+    // route is not defined
+    else {
+      $response->setStatusCode(Response::HTTP_NOT_FOUND);
+      $response->setContent('Not Found');
+    }
+
+    $response->send();
+  }
+}
+
diff --git a/src/Template.php b/src/Template.php
new file mode 100644
index 0000000..b4360b8
--- /dev/null
+++ b/src/Template.php
@@ -0,0 +1,23 @@
+render($name, $context);
+  }
+}
+
diff --git a/templates/home.twig b/templates/home.twig
new file mode 100644
index 0000000..bc5b812
--- /dev/null
+++ b/templates/home.twig
@@ -0,0 +1,2 @@
+

Home

+ -- cgit v1.2.3