Skip to content

Commit

Permalink
Add self validating Value Objects (#16725)
Browse files Browse the repository at this point in the history
* Add some Basic Value Objects.

 - ArrayOfBoolean
 - ArrayOfIntegers
 - PositiveInteger
 - ArrayOfPositiveIntegers
 - ArrayOfStrings
 - Path

* Update Upgrade nodes.
  • Loading branch information
martineiber committed Mar 5, 2024
1 parent 7826532 commit 7f779c3
Show file tree
Hide file tree
Showing 13 changed files with 771 additions and 0 deletions.
8 changes: 8 additions & 0 deletions doc/23_Installation_and_Upgrade/09_Upgrade_Notes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
- Add rootCallback option to `Pimcore\Navigation\Builder::getNavigation()`
#### [Symfony]
- Bumped Symfony packages to "^6.4".
#### [Value Objects]
- Added new self validating Value Objects:
- `Pimcore\ValueObject\BooleanArray`
- `Pimcore\ValueObject\IntegerArray`
- `Pimcore\ValueObject\Path`
- `Pimcore\ValueObject\PositiveInteger`
- `Pimcore\ValueObject\PositiveIntegerArray`
- `Pimcore\ValueObject\StringArray`

> [!WARNING]
> For [environment variable consistency purposes](https://github.com/pimcore/pimcore/issues/16638) in boostrap, please fix `public/index.php` in project root by moving `Bootstrap::bootstrap();` just above `$kernel = Bootstrap::kernel()` line instead of outside the closure.
Expand Down
63 changes: 63 additions & 0 deletions lib/ValueObject/Collection/ArrayOfBoolean.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


namespace Pimcore\ValueObject\Collection;

use ValueError;

final class ArrayOfBoolean
{

/**
* @throws ValueError
*/
public function __construct(private readonly array $value)
{
$this->validate();
}

/**
* @throws ValueError
*/
public function __wakeup(): void
{
$this->validate();
}

private function validate(): void
{
foreach ($this->value as $value) {
if (!is_bool($value)) {
throw new ValueError(
sprintf(
'Provided array must contain only boolean values. (%s given)',
gettype($value)
),
);
}
}
}

/**
* @return bool[]
*/
public function getValue(): array
{
return $this->value;
}
}
63 changes: 63 additions & 0 deletions lib/ValueObject/Collection/ArrayOfIntegers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types = 1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


namespace Pimcore\ValueObject\Collection;

use ValueError;

final class ArrayOfIntegers
{

/**
* @throws ValueError
*/
public function __construct(private readonly array $value)
{
$this->validate();
}

/**
* @throws ValueError
*/
public function __wakeup(): void
{
$this->validate();
}

private function validate(): void
{
foreach ($this->value as $value) {
if (!is_int($value)) {
throw new ValueError(
sprintf(
'Provided array must contain only integer values. (%s given)',
gettype($value)
),
);
}
}
}

/**
* @return int[]
*/
public function getValue(): array
{
return $this->value;
}
}
72 changes: 72 additions & 0 deletions lib/ValueObject/Collection/ArrayOfPositiveIntegers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types = 1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


namespace Pimcore\ValueObject\Collection;

use ValueError;

final class ArrayOfPositiveIntegers
{

/**
* @throws ValueError
*/
public function __construct(private readonly array $value)
{
$this->validate();
}

/**
* @throws ValueError
*/
public function __wakeup(): void
{
$this->validate();
}

private function validate(): void
{
foreach ($this->value as $value) {
if (!is_int($value)) {
throw new ValueError(
sprintf(
'Provided array must contain only integer values. (%s given)',
gettype($value)
),
);
}

if ($value < 0) {
throw new ValueError(
sprintf(
'Provided integer must be positive. (%s given)',
$value
),
);
}
}
}

/**
* @return int[]
*/
public function getValue(): array
{
return $this->value;
}
}
63 changes: 63 additions & 0 deletions lib/ValueObject/Collection/ArrayOfStrings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


namespace Pimcore\ValueObject\Collection;

use ValueError;

final class ArrayOfStrings
{

/**
* @throws ValueError
*/
public function __construct(private readonly array $value)
{
$this->validate();
}

/**
* @throws ValueError
*/
public function __wakeup(): void
{
$this->validate();
}

private function validate(): void
{
foreach ($this->value as $value) {
if (!is_string($value)) {
throw new ValueError(
sprintf(
'Provided array must contain only string values. (%s given)',
gettype($value)
),
);
}
}
}

/**
* @return string[]
*/
public function getValue(): array
{
return $this->value;
}
}
63 changes: 63 additions & 0 deletions lib/ValueObject/Integer/PositiveInteger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types = 1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/


namespace Pimcore\ValueObject\Integer;

use ValueError;

final class PositiveInteger
{

/**
* @throws ValueError
*/
public function __construct(private readonly int $value)
{
$this->validate();
}

/**
* @throws ValueError
*/
public function __wakeup(): void
{
$this->validate();
}

private function validate(): void
{
if ($this->value < 0) {
throw new ValueError(
sprintf(
'Provided integer must be positive. (%s given)',
$this->value
),
);
}
}

public function getValue(): int
{
return $this->value;
}

public function equals(PositiveInteger $positiveInteger): bool
{
return $this->value === $positiveInteger->getValue();
}
}
Loading

0 comments on commit 7f779c3

Please sign in to comment.