PHP code example of chocochaos / propel-rulable-behavior

1. Go to this page and download the library: Download chocochaos/propel-rulable-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chocochaos / propel-rulable-behavior example snippets




namespace Chocochaos\SampleProject\Models\User\Rules;

use Chocochaos\SampleProject\Models\User\Map\UserGroupFunctionTableMap;
use Chocochaos\SampleProject\Models\User\UserGroupFunction;
use Chocochaos\SampleProject\Models\User\UserGroupFunctionQuery;
use Chocochaos\Rulable\RuleInterface;
use DateTime;
use LogicException;
use Propel\Runtime\ActiveQuery\BaseModelCriteria;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveRecord\ActiveRecordInterface;

/**
 * Class UserGroupFunctionIsActive
 *
 * @package Chocochaos\SampleProject\Models\User\Rules
 */
class UserGroupFunctionIsActive implements RuleInterface
{
    /**
     * @param ActiveRecordInterface $object
     *
     * @return bool
     */
    public function objectMeetsRule(ActiveRecordInterface $object): bool
    {
        if ($object instanceof UserGroupFunction) {
            if (!($object->getStart() instanceof DateTime
                && $object->getStart() <= new DateTime())) {
                return false;
            }
            if ($object->getEnd() instanceof DateTime
                && $object->getEnd() < new DateTime()) {
                return false;
            }

            return true;
        }

        throw new LogicException(
            sprintf(
                'The rule %s can only be applied to objects of type %s.',
                static::class,
                UserGroupFunction::class
            )
        );
    }

    /**
     * @param BaseModelCriteria $query
     *
     * @return BaseModelCriteria
     */
    public function filterByMeetsRule(
        BaseModelCriteria $query
    ): BaseModelCriteria {
        if ($query instanceof UserGroupFunctionQuery) {
            return $query
                ->condition(
                    'has_start_date',
                    UserGroupFunctionTableMap::COL_START . ' IS NOT NULL'
                )
                ->condition(
                    'start_date_in_past',
                    UserGroupFunctionTableMap::COL_START . ' <= NOW()'
                )
                ->condition(
                    'has_no_end_date',
                    UserGroupFunctionTableMap::COL_END . ' IS NULL'
                )
                ->condition(
                    'end_date_in_future',
                    UserGroupFunctionTableMap::COL_END . ' >= NOW()'
                )
                ->combine(
                    ['has_start_date', 'start_date_in_past'],
                    Criteria::LOGICAL_AND,
                    'start_date_valid'
                )
                ->combine(
                    ['has_no_end_date', 'end_date_in_future'],
                    Criteria::LOGICAL_OR,
                    'end_date_valid'
                )
                ->where(
                    ['start_date_valid', 'end_date_valid'],
                    Criteria::LOGICAL_AND
                );
        }

        throw new LogicException(
            sprintf(
                'The rule %s can only be applied to queries of type %s.',
                static::class,
                UserGroupFunctionQuery::class
            )
        );
    }

    /**
     * @param BaseModelCriteria $query
     *
     * @return BaseModelCriteria
     */
    public function filterByFailsRule(
        BaseModelCriteria $query
    ): BaseModelCriteria {
        if ($query instanceof UserGroupFunctionQuery) {
            return $query
                ->condition(
                    'has_no_start_date',
                    UserGroupFunctionTableMap::COL_START . ' IS NULL'
                )
                ->condition(
                    'start_date_in_future',
                    UserGroupFunctionTableMap::COL_START . ' > NOW()'
                )
                ->condition(
                    'has_end_date',
                    UserGroupFunctionTableMap::COL_END . ' IS NOT NULL'
                )
                ->condition(
                    'end_date_in_past',
                    UserGroupFunctionTableMap::COL_END . ' < NOW()'
                )
                ->combine(
                    ['has_no_start_date', 'start_date_in_future'],
                    Criteria::LOGICAL_OR,
                    'start_date_invalid'
                )
                ->combine(
                    ['has_end_date', 'end_date_in_past'],
                    Criteria::LOGICAL_AND,
                    'end_date_invalid'
                )
                ->where(
                    ['start_date_invalid', 'end_date_invalid'],
                    Criteria::LOGICAL_OR
                );
        }

        throw new LogicException(
            sprintf(
                'The rule %s can only be applied to queries of type %s.',
                static::class,
                UserGroupFunctionQuery::class
            )
        );
    }
}



namespace Chocochaos\SampleProject\Models\User\Rules;

use Chocochaos\SampleProject\Models\User\Map\UserGroupFunctionTableMap;
use Chocochaos\SampleProject\Models\User\UserGroupFunction;
use Chocochaos\SampleProject\Models\User\UserGroupFunctionQuery;
use DateTime;
use Propel\Runtime\ActiveQuery\Criteria;

/**
 * Class UserGroupFunctionIsActive
 *
 * @package Chocochaos\SampleProject\Models\User\Rules
 */
class UserGroupFunctionIsActive
{
    /**
     * @param UserGroupFunction $userGroupFunction
     *
     * @return bool
     */
    public function objectMeetsRule(UserGroupFunction $userGroupFunction): bool
    {
        if (!($userGroupFunction->getStart() instanceof DateTime
            && $userGroupFunction->getStart() <= new DateTime())) {
            return false;
        }
        if ($userGroupFunction->getEnd() instanceof DateTime
            && $userGroupFunction->getEnd() < new DateTime()) {
            return false;
        }

        return true;
    }

    /**
     * @param UserGroupFunctionQuery $query
     *
     * @return UserGroupFunctionQuery
     */
    public function filterByMeetsRule(
        UserGroupFunctionQuery $query
    ): UserGroupFunctionQuery {
        return $query
            ->condition(
                'has_start_date',
                UserGroupFunctionTableMap::COL_START . ' IS NOT NULL'
            )
            ->condition(
                'start_date_in_past',
                UserGroupFunctionTableMap::COL_START . ' <= NOW()'
            )
            ->condition(
                'has_no_end_date',
                UserGroupFunctionTableMap::COL_END . ' IS NULL'
            )
            ->condition(
                'end_date_in_future',
                UserGroupFunctionTableMap::COL_END . ' >= NOW()'
            )
            ->combine(
                ['has_start_date', 'start_date_in_past'],
                Criteria::LOGICAL_AND,
                'start_date_valid'
            )
            ->combine(
                ['has_no_end_date', 'end_date_in_future'],
                Criteria::LOGICAL_OR,
                'end_date_valid'
            )
            ->where(
                ['start_date_valid', 'end_date_valid'],
                Criteria::LOGICAL_AND
            );
    }

    /**
     * @param UserGroupFunctionQuery $query
     *
     * @return UserGroupFunctionQuery
     */
    public function filterByFailsRule(
        UserGroupFunctionQuery $query
    ): UserGroupFunctionQuery {
        return $query
            ->condition(
                'has_no_start_date',
                UserGroupFunctionTableMap::COL_START . ' IS NULL'
            )
            ->condition(
                'start_date_in_future',
                UserGroupFunctionTableMap::COL_START . ' > NOW()'
            )
            ->condition(
                'has_end_date',
                UserGroupFunctionTableMap::COL_END . ' IS NOT NULL'
            )
            ->condition(
                'end_date_in_past',
                UserGroupFunctionTableMap::COL_END . ' < NOW()'
            )
            ->combine(
                ['has_no_start_date', 'start_date_in_future'],
                Criteria::LOGICAL_OR,
                'start_date_invalid'
            )
            ->combine(
                ['has_end_date', 'end_date_in_past'],
                Criteria::LOGICAL_AND,
                'end_date_invalid'
            )
            ->where(
                ['start_date_invalid', 'end_date_invalid'],
                Criteria::LOGICAL_OR
            );
    }
}