Skip to content

Commit

Permalink
Merge branch 'master' of github.com:apioo/psx-schema into 6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Oct 9, 2024
2 parents b04c3f3 + f678247 commit 8bf2fe0
Show file tree
Hide file tree
Showing 52 changed files with 651 additions and 818 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
Expand All @@ -24,12 +24,11 @@ jobs:
strategy:
matrix:
php-versions:
- 8.1
- 8.2
- 8.3
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
Expand Down
46 changes: 0 additions & 46 deletions bin/schema

This file was deleted.

3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@
"require-dev": {
"phpunit/phpunit": "^9.0",
"symfony/yaml": "^5.0|^6.0|^7.0",
"vimeo/psalm": "^4.0"
"vimeo/psalm": "^5.0"
},
"bin": ["bin/schema"],
"autoload": {
"psr-4": {
"PSX\\Schema\\": "src/"
Expand Down
112 changes: 112 additions & 0 deletions src/ContentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Schema;

/**
* Describes a content type
*
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
class ContentType implements \Stringable, \JsonSerializable
{
public const BINARY = 'application/octet-stream';
public const FORM = 'application/x-www-form-urlencoded';
public const JSON = 'application/json';
public const MULTIPART = 'multipart/form-data';
public const TEXT = 'text/plain';
public const XML = 'application/xml';

private string $type;

public function __construct(string $contentType)
{
$this->type = strtolower($contentType);
}

/**
* The shape is a specific content type which represents a complete group of content types
* i.e. the shape of the content type "application/vnd.github.raw+json" is "application/json"
* based on the shape we generate specific client or server code
*/
public function getShape(): ?string
{
if ($this->isJson()) {
return self::JSON;
} elseif ($this->isXml()) {
return self::XML;
} elseif ($this->isForm()) {
return self::FORM;
} elseif ($this->isMultipart()) {
return self::MULTIPART;
} elseif ($this->isText()) {
return self::TEXT;
} else {
return self::BINARY;
}
}

public function toString(): string
{
return $this->type;
}

public function __toString(): string
{
return $this->type;
}

public function jsonSerialize(): mixed
{
return $this->type;
}

private function isForm(): bool
{
return $this->type === 'application/x-www-form-urlencoded';
}

private function isMultipart(): bool
{
return $this->type === 'multipart/form-data';
}

private function isText(): bool
{
return str_starts_with($this->type, 'text/');
}

private function isJson(): bool
{
return $this->type === 'application/json' || str_ends_with($this->type, '+json');
}

private function isXml(): bool
{
return $this->type === 'application/xml' || $this->type === 'text/xml' || str_ends_with($this->type, '+xml');
}

public static function from(string $value): static
{
return new static($value);
}
}
17 changes: 2 additions & 15 deletions src/Exception/TypeNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,8 @@
*/
class TypeNotFoundException extends \Exception
{
/**
* @var string
*/
private $namespace;

/**
* @var string
*/
private $name;
private string $namespace;
private string $name;

public function __construct(string $message, string $namespace, string $name, $code = 0, Throwable $previous = null)
{
Expand All @@ -49,17 +42,11 @@ public function __construct(string $message, string $namespace, string $name, $c
$this->name = $name;
}

/**
* @return string
*/
public function getNamespace(): string
{
return $this->namespace;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
Expand Down
24 changes: 2 additions & 22 deletions src/Exception/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,9 @@
*/
class ValidationException extends \Exception
{
/**
* @var string
*/
protected $keyword;
protected string $keyword;
protected array $path;

/**
* @var array
*/
protected $path;

/**
* @param string $message
* @param string $keyword
* @param array $path
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $message, string $keyword, array $path, int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
Expand All @@ -58,17 +44,11 @@ public function __construct(string $message, string $keyword, array $path, int $
$this->path = $path;
}

/**
* @return string
*/
public function getKeyword(): string
{
return $this->keyword;
}

/**
* @return array
*/
public function getPath(): array
{
return $this->path;
Expand Down
17 changes: 17 additions & 0 deletions src/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ enum Format: string
*/
case BINARY = 'base64';

/**
* An ISO 3166-1 alpha-2 country code
*/
case COUNTRY = 'country';

/**
* An 3-letter ISO 4217 currency name
*/
case CURRENCY = 'currency';

/**
* A date without a time-zone in the ISO-8601 calendar system, such as '2007-12-03'
*
Expand Down Expand Up @@ -122,6 +132,13 @@ enum Format: string
*/
case URN = 'urn';

/**
* An ulid string, such as '01ARZ3NDEKTSV4RRFFQ69G5FAV'
*
* @see https://github.com/ulid/spec
*/
case ULID = 'ulid';

/**
* An uuid string, such as 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6'
*
Expand Down
4 changes: 3 additions & 1 deletion src/Generator/Code/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*
* @template-extends \ArrayObject<string, string>
*/
class Arguments extends \ArrayObject
{
}
}
2 changes: 2 additions & 0 deletions src/Generator/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* @author Christoph Kappestein <[email protected]>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*
* @template-extends Record<mixed>
*/
class Config extends Record
{
Expand Down
26 changes: 0 additions & 26 deletions src/Generator/Go.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,6 @@ protected function writeHeader(TypeAbstract $origin, Code\Name $className): stri
$code.= 'package ' . $this->namespace . "\n";
}

$imports = $this->getImports($origin);
if (!empty($imports)) {
$code.= "\n";
$code.= implode("\n", $imports);
$code.= "\n";
}

$code.= "\n";

$comment = $origin->getDescription();
Expand All @@ -123,23 +116,4 @@ protected function writeHeader(TypeAbstract $origin, Code\Name $className): stri

return $code;
}

private function getImports(TypeAbstract $origin): array
{
$imports = [];

if (TypeUtil::contains($origin, StringType::class, Format::DATE)) {
$imports[] = 'import "time"';
} elseif (TypeUtil::contains($origin, StringType::class, Format::DATETIME)) {
$imports[] = 'import "time"';
} elseif (TypeUtil::contains($origin, StringType::class, Format::DURATION)) {
$imports[] = 'import "time"';
} elseif (TypeUtil::contains($origin, StringType::class, Format::PERIOD)) {
$imports[] = 'import "time"';
} elseif (TypeUtil::contains($origin, StringType::class, Format::TIME)) {
$imports[] = 'import "time"';
}

return $imports;
}
}
Loading

0 comments on commit 8bf2fe0

Please sign in to comment.