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

Discuss: can i use typhoon as helper for auto typecast tool for work with rest-api? #31

Open
mesilov opened this issue Mar 15, 2024 · 2 comments

Comments

@mesilov
Copy link

mesilov commented Mar 15, 2024

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 
    ]
);
@vudaltsov
Copy link
Member

We're planning to add phpDoc properties in #8 and Typhoon Hydrator soon. Will that help?

@mesilov
Copy link
Author

mesilov commented Mar 16, 2024

But first of all I interested about my idea about use typhoon in my scenario, it's relevant tool?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants