diff --git a/.gitignore b/.gitignore index 408cb749d..eb1a2c123 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.vscode/ + node_modules public/build yarn.lock diff --git a/.vscode/extensions.json b/.vscode/extensions.json deleted file mode 100644 index 99e242bbb..000000000 --- a/.vscode/extensions.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "recommendations": ["bradlc.vscode-tailwindcss"] -} diff --git a/README.md b/README.md deleted file mode 100644 index ad88e6fac..000000000 --- a/README.md +++ /dev/null @@ -1,53 +0,0 @@ -# Tailwind CSS Playground - -A simple starter project for playing around with Tailwind in a proper PostCSS environment. - -To get started: - -1. Clone the repository: - - ```bash - git clone https://github.com/tailwindcss/playground.git tailwindcss-playground - - cd tailwindcss-playground - ``` - -2. Install the dependencies: - - ```bash - # Using npm - npm install - - # Using Yarn - yarn - ``` - -3. Start the development server: - - ```bash - # Using npm - npm run serve - - # Using Yarn - yarn run serve - ``` - - Now you should be able to see the project running at localhost:8080. - -4. Open `public/index.html` in your editor and start experimenting! - -## Building for production - -Even though this isn't necessarily a starter kit for a proper project, we've included an example of setting up both [Purgecss](https://www.purgecss.com/) and [cssnano](https://cssnano.co/) to optimize your CSS for production. - -To build an optimized version of your CSS, simply run: - -```bash -# Using npm -npm run production - -# Using Yarn -yarn run production -``` - -After that's done, check out `./public/build/tailwind.css` to see the optimized output. diff --git a/archetypes/default.md b/archetypes/default.md new file mode 100644 index 000000000..f7e5e03d9 --- /dev/null +++ b/archetypes/default.md @@ -0,0 +1,7 @@ +--- +title: "{{ replace .Name "-" " " | title }}" +description: "" +date: {{ .Date }} +draft: true +tags: [] +--- diff --git a/i18n/en.yaml b/i18n/en.yaml new file mode 100644 index 000000000..8326be14a --- /dev/null +++ b/i18n/en.yaml @@ -0,0 +1,5 @@ +- id: back_home + translation: "Back Home" + +- id: not_found_page_title + translation: "Whoops! The page you're looking for doesn't exist :(" diff --git a/layouts/404.html b/layouts/404.html new file mode 100644 index 000000000..afa69fec7 --- /dev/null +++ b/layouts/404.html @@ -0,0 +1,12 @@ +{{ define "heading"}} +
{{ .Site.Params.tagline }}
+yet another blog about dev, sometimes ops
-If you already have heard about DDD (Domain-Driven Design), you probably also may have heard about Value Objects. It is one of the building blocks introduced by Eric Evans in “the blue book”.
- -A value object is a type which wraps data and is distinguishable only by its properties. Unlike an Entity
, it doesn’t have a unique identifier. Thus, two value objects with the same property values should be considered equal.
A good example of candidates for value objects are:
- -When designing a value object, you should pay attention to its three main characteristics: immutability, structural equality, and self-validation.
- -Here’s an example:
- -<?php declare(strict_types=1);
-
- final class Price
- {
- const USD = 'USD';
- const CAD = 'CAD';
-
- /** @var float */
- private $amount;
-
- /** @var string */
- private $currency;
-
- public function __construct(float $amount, string $currency = 'USD')
- {
- if ($amount < 0) {
- throw new \InvalidArgumentException("Amount should be a positive value: {$amount}.");
- }
-
- if (!in_array($currency, $this->getAvailableCurrencies())) {
- throw new \InvalidArgumentException("Currency should be a valid one: {$currency}.");
- }
-
- $this->amount = $amount;
- $this->currency = $currency;
- }
-
- private function getAvailableCurrencies(): array
- {
- return [self::USD, self::CAD];
- }
-
- public function getAmount(): float
- {
- return $this->amount;
- }
-
- public function getCurrency(): string
- {
- return $this->currency;
- }
- }
-
-
- Once you instantiate a value object, it should be the same for the rest of the application lifecycle. If you need to change its value, it should be done by entirely replacing that object.
- -Using mutable value objects is acceptable if you are using them entirely within a local scope, with only one reference to the object. Otherwise, you may have problems.
- -Taking the previous example, here’s how you can update the amount of a Price
type:
<?php declare(strict_types=1);
-
- final class Price
- {
- // ...
-
- private function hasSameCurrency(Price $price): bool
- {
- return $this->currency === $price->currency;
- }
-
- public function sum(Price $price): self
- {
- if (!$this->hasSameCurrency($price)) {
- throw \InvalidArgumentException(
- "You can only sum values with the same currency: {$this->currency} !== {$price->currency}."
- );
- }
-
- return new self($this->amount + $price->amount, $this->currency);
- }
- }
-
-
- Value objects don’t have an identifier. In other words, if two value objects have the same internal values, they must be considered equal. As PHP doesn’t have a way to override the equality operator, you should implement it by yourself.
- -You can create a specialized method to do that:
- -<?php declare(strict_types=1);
-
- final class Price
- {
- // ...
-
- public function isEqualsTo(Price $price): bool
- {
- return $this->amount === $price->amount && $this->currency === $price->currency;
- }
- }
-
-
- Another option is to create a hash based on its properties:
- -<?php declare(strict_types=1);
-
- final class Price
- {
- // ...
-
- private function hash(): string
- {
- return md5("{$this->amount}{$this->currency}");
- }
-
- public function isEqualsTo(Price $price): bool
- {
- return $this->hash() === $price->hash();
- }
- }
-
-
- The validation of a value object should occur on its creation. If any of its properties are invalid, it should throw an exception. Putting this together with immutability, once you create a value object, you can be sure it will always be valid.
- -Taking the Price
type example once again, it doesn’t make sense to have a negative amount for the domain of the application:
<?php declare(strict_types=1);
-
- final class Price
- {
- // ...
-
- public function __construct(float $amount, string $currency = 'USD')
- {
- if ($amount < 0) {
- throw new \InvalidArgumentException("Amount should be a positive value: {$amount}.");
- }
-
- if (!in_array($currency, $this->getAvailableCurrencies())) {
- throw new \InvalidArgumentException("Currency should be a valid one: {$currency}.");
- }
-
- $this->amount = $amount;
- $this->currency = $currency;
- }
- }
-
-
- Storing and retrieving value objects from the database using Doctrine is quite easy using Embeddable
s. According to the documentation, Embeddable
s are not entities. But, you embed them in entities, which makes them perfect for dealing with value objects.
Let’s suppose you have a Product
class, and you would like to store the price in that class. You will end up with the following modeling:
<?php declare(strict_types=1);
-
- /** @Embeddable */
- final class Price
- {
- /** @Column(type="float") */
- private $amount;
-
- /** @Column(type="string") */
- private $currency;
-
- public function __construct(float $amount, string $currency = 'USD')
- {
- // ...
-
- $this->amount = $amount;
- $this->currency = $currency;
- }
-
- // ...
- }
-
- /** @Entity */
- class Product
- {
- /** @Embedded(class="Price") */
- private $price;
-
- public function __construct(Price $price)
- {
- $this->price = $price;
- }
- }
-
-
- Doctrine will automatically create the columns from the Price
class into the table of the Product
class. By default, it prefixes the database columns after the Embeddable
class name, in this case: price_amount
and price_currency
.
Value objects are useful for writing clean code. Instead of writing:
- -public function addPhoneNumber(string $phone): void {}
-
-
- You can write:
- -public function addPhoneNumber(PhoneNumber $phone): void {}
-
-
- Which makes it easy to read and reason about it, also you don’t need to figure out which phone format you should use.
- -Since their attributes define them, and you can share them with other different entities, they can be cacheable forever.
- -They can help you to reduce duplication. Instead of having multiples amount
and currency
fields, you can use a pure Price
class.
Of course, like everything in life, you should not abuse of value objects. Imagine you converting tons of objects to primitive values to store them in the database, or converting those back to value objects when fetching them from the database.Indeed, you can have performance issues. Also, having tons of granular value objects can bloat your codebase.
- -With value objects, you can reduce the primitive obsession. Use them to represent a field or group of fields of your domain that require validation or can cause ambiguity if you use primitive values.
- -Thanks for reading, and happy coding!
- -