From 3a6cae68a40dfdb692926591748c022ea1f715cd Mon Sep 17 00:00:00 2001 From: Max Podkosov Date: Sun, 28 Feb 2021 18:02:16 +0300 Subject: [PATCH] =?UTF-8?q?#39=20=D0=A1=D0=BA=D0=B0=D1=87=D0=B8=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Components/Fields/AbstractFieldAccessor.php | 9 ++++++++- src/Components/Fields/EntityFields.php | 6 +++--- src/Components/Fields/EntityRelation.php | 4 ++-- src/Components/Fields/ImageField.php | 8 ++++++-- src/Components/Http/MoySkladHttpClient.php | 14 ++++++++++++++ src/Entities/AbstractEntity.php | 10 +++++----- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Components/Fields/AbstractFieldAccessor.php b/src/Components/Fields/AbstractFieldAccessor.php index 4374677..0ad51f4 100644 --- a/src/Components/Fields/AbstractFieldAccessor.php +++ b/src/Components/Fields/AbstractFieldAccessor.php @@ -2,6 +2,8 @@ namespace MoySklad\Components\Fields; +use MoySklad\Entities\AbstractEntity; + /** * Class for storing different fields * Class AbstractFieldAccessor @@ -9,9 +11,14 @@ */ abstract class AbstractFieldAccessor implements \JsonSerializable { protected $storage; + /** + * @var AbstractEntity + */ + protected $e; - public function __construct($fields) + public function __construct($fields, AbstractEntity &$entity = null) { + $this->e = $entity; $this->storage = new \stdClass(); $this->replace($fields); } diff --git a/src/Components/Fields/EntityFields.php b/src/Components/Fields/EntityFields.php index 2539d27..2ff614f 100644 --- a/src/Components/Fields/EntityFields.php +++ b/src/Components/Fields/EntityFields.php @@ -18,13 +18,13 @@ public function replace($fields) foreach ( $fields as $fieldName => $field ){ switch ( $fieldName ){ case "meta": - $this->storage->meta = new MetaField($field); + $this->storage->meta = new MetaField($field, $this->e); break; case "attributes": - $this->storage->attributes = new AttributeCollection($field); + $this->storage->attributes = new AttributeCollection($field, $this->e); break; case "image": - $this->storage->image = new ImageField($field); + $this->storage->image = new ImageField($field, $this->e); break; default: $this->storage->{$fieldName} = $field; diff --git a/src/Components/Fields/EntityRelation.php b/src/Components/Fields/EntityRelation.php index 55db66b..eb908d3 100644 --- a/src/Components/Fields/EntityRelation.php +++ b/src/Components/Fields/EntityRelation.php @@ -13,9 +13,9 @@ class EntityRelation extends AbstractFieldAccessor { private $relatedByClass = null; - public function __construct($fields, $relatedByClass) + public function __construct($fields, $relatedByClass, AbstractEntity &$entity = null) { - parent::__construct($fields); + parent::__construct($fields, $entity); $this->relatedByClass = $relatedByClass; } diff --git a/src/Components/Fields/ImageField.php b/src/Components/Fields/ImageField.php index 28073b0..92ac53f 100644 --- a/src/Components/Fields/ImageField.php +++ b/src/Components/Fields/ImageField.php @@ -70,10 +70,14 @@ public function getDownloadLink($size = 'normal'){ * @throws \Exception */ public function download($saveFile, $size = 'normal'){ - if ( $link = $this->getDownloadLink($size) ){ + if ($link = $this->getDownloadLink($size)) { $filePath = fopen($saveFile,'w+'); $client = new Client(); - $response = $client->get($link, [RequestOptions::SINK => $filePath]); + + $response = $this->e->getSkladInstance()->getClient()->getRaw( + $link, [RequestOptions::SINK => $filePath] + ); + return $response->getStatusCode(); } throw new \Exception("Image does not have a $size size link. Try to refresh hosting entity"); diff --git a/src/Components/Http/MoySkladHttpClient.php b/src/Components/Http/MoySkladHttpClient.php index 3e8fdb1..d42b418 100644 --- a/src/Components/Http/MoySkladHttpClient.php +++ b/src/Components/Http/MoySkladHttpClient.php @@ -101,6 +101,20 @@ public function delete($method, $payload = [], $options = null){ ); } + /** + * @param $link + * @param $options + * @return \Psr\Http\Message\ResponseInterface + */ + public function getRaw($link, $options) { + if (empty($options['headers']['Authorization'])) { + $options['headers']['Authorization'] = "Basic " . base64_encode($this->login . ':' . $this->password); + } + + $client = new Client(); + return $client->get($link, $options); + } + public function getLastRequest(){ return RequestLog::getLast(); } diff --git a/src/Entities/AbstractEntity.php b/src/Entities/AbstractEntity.php index 7d3ebb4..7b45779 100644 --- a/src/Entities/AbstractEntity.php +++ b/src/Entities/AbstractEntity.php @@ -80,9 +80,9 @@ public function __construct(MoySklad $skladInstance, $fields = [], ConstructionS { if ( !$specs ) $specs = ConstructionSpecs::create(); if ( is_array($fields) === false && is_object($fields) === false) $fields = [$fields]; - $this->fields = new EntityFields($fields); - $this->links = new EntityLinker([]); - $this->relations = new EntityRelation([], static::class); + $this->fields = new EntityFields($fields, $this); + $this->links = new EntityLinker([], $this); + $this->relations = new EntityRelation([], static::class, $this); $this->skladHashCode = $skladInstance->hashCode(); $this->processConstructionSpecs($specs); } @@ -156,8 +156,8 @@ public function fresh(Expand $expand = null){ * @return $this */ public function replaceFields(AbstractEntity $entity){ - $this->fields = new EntityFields($entity->fields); - $this->relations = new EntityRelation($entity->relations, get_class($this)); + $this->fields = new EntityFields($entity->fields, $this); + $this->relations = new EntityRelation($entity->relations, get_class($this), $this); return $this; }