-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add self validating Value Objects (#16725)
* Add some Basic Value Objects. - ArrayOfBoolean - ArrayOfIntegers - PositiveInteger - ArrayOfPositiveIntegers - ArrayOfStrings - Path * Update Upgrade nodes.
- Loading branch information
1 parent
7826532
commit 7f779c3
Showing
13 changed files
with
771 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.