Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input validation #636

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Pyz/Zed/LeapYear/Business/Checker/LeapYearChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear\Business\Checker;

class LeapYearChecker
{
public function checkLeapYear($year)
{
return ($year % 4 === 0 && ($year % 100 !== 0 || $year % 400 === 0));
}
}
24 changes: 24 additions & 0 deletions src/Pyz/Zed/LeapYear/Business/LeapYearBusinessFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear\Business;

use Pyz\Zed\LeapYear\Business\Checker\LeapYearChecker;
use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;

/**
* @method \Pyz\Zed\LeapYear\LeapYearConfig getConfig()
*/
class LeapYearBusinessFactory extends AbstractBusinessFactory
{
public function createLeapYearChecker()
{
return new LeapYearChecker();
}
}
30 changes: 30 additions & 0 deletions src/Pyz/Zed/LeapYear/Business/LeapYearFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear\Business;

use Spryker\Zed\Kernel\Business\AbstractFacade;

/**
* @method \Pyz\Zed\LeapYear\Business\LeapYearBusinessFactory getFactory()
*/
class LeapYearFacade extends AbstractFacade implements LeapYearFacadeInterface
{
/**
* Specification:
* Converts any roman number from 1 to 3999 to integer
*
* @param $year
* @return bool
*/
public function checkLeapYear($year): bool
{
return $this->getFactory()->createLeapYearChecker()->checkLeapYear($year);
}
}
15 changes: 15 additions & 0 deletions src/Pyz/Zed/LeapYear/Business/LeapYearFacadeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear\Business;

interface LeapYearFacadeInterface
{
public function checkLeapYear($year): bool;
}
16 changes: 16 additions & 0 deletions src/Pyz/Zed/LeapYear/LeapYearConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear;

use Spryker\Zed\Kernel\AbstractBundleConfig;

class LeapYearConfig extends AbstractBundleConfig
{
}
16 changes: 16 additions & 0 deletions src/Pyz/Zed/LeapYear/LeapYearDependencyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\LeapYear;

use Spryker\Zed\Kernel\AbstractBundleDependencyProvider;

class LeapYearDependencyProvider extends AbstractBundleDependencyProvider
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers\Business\Converter;

use function PHPUnit\Framework\throwException;
use Pyz\Zed\RomanNumbers\Business\Exception\NotARomanNumberException;
class RomanNumbersConverter
{
private const NUMBERS = [
'I' => 1,
'V' => 5,
'X' => 10,
'L' => 50,
'C' => 100,
'D' => 500,
'M' => 1000,
];

/**
* @param string $romanNumbers
* @return int
* @throws NotARomanNumberException
*/
public function convert(string $romanNumbers): int
{
if (!$this->isValidRomanNumber($romanNumbers)) {
throw new NotARomanNumberException($romanNumbers);
}

$romanNumbers = str_replace('IV', 'IIII', $romanNumbers);
$romanNumbers = str_replace('IX', 'VIIII', $romanNumbers);
$romanNumbers = str_replace('XL', 'XXXX', $romanNumbers);
$romanNumbers = str_replace('XC', 'LXXXX', $romanNumbers);
$romanNumbers = str_replace('CD', 'CCCC', $romanNumbers);
$romanNumbers = str_replace('CM', 'DCCCC', $romanNumbers);
$romanNumbers = str_replace('XD', 'CCCCLXXXX', $romanNumbers);

$result = 0;
$result += substr_count($romanNumbers, 'I');
$result += substr_count($romanNumbers, 'V') * 5;
$result += substr_count($romanNumbers, 'X') * 10;
$result += substr_count($romanNumbers, 'L') * 50;
$result += substr_count($romanNumbers, 'C') * 100;
$result += substr_count($romanNumbers, 'D') * 500;
$result += substr_count($romanNumbers, 'M') * 1000;

return $result;
}

/**
* @param string $romanNumbers
* @return bool
*/
private function isValidRomanNumber(string $romanNumbers): bool
{
if (empty($romanNumbers)) {
return false;
}
$split_romanNumbers = str_split($romanNumbers);
foreach ($split_romanNumbers as $romanNumber) {
$validcharacters = ["I", "V", "X", "L", "C", "D", "M"];
if (!in_array($romanNumber, $validcharacters)) {
return false;
}
}
return true;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers\Business\Exception;

use Exception;

class NotARomanNumberException extends Exception
{
}
24 changes: 24 additions & 0 deletions src/Pyz/Zed/RomanNumbers/Business/RomanNumbersBusinessFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers\Business;

use Pyz\Zed\RomanNumbers\Business\Converter\RomanNumbersConverter;
use Spryker\Zed\Kernel\Business\AbstractBusinessFactory;

/**
* @method \Pyz\Zed\RomanNumbers\RomanNumbersConfig getConfig()
*/
class RomanNumbersBusinessFactory extends AbstractBusinessFactory
{
public function createRomanNumbersConverter()
{
return new RomanNumbersConverter();
}
}
31 changes: 31 additions & 0 deletions src/Pyz/Zed/RomanNumbers/Business/RomanNumbersFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers\Business;

use Spryker\Zed\Kernel\Business\AbstractFacade;

/**
* @method \Pyz\Zed\RomanNumbers\Business\RomanNumbersBusinessFactory getFactory()
*/
class RomanNumbersFacade extends AbstractFacade implements RomanNumbersFacadeInterface
{
/**
* Specification:
* Converts any roman number from 1 to 3999 to integer
*
* @param string $romanNumbers
*
* @return int
*/
public function convertRomanToInteger(string $romanNumber): int
{
return $this->getFactory()->createRomanNumbersConverter()->convert($romanNumber);
}
}
15 changes: 15 additions & 0 deletions src/Pyz/Zed/RomanNumbers/Business/RomanNumbersFacadeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers\Business;

interface RomanNumbersFacadeInterface
{
public function convertRomanToInteger(string $romanNumber): int;
}
16 changes: 16 additions & 0 deletions src/Pyz/Zed/RomanNumbers/RomanNumbersConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers;

use Spryker\Zed\Kernel\AbstractBundleConfig;

class RomanNumbersConfig extends AbstractBundleConfig
{
}
55 changes: 55 additions & 0 deletions src/Pyz/Zed/RomanNumbers/RomanNumbersDependencyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* This file is part of the Spryker Commerce OS.
* For full license information, please view the LICENSE file that was distributed with this source code.
*/

declare(strict_types = 1);

namespace Pyz\Zed\RomanNumbers;

use Spryker\Zed\Kernel\AbstractBundleDependencyProvider;
use Spryker\Zed\Kernel\Container;

/**
* @method \Pyz\Zed\RomanNumbers\RomanNumbersConfig getConfig()
*/
class RomanNumbersDependencyProvider extends AbstractBundleDependencyProvider
{
/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
public function provideCommunicationLayerDependencies(Container $container): Container
{
$container = parent::provideCommunicationLayerDependencies($container);

return $container;
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
public function provideBusinessLayerDependencies(Container $container): Container
{
$container = parent::provideBusinessLayerDependencies($container);

return $container;
}

/**
* @param \Spryker\Zed\Kernel\Container $container
*
* @return \Spryker\Zed\Kernel\Container
*/
public function providePersistenceLayerDependencies(Container $container): Container
{
$container = parent::providePersistenceLayerDependencies($container);

return $container;
}
}
Loading
Loading