diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 8196172..248a34c 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -11,12 +11,19 @@ permissions: jobs: build: - runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] steps: - uses: actions/checkout@v3 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + - name: Validate composer.json and composer.lock run: composer validate --strict diff --git a/composer.json b/composer.json index 31ccc06..5b92ae4 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require-dev": { "symfony/console": "^4.4", - "phpunit/phpunit": "^8.5", + "phpunit/phpunit": "^8.5 || ^9.0 || ^10.0", "phpstan/phpstan": "^1.8", "squizlabs/php_codesniffer": "^3.5", "friendsofphp/php-cs-fixer": "^2.16 || ^3.0", diff --git a/phpcs.xml b/phpcs.xml index 017f8e7..84425db 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -9,7 +9,6 @@ - diff --git a/src/Data/DataObject.php b/src/Data/DataObject.php index 9338f43..46b90a0 100644 --- a/src/Data/DataObject.php +++ b/src/Data/DataObject.php @@ -42,7 +42,7 @@ class DataObject extends RecursiveArrayIterator implements DataInterface /** * Constructor * - * @param array|object $obj The object to use + * @param array|object $data The object to use */ public function __construct($data) { @@ -53,6 +53,10 @@ public function __construct($data) /** * Init data object + * + * @param string $path + * + * @return DataPath */ public function pathInit(string $path): DataPath { @@ -93,7 +97,7 @@ public function cache(string $path, $value): void * * @return mixed */ - public function get(string $path) + public function get(?string $path = null) { if ($this->cache->isCached($path)) { return $this->cache->get($path); @@ -174,10 +178,6 @@ public function set(string $path, $value): void // Returned DataPath object $path_ = $this->pathInit($path); - if (!$path_) { - return; - } - // Add given value to the cache $this->cache($path, $value); @@ -312,6 +312,16 @@ public function key(): string */ public function has(string $path): bool { - return in_array($path, $this->paths, true); + return array_key_exists($path, $this->paths); + } + + /** + * @return DataObject|null + */ + #[\ReturnTypeWillChange] + public function getChildren() + { + /** @var DataObject|null */ + return parent::getChildren(); } } diff --git a/src/Data/DataPath.php b/src/Data/DataPath.php index 6e82f44..bd145c8 100644 --- a/src/Data/DataPath.php +++ b/src/Data/DataPath.php @@ -49,10 +49,9 @@ public function __construct(string $path) } /** - * Inıt path + * Init path * - * @param string $path - * @param DataInterface $data + * @param string $path * * @return DataPath */ diff --git a/src/Data/Validation.php b/src/Data/Validation.php index 0c98aeb..3271444 100644 --- a/src/Data/Validation.php +++ b/src/Data/Validation.php @@ -142,7 +142,7 @@ public function setRules(array $rules): void * Checks whether the value which is in the desired path * and added to the control list is valid or not * - * @param string $path requested path + * @param string $path requested path * * @return bool */ @@ -167,8 +167,10 @@ public function isValid(string $path = ''): bool /** * Adds new validation error status to the validationStatus array * - * @param string $path requested path - * @param bool $status validation status + * @param string $path requested path + * @param mixed $value Data value + * @param string $pattern Validation pattern + * @param bool $required Is value required * * @return bool */ @@ -183,10 +185,10 @@ public function setValidationStatus(string $path, $value, string $pattern, bool /** * Sets the status to the all parent paths. * - * @param string $path Data path - * @param mixed $value Data value - * @param string $pattern Validation pattern - * @param bool $status Is value required + * @param string $path Data path + * @param mixed $value Data value + * @param string $pattern Validation pattern + * @param bool $required Is value required */ public function addValidationStatus(string $path, $value, string $pattern, bool $required): void { diff --git a/src/Helpers/DataParsers.php b/src/Helpers/DataParsers.php index 94a376d..fe5a38c 100644 --- a/src/Helpers/DataParsers.php +++ b/src/Helpers/DataParsers.php @@ -52,8 +52,9 @@ public function findPaths(string $path, array $data, ?Closure $closure = null): $paths = []; foreach ($data as $key => $val) { - if (substr_count($path, "*") > 0) { - $path_ = substr_replace($path, $key, strpos($path, "*"), 1); + $pos = strpos($path, "*"); + if ($pos !== false) { + $path_ = substr_replace($path, (string)$key, $pos, 1); if ($closure) { $val = $closure($path_, $val); diff --git a/src/Middleware.php b/src/Middleware.php index 61603ce..bce196b 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -30,7 +30,7 @@ class Middleware * * @var array */ - private array $_options = []; + private array $options = []; /** * __construct function @@ -39,7 +39,7 @@ class Middleware */ public function __construct(array $options = []) { - $this->_options = $options; + $this->options = $options; } /** @@ -52,7 +52,7 @@ public function __construct(array $options = []) */ public function set(string $name, $value): void { - $this->_options[$name] = $value; + $this->options[$name] = $value; } /** @@ -64,7 +64,7 @@ public function set(string $name, $value): void */ public function get(string $name) { - return $this->_options[$name] ?? null; + return $this->options[$name] ?? null; } /** diff --git a/src/StringObjects.php b/src/StringObjects.php index 7ff997a..a7367bd 100644 --- a/src/StringObjects.php +++ b/src/StringObjects.php @@ -36,28 +36,28 @@ class StringObjects * * @var DataObject */ - private DataObject $_obj; + private DataObject $obj; /** * Validation object * * @var Validation */ - private Validation $_validation; + private Validation $validation; /** * Middleware object * * @var Middleware */ - private Middleware $_middleware; + private Middleware $middleware; /** * Filters object * * @var DataFilters */ - private DataFilters $_filters; + private DataFilters $filters; /** * Constructor @@ -65,22 +65,22 @@ class StringObjects * @param object $obj The object to use * @param array $options Options */ - public function __construct(object $obj, array $options = []) + final public function __construct(object $obj, array $options = []) { - $this->_obj = new DataObject($obj); + $this->obj = new DataObject($obj); if (isset($options['middleware'])) { - $this->_middleware = new Middleware($options['middleware']); - $this->_middleware->memoryLeakProtection(); + $this->middleware = new Middleware($options['middleware']); + $this->middleware->memoryLeakProtection(); } if (isset($options['validation'])) { - $this->_validation = new Validation($this->_obj, $options['validation']); - $this->_validation->validate(); + $this->validation = new Validation($this->obj, $options['validation']); + $this->validation->validate(); } if (isset($options['filters'])) { - $this->_filters = new DataFilters($options['filters']); + $this->filters = new DataFilters($options['filters']); } } @@ -90,7 +90,8 @@ public function __construct(object $obj, array $options = []) * @param mixed $data The mixed type of object data to use * @param array $options Options * - * @return static|bool + * @return static + * @throws Exception */ public static function instance($data, array $options = []) { @@ -114,7 +115,7 @@ public static function instance($data, array $options = []) throw new Exception("Input data is not a valid object!\r\n" . print_r($data, true), 23); } - return new self($data, $options); + return new static($data, $options); } /** @@ -129,14 +130,14 @@ public static function instance($data, array $options = []) */ public function get(?string $path = '', $default = false) { - $result = $this->_obj->get($path); + $result = $this->obj->get($path); if ($result === false) { return $default; } - if (isset($this->_filters)) { - $result = $this->_filters->filter($path, $result); + if (isset($this->filters)) { + $result = $this->filters->filter($path, $result); } return $result; @@ -153,7 +154,7 @@ public function get(?string $path = '', $default = false) */ public function set(string $path, $value): void { - $this->_obj->set($path, $value); + $this->obj->set($path, $value); } /** @@ -166,7 +167,7 @@ public function set(string $path, $value): void */ public function has(string $path): bool { - return $this->_obj->has($path); + return $this->obj->has($path); } /** @@ -176,7 +177,7 @@ public function has(string $path): bool */ public function toJson(): string { - return json_encode($this->_obj); + return json_encode($this->obj); } /** @@ -186,7 +187,7 @@ public function toJson(): string */ public function toArray(): array { - return $this->_obj->toArray(); + return $this->obj->toArray(); } /** @@ -198,7 +199,7 @@ public function toArray(): array */ public function isValid(string $path = ''): bool { - return $this->_validation->isValid($path); + return $this->validation->isValid($path); } /** @@ -210,6 +211,6 @@ public function isValid(string $path = ''): bool */ public function setMemoryLimit(int $memory): void { - $this->_middleware->setMemoryLimit($memory); + $this->middleware->setMemoryLimit($memory); } } diff --git a/tests/TestDataFilters.php b/tests/TestDataFilters.php index 416111a..aae5306 100644 --- a/tests/TestDataFilters.php +++ b/tests/TestDataFilters.php @@ -20,19 +20,9 @@ class TestDataFilters extends TestCase { - /** - * Test Constructor. - */ - public function __construct() - { - parent::__construct(); - } - /** * Fetches the raw JSON data as a string format * - * @param boolean $isArray - * * @return string */ public function getTestData() diff --git a/tests/TestDataObject.php b/tests/TestDataObject.php index c44a0d4..12a089f 100644 --- a/tests/TestDataObject.php +++ b/tests/TestDataObject.php @@ -20,19 +20,9 @@ class TestDataObject extends TestCase { - /** - * Test Constructor. - */ - public function __construct() - { - parent::__construct(); - } - /** * Fetches the raw JSON data as a string format * - * @param boolean $isArray - * * @return string */ public function getTestData() diff --git a/tests/TestValidation.php b/tests/TestValidation.php index 2f48c6c..cfd9649 100644 --- a/tests/TestValidation.php +++ b/tests/TestValidation.php @@ -21,19 +21,9 @@ class TestValidation extends TestCase { - /** - * Test Constructor. - */ - public function __construct() - { - parent::__construct(); - } - /** * Fetches the raw JSON data as a string format * - * @param boolean $isArray - * * @return string */ public function getTestData()