first commit

This commit is contained in:
Mr.Qin
2022-08-19 19:48:37 +08:00
commit afdd648b65
3275 changed files with 631084 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Article.
*/
class Article extends Message
{
/**
* @var string
*/
protected $type = 'mpnews';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'thumb_media_id',
'author',
'title',
'content',
'digest',
'source_url',
'show_cover',
];
/**
* Aliases of attribute.
*
* @var array
*/
protected $jsonAliases = [
'content_source_url' => 'source_url',
'show_cover_pic' => 'show_cover',
];
/**
* @var array
*/
protected $required = [
'thumb_media_id',
'title',
'content',
'show_cover',
];
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Card.php.
*
* @author overtrue <i@overtrue.me>
* @copyright 2015 overtrue <i@overtrue.me>
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Card.
*/
class Card extends Message
{
/**
* Message type.
*
* @var string
*/
protected $type = 'wxcard';
/**
* Properties.
*
* @var array
*/
protected $properties = ['card_id'];
/**
* Media constructor.
*/
public function __construct(string $cardId)
{
parent::__construct(['card_id' => $cardId]);
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class DeviceEvent.
*
* @property string $media_id
*/
class DeviceEvent extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'device_event';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'device_type',
'device_id',
'content',
'session_id',
'open_id',
];
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class DeviceText.
*
* @property string $content
*/
class DeviceText extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'device_text';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'device_type',
'device_id',
'content',
'session_id',
'open_id',
];
public function toXmlArray()
{
return [
'DeviceType' => $this->get('device_type'),
'DeviceID' => $this->get('device_id'),
'SessionID' => $this->get('session_id'),
'Content' => base64_encode($this->get('content')),
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Image.
*
* @property string $media_id
*/
class File extends Media
{
/**
* @var string
*/
protected $type = 'file';
}

View File

@@ -0,0 +1,27 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Image.
*
* @property string $media_id
*/
class Image extends Media
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'image';
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Link.
*/
class Link extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'link';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'url',
];
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Location.
*/
class Location extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'location';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'latitude',
'longitude',
'scale',
'label',
'precision',
];
}

View File

@@ -0,0 +1,66 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
use EasyWeChat\Kernel\Contracts\MediaInterface;
use EasyWeChat\Kernel\Support\Str;
/**
* Class Media.
*/
class Media extends Message implements MediaInterface
{
/**
* Properties.
*
* @var array
*/
protected $properties = ['media_id'];
/**
* @var array
*/
protected $required = [
'media_id',
];
/**
* MaterialClient constructor.
*
* @param string $type
*/
public function __construct(string $mediaId, $type = null, array $attributes = [])
{
parent::__construct(array_merge(['media_id' => $mediaId], $attributes));
!empty($type) && $this->setType($type);
}
/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function getMediaId(): string
{
$this->checkRequiredAttributes();
return $this->get('media_id');
}
public function toXmlArray()
{
return [
Str::studly($this->getType()) => [
'MediaId' => $this->get('media_id'),
],
];
}
}

View File

@@ -0,0 +1,187 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
use EasyWeChat\Kernel\Contracts\MessageInterface;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\XML;
use EasyWeChat\Kernel\Traits\HasAttributes;
/**
* Class Messages.
*/
abstract class Message implements MessageInterface
{
use HasAttributes;
public const TEXT = 2;
public const IMAGE = 4;
public const VOICE = 8;
public const VIDEO = 16;
public const SHORT_VIDEO = 32;
public const LOCATION = 64;
public const LINK = 128;
public const DEVICE_EVENT = 256;
public const DEVICE_TEXT = 512;
public const FILE = 1024;
public const TEXT_CARD = 2048;
public const TRANSFER = 4096;
public const EVENT = 1048576;
public const MINIPROGRAM_PAGE = 2097152;
public const MINIPROGRAM_NOTICE = 4194304;
public const ALL = self::TEXT | self::IMAGE | self::VOICE | self::VIDEO | self::SHORT_VIDEO | self::LOCATION | self::LINK
| self::DEVICE_EVENT | self::DEVICE_TEXT | self::FILE | self::TEXT_CARD | self::TRANSFER | self::EVENT
| self::MINIPROGRAM_PAGE | self::MINIPROGRAM_NOTICE;
/**
* @var string
*/
protected $type;
/**
* @var int
*/
protected $id;
/**
* @var string
*/
protected $to;
/**
* @var string
*/
protected $from;
/**
* @var array
*/
protected $properties = [];
/**
* @var array
*/
protected $jsonAliases = [];
/**
* Message constructor.
*/
public function __construct(array $attributes = [])
{
$this->setAttributes($attributes);
}
/**
* Return type name message.
*/
public function getType(): string
{
return $this->type;
}
public function setType(string $type)
{
$this->type = $type;
}
/**
* Magic getter.
*
* @param string $property
*
* @return mixed
*/
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
return $this->getAttribute($property);
}
/**
* Magic setter.
*
* @param string $property
* @param mixed $value
*
* @return Message
*/
public function __set($property, $value)
{
if (property_exists($this, $property)) {
$this->$property = $value;
} else {
$this->setAttribute($property, $value);
}
return $this;
}
/**
* @return array
*/
public function transformForJsonRequestWithoutType(array $appends = [])
{
return $this->transformForJsonRequest($appends, false);
}
/**
* @param bool $withType
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function transformForJsonRequest(array $appends = [], $withType = true): array
{
if (!$withType) {
return $this->propertiesToArray([], $this->jsonAliases);
}
$messageType = $this->getType();
$data = array_merge(['msgtype' => $messageType], $appends);
$data[$messageType] = array_merge($data[$messageType] ?? [], $this->propertiesToArray([], $this->jsonAliases));
return $data;
}
public function transformToXml(array $appends = [], bool $returnAsArray = false): string
{
$data = array_merge(['MsgType' => $this->getType()], $this->toXmlArray(), $appends);
return $returnAsArray ? $data : XML::build($data);
}
/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
protected function propertiesToArray(array $data, array $aliases = []): array
{
$this->checkRequiredAttributes();
foreach ($this->attributes as $property => $value) {
if (is_null($value) && !$this->isRequired($property)) {
continue;
}
$alias = array_search($property, $aliases, true);
$data[$alias ?: $property] = $this->get($property);
}
return $data;
}
public function toXmlArray()
{
throw new RuntimeException(sprintf('Class "%s" cannot support transform to XML message.', __CLASS__));
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class MiniProgramPage.
*/
class MiniProgramPage extends Message
{
protected $type = 'miniprogrampage';
protected $properties = [
'title',
'appid',
'pagepath',
'thumb_media_id',
];
protected $required = [
'thumb_media_id', 'appid', 'pagepath',
];
}

View File

@@ -0,0 +1,22 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
class MiniprogramNotice extends Message
{
protected $type = 'miniprogram_notice';
protected $properties = [
'appid',
'title',
];
}

View File

@@ -0,0 +1,73 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Music.
*
* @property string $url
* @property string $hq_url
* @property string $title
* @property string $description
* @property string $thumb_media_id
* @property string $format
*/
class Music extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'music';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'url',
'hq_url',
'thumb_media_id',
'format',
];
/**
* Aliases of attribute.
*
* @var array
*/
protected $jsonAliases = [
'musicurl' => 'url',
'hqmusicurl' => 'hq_url',
];
public function toXmlArray()
{
$music = [
'Music' => [
'Title' => $this->get('title'),
'Description' => $this->get('description'),
'MusicUrl' => $this->get('url'),
'HQMusicUrl' => $this->get('hq_url'),
],
];
if ($thumbMediaId = $this->get('thumb_media_id')) {
$music['Music']['ThumbMediaId'] = $thumbMediaId;
}
return $music;
}
}

View File

@@ -0,0 +1,65 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class News.
*
* @author overtrue <i@overtrue.me>
*/
class News extends Message
{
/**
* @var string
*/
protected $type = 'news';
/**
* @var array
*/
protected $properties = [
'items',
];
/**
* News constructor.
*/
public function __construct(array $items = [])
{
parent::__construct(compact('items'));
}
public function propertiesToArray(array $data, array $aliases = []): array
{
return ['articles' => array_map(function ($item) {
if ($item instanceof NewsItem) {
return $item->toJsonArray();
}
}, $this->get('items'))];
}
public function toXmlArray()
{
$items = [];
foreach ($this->get('items') as $item) {
if ($item instanceof NewsItem) {
$items[] = $item->toXmlArray();
}
}
return [
'ArticleCount' => count($items),
'Articles' => $items,
];
}
}

View File

@@ -0,0 +1,57 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class NewsItem.
*/
class NewsItem extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'news';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'url',
'image',
];
public function toJsonArray()
{
return [
'title' => $this->get('title'),
'description' => $this->get('description'),
'url' => $this->get('url'),
'picurl' => $this->get('image'),
];
}
public function toXmlArray()
{
return [
'Title' => $this->get('title'),
'Description' => $this->get('description'),
'Url' => $this->get('url'),
'PicUrl' => $this->get('image'),
];
}
}

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Raw.
*/
class Raw extends Message
{
/**
* @var string
*/
protected $type = 'raw';
/**
* Properties.
*
* @var array
*/
protected $properties = ['content'];
/**
* Constructor.
*/
public function __construct(string $content)
{
parent::__construct(['content' => strval($content)]);
}
/**
* @param bool $withType
*/
public function transformForJsonRequest(array $appends = [], $withType = true): array
{
return json_decode($this->content, true) ?? [];
}
public function __toString()
{
return $this->get('content') ?? '';
}
}

View File

@@ -0,0 +1,30 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class ShortVideo.
*
* @property string $title
* @property string $media_id
* @property string $description
* @property string $thumb_media_id
*/
class ShortVideo extends Video
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'shortvideo';
}

View File

@@ -0,0 +1,44 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class TaskCard.
*
* @property string $title
* @property string $description
* @property string $url
* @property string $task_id
* @property array $btn
*/
class TaskCard extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'taskcard';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'url',
'task_id',
'btn',
];
}

View File

@@ -0,0 +1,52 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Text.
*
* @property string $content
*/
class Text extends Message
{
/**
* Message type.
*
* @var string
*/
protected $type = 'text';
/**
* Properties.
*
* @var array
*/
protected $properties = ['content'];
/**
* Text constructor.
*/
public function __construct(string $content)
{
parent::__construct(compact('content'));
}
/**
* @return array
*/
public function toXmlArray()
{
return [
'Content' => $this->get('content'),
];
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Text.
*
* @property string $title
* @property string $description
* @property string $url
*/
class TextCard extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'textcard';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'url',
];
}

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Transfer.
*
* @property string $to
* @property string $account
*/
class Transfer extends Message
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'transfer_customer_service';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'account',
];
/**
* Transfer constructor.
*/
public function __construct(string $account = null)
{
parent::__construct(compact('account'));
}
public function toXmlArray()
{
return empty($this->get('account')) ? [] : [
'TransInfo' => [
'KfAccount' => $this->get('account'),
],
];
}
}

View File

@@ -0,0 +1,62 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Video.
*
* @property string $video
* @property string $title
* @property string $media_id
* @property string $description
* @property string $thumb_media_id
*/
class Video extends Media
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'video';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'title',
'description',
'media_id',
'thumb_media_id',
];
/**
* Video constructor.
*/
public function __construct(string $mediaId, array $attributes = [])
{
parent::__construct($mediaId, 'video', $attributes);
}
public function toXmlArray()
{
return [
'Video' => [
'MediaId' => $this->get('media_id'),
'Title' => $this->get('title'),
'Description' => $this->get('description'),
],
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Messages;
/**
* Class Voice.
*
* @property string $media_id
*/
class Voice extends Media
{
/**
* Messages type.
*
* @var string
*/
protected $type = 'voice';
/**
* Properties.
*
* @var array
*/
protected $properties = [
'media_id',
'recognition',
];
}