typhoon
typhoon copied to clipboard
Discuss: can i use typhoon as helper for auto typecast tool for work with rest-api?
AS IS I work with REST-API via SDK-client, and they return results as an immutable object
<?php
declare(strict_types=1);
namespace Bitrix24\SDK\Core\Result;
use ArrayIterator;
use Bitrix24\SDK\Core\Exceptions\ImmutableResultViolationException;
use IteratorAggregate;
use Traversable;
abstract class AbstractItem implements IteratorAggregate
{
protected array $data;
/**
* AbstractItem constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
$this->data = $data;
}
/**
* @param int|string $offset
*
* @return bool
*/
public function __isset($offset): bool
{
return isset($this->data[$offset]);
}
/**
* @param int|string $offset
*
* @return mixed
*/
public function __get($offset)
{
return $this->data[$offset] ?? null;
}
/**
* @param int|string $offset
* @param mixed $value
*
* @return void
* @throws ImmutableResultViolationException
*
*/
public function __set($offset, $value)
{
throw new ImmutableResultViolationException(sprintf('Result is immutable, violation at offset %s', $offset));
}
/**
* @param int|string $offset
*
* @throws ImmutableResultViolationException
*/
public function __unset($offset)
{
throw new ImmutableResultViolationException(sprintf('Result is immutable, violation at offset %s', $offset));
}
/**
* {@inheritdoc}
*/
public function getIterator():Traversable
{
return new ArrayIterator($this->data);
}
/**
* @param string $key
*
* @return bool
*/
protected function isKeyExists(string $key): bool
{
return array_key_exists($key, $this->data);
}
}
for each result object type I wrote php-doc annotations
<?php
declare(strict_types=1);
namespace Bitrix24\SDK\Services\CRM\Deal\Result;
use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
use DateTimeImmutable;
/**
* Class DealItemResult
*
* @property int $ID
* @property DateTimeImmutable $CREATED_DATE
* @property string $NAME
* @property bool $IS_LOCKED
* @property int $SORT
* ...
* + 50 properties
*/
class DealCategoryItemResult extends AbstractCrmItem
{
}
in class AbstractCrmItem
I write large swich-case with typcast for fields like us @property DateTimeImmutable $CREATED_DATE
Question 1: Can I use typhoon for read annotations and write more smart and automatic typecast code?
In some methods such us add \ update I send arrays like this:
$b24Service->getCRMScope()->deal()->update(
$deailId,
[
'START_DATE' => (new DateTime())->format(DateTime::ATOM), // i can't pass DateTime objects
'IS_PRIVATE' => 'Y', // i can't pass boolean variables
'PRICE' => '245.59' // i can't pass phpmoney/money objects
]
);
Question 2: if i already have information about types and properties in DealCategoryItemResult
annotation can I use this info to typecast array values like us in this example automatically?
$b24Service->getCRMScope()->deal()->update(
$deailId,
[
'START_DATE' => new DateTime(), // magic typecast underhood
'IS_PRIVATE' => $isPrivate, // magic typecast underhood
'PRICE' => $dealPrice // magic typecast underhood
]
);
We're planning to add phpDoc properties in https://github.com/typhoon-php/typhoon/issues/8 and Typhoon Hydrator soon. Will that help?
But first of all I interested about my idea about use typhoon in my scenario, it's relevant tool?