Skip to content
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
40 changes: 40 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Continuous integration

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
tests:
runs-on: ubuntu-22.04
strategy:
max-parallel: 3
matrix:
php: [ 8.2, 8.3 ]
composer_flags: [ "", "--prefer-lowest" ]
name: PHP ${{ matrix.php }} ${{ matrix.composer_flags}}
env:
PHP: ${{ matrix.os }}
COMPOSER_MEMORY_LIMIT: -1
COMPOSER_FLAGS: ${{ matrix.composer_flags }}
PHP_VERSION: ${{ matrix.php }}
steps:
- uses: actions/checkout@v2
- name: Install PHP
uses: shivammathur/setup-php@master
with:
php-version: ${{ matrix.php }}
extensions: xdebug
- name: Install dependencies
run: |
composer self-update
COMPOSER_MEMORY_LIMIT=-1 composer update --prefer-dist --no-interaction $COMPOSER_FLAGS
composer require doctrine/dbal
- name: Execute tests (Unit and Feature)
run: vendor/bin/phpunit --coverage-text
- name: Static analysis with PHPStan
run: vendor/bin/phpstan analyse
- name: Coding style PSR12 Check
run: vendor/bin/phpcs
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
clover.xml
html/
vendor/*
composer.lock
.phpunit.result.cache
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2022, Joshua Thijssen

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
Auto-generated README for icore-php-typearray
# Typed Array

Simple wrapper around property-accessor to fetch elements in a typed way. This package is created because I was fiddling around with a lot of json-arrays
that were not typed and phpstan had a big issue with that.

So, instead of having `mixed[] $myarray`, we can use `TypeArray`, and fetch elements with:

```
$arr = new TypeArray(['foo' => 'string', 'bar' => 4 ]);

$arr->getString('[foo]'); // always returns a string
$arr->getString('[notexists]', 'defaultval'); // Returns default val when not found

$arr->getInt('[bar]'); // returns int(4)
$arr->getInt('[foo]'); // Returns InvalidTypeException as this element is a string, not an int

```

See symfony propertyaccess component for the path syntax used.
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "minvws/icore-php-typearray",
"description": "Typed Array",
"type": "library",
"require": {
"php": ">=8.0.2",
"symfony/property-access": "^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6",
"phpstan/phpstan": "^1.0"
},
"suggest": {
"doctrine/dbal": "For using type_array directly with Doctrine"
},
"license": "BSD-3-Clause",
"autoload": {
"psr-4": {
"Jaytaph\\TypeArray\\": "src/"
}
},
"authors": [
{
"name": "Joshua Thijssen",
"email": "jthijssen@noxlogic.nl"
}
],
"scripts": {
"test": [
"vendor/bin/phpcs",
"vendor/bin/phpstan",
"vendor/bin/phpunit"
]
}
}
9 changes: 9 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<ruleset>
<arg name="basepath" value="."/>

<file>./src</file>
<file>./tests</file>

<rule ref="PSR12" />
</ruleset>
7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
paths:
- src
level: 8
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="TypeArray Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
23 changes: 23 additions & 0 deletions phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "true"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false">

<testsuites>
<testsuite name="TypeArray Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
</phpunit>
97 changes: 97 additions & 0 deletions src/Doctrine/DBAL/Types/TypeArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

declare(strict_types=1);

namespace Jaytaph\TypeArray\Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use Doctrine\Deprecations\Deprecation;
use JsonException;
use Jaytaph\TypeArray\TypeArray;

use function is_resource;
use function json_encode;
use function stream_get_contents;

use const JSON_PRESERVE_ZERO_FRACTION;
use const JSON_THROW_ON_ERROR;

class TypeArrayType extends Type
{
public const TYPE = 'type_array';

/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)

Check failure on line 28 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::getSQLDeclaration() has no return type specified.

Check failure on line 28 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Return type mixed of method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::getSQLDeclaration() is not covariant with return type string of method Doctrine\DBAL\Types\Type::getSQLDeclaration().
{
return $platform->getJsonTypeDeclarationSQL($column);
}

/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)

Check failure on line 36 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Return type mixed of method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::convertToDatabaseValue() is not covariant with return type mixed of method Doctrine\DBAL\Types\Type::convertToDatabaseValue().
{
if ($value === null) {
return null;
}

if (! $value instanceof TypeArray) {
throw ConversionException::conversionFailedInvalidType($value, self::TYPE, ['null', TypeArray::class]);

Check failure on line 43 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailedInvalidType().
}

try {
return json_encode($value->toArray(), JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
} catch (JsonException $e) {
throw ConversionException::conversionFailedSerialization($value, self::TYPE, $e->getMessage(), $e);

Check failure on line 49 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailedSerialization().
}
}

/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)

Check failure on line 56 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Return type mixed of method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::convertToPHPValue() is not covariant with return type mixed of method Doctrine\DBAL\Types\Type::convertToPHPValue().
{
if ($value === null || $value === '') {
return null;
}

if (is_resource($value)) {
$value = stream_get_contents($value);
}

try {
return TypeArray::fromJson(strval($value));
} catch (JsonException $e) {
throw ConversionException::conversionFailed($value, $this->getName(), $e);

Check failure on line 69 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailed().
}
}

/**
* {@inheritdoc}
*/
public function getName()

Check failure on line 76 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::getName() has no return type specified.
{
return self::TYPE;
}

/**
* {@inheritdoc}
*
* @deprecated
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)

Check failure on line 86 in src/Doctrine/DBAL/Types/TypeArrayType.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Method Jaytaph\TypeArray\Doctrine\DBAL\Types\TypeArrayType::requiresSQLCommentHint() has no return type specified.
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/dbal',
'https://github.com/doctrine/dbal/pull/5509',
'%s is deprecated.',
__METHOD__,
);

return true;
}
}
13 changes: 13 additions & 0 deletions src/Exception/IncorrectDataTypeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Jaytaph\TypeArray\Exception;

class IncorrectDataTypeException extends \Exception
{
public function __construct(string $want, string $have)
{
parent::__construct(sprintf('Incorrect data type. Want: %s, have: %s', $want, $have));
}
}
13 changes: 13 additions & 0 deletions src/Exception/InvalidIndexException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Jaytaph\TypeArray\Exception;

class InvalidIndexException extends \Exception
{
public function __construct(string $path)
{
parent::__construct(sprintf('Invalid index: "%s"', $path));
}
}
Loading
Loading