123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\DB\Ddl;
- /**
- * @api
- * @since 100.0.2
- */
- class Trigger
- {
- /**#@+
- * Trigger times
- */
- const TIME_BEFORE = 'BEFORE';
- const TIME_AFTER = 'AFTER';
- /**#@-*/
- /**#@+
- * Trigger events
- */
- const EVENT_INSERT = 'INSERT';
- const EVENT_UPDATE = 'UPDATE';
- const EVENT_DELETE = 'DELETE';
- /**#@-*/
- /**#@-*/
- protected static $listOfTimes = [self::TIME_BEFORE, self::TIME_AFTER];
- /**
- * List of events available for trigger
- *
- * @var array
- */
- protected static $listOfEvents = [self::EVENT_INSERT, self::EVENT_UPDATE, self::EVENT_DELETE];
- /**
- * Name of trigger
- *
- * @var string
- */
- protected $name;
- /**
- * Time of trigger
- *
- * @var string
- */
- protected $time;
- /**
- * Time of trigger
- *
- * @var string
- */
- protected $event;
- /**
- * Table name
- *
- * @var string
- */
- protected $tableName;
- /**
- * List of statements for trigger body
- *
- * @var array
- */
- protected $statements = [];
- /**
- * Set trigger name
- *
- * @param string $name
- * @throws \InvalidArgumentException
- * @return \Magento\Framework\DB\Ddl\Trigger
- */
- public function setName($name)
- {
- if (!is_string($name)) {
- throw new \InvalidArgumentException(
- (string)new \Magento\Framework\Phrase('Trigger name should be a string')
- );
- }
- $this->name = strtolower($name);
- return $this;
- }
- /**
- * Retrieve name of trigger
- *
- * @throws \Zend_Db_Exception
- * @return string
- */
- public function getName()
- {
- if (empty($this->name)) {
- throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger name is not defined'));
- }
- return $this->name;
- }
- /**
- * Set trigger time
- *
- * @param string $time
- * @throws \InvalidArgumentException
- * @return \Magento\Framework\DB\Ddl\Trigger
- */
- public function setTime($time)
- {
- if (in_array($time, self::$listOfTimes)) {
- $this->time = strtoupper($time);
- } else {
- throw new \InvalidArgumentException((string)new \Magento\Framework\Phrase('Trigger unsupported time type'));
- }
- return $this;
- }
- /**
- * Retrieve time of trigger
- *
- * @throws \Zend_Db_Exception
- * @return string
- */
- public function getTime()
- {
- if ($this->time === null) {
- throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger time is not defined'));
- }
- return $this->time;
- }
- /**
- * Set trigger event
- *
- * @param string $event
- * @throws \InvalidArgumentException
- * @return \Magento\Framework\DB\Ddl\Trigger
- */
- public function setEvent($event)
- {
- if (in_array($event, self::$listOfEvents)) {
- $this->event = strtoupper($event);
- } else {
- throw new \InvalidArgumentException(
- (string)new \Magento\Framework\Phrase('Trigger unsupported event type')
- );
- }
- return $this;
- }
- /**
- * Retrieve event of trigger
- *
- * @throws \Zend_Db_Exception
- * @return string
- */
- public function getEvent()
- {
- if ($this->event === null) {
- throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger event is not defined'));
- }
- return $this->event;
- }
- /**
- * Set table name
- *
- * @param string $name
- * @throws \InvalidArgumentException
- * @return \Magento\Framework\DB\Ddl\Trigger
- */
- public function setTable($name)
- {
- if (!is_string($name)) {
- throw new \InvalidArgumentException(
- (string)new \Magento\Framework\Phrase('Trigger table name should be a string')
- );
- }
- $this->tableName = $name;
- return $this;
- }
- /**
- * Retrieve table name
- *
- * @throws \Zend_Db_Exception
- * @return string
- */
- public function getTable()
- {
- if (empty($this->tableName)) {
- throw new \Zend_Db_Exception((string)new \Magento\Framework\Phrase('Trigger table name is not defined'));
- }
- return $this->tableName;
- }
- /**
- * Add statement to trigger
- *
- * @param string $statement
- * @throws \InvalidArgumentException
- * @return \Magento\Framework\DB\Ddl\Trigger
- */
- public function addStatement($statement)
- {
- if (!is_string($statement)) {
- throw new \InvalidArgumentException(
- (string)new \Magento\Framework\Phrase('Trigger statement should be a string')
- );
- }
- $statement = trim($statement);
- $statement = rtrim($statement, ';') . ';';
- $this->statements[] = $statement;
- return $this;
- }
- /**
- * Retrieve list of statements of trigger
- *
- * @return array
- */
- public function getStatements()
- {
- return $this->statements;
- }
- /**
- * Retrieve list of times available for trigger
- *
- * @return array
- */
- public static function getListOfTimes()
- {
- return self::$listOfTimes;
- }
- /**
- * Retrieve list of events available for trigger
- *
- * @return array
- */
- public static function getListOfEvents()
- {
- return self::$listOfEvents;
- }
- }
|