|
| 1 | +<?xml version="1.0" encoding="UTF-8"?> |
| 2 | +<!DOCTYPE topic |
| 3 | + SYSTEM "https://resources.jetbrains.com/writerside/1.0/xhtml-entities.dtd"> |
| 4 | +<topic |
| 5 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 6 | + xsi:noNamespaceSchemaLocation="https://resources.jetbrains.com/writerside/1.0/topic.v2.xsd" |
| 7 | + title="Extending functionality" id="extending-functionality"> |
| 8 | + |
| 9 | + <link-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</link-summary> |
| 10 | + <card-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</card-summary> |
| 11 | + <web-summary>Extend feeds using conditional clauses and macros for Feed, FeedItem, and FeedInfo.</web-summary> |
| 12 | + |
| 13 | + <show-structure depth="3" /> |
| 14 | + |
| 15 | + <chapter title="Conditional clauses" id="conditional_clauses"> |
| 16 | + <note> |
| 17 | + Conditional clauses are available for <code>Feed</code>, <code>FeedInfo</code> and |
| 18 | + <code>FeedItem</code> classes. |
| 19 | + </note> |
| 20 | + |
| 21 | + <p> |
| 22 | + Sometimes, you may want certain feed clauses to apply to a feed based on another condition. |
| 23 | + For instance, you may only want to apply a where statement if a given input value is present or another attribute has a value. |
| 24 | + You can accomplish this using the <code>when</code> method: |
| 25 | + </p> |
| 26 | + |
| 27 | + <code-block lang="php" src="advanced-extends-conditional.php" include-lines="5-" /> |
| 28 | + |
| 29 | + <p> |
| 30 | + The <code>when</code> method only executes the given closure when the first argument is <code>true</code>. |
| 31 | + If the first argument is <code>false</code>, the closure will not be executed. |
| 32 | + So, in the example above, the closure given to the <code>when</code> method will only be invoked if the |
| 33 | + <code>category</code> field is present on the model and evaluates to <code>true</code>. |
| 34 | + </p> |
| 35 | + |
| 36 | + <p> |
| 37 | + You may pass another closure as the third argument to the <code>when</code> method. |
| 38 | + This closure will only execute if the first argument evaluates to <code>false</code>. |
| 39 | + To illustrate how this feature may be used, we will use it to configure the model title suffix: |
| 40 | + </p> |
| 41 | + |
| 42 | + <code-block lang="php" src="advanced-extends-conditional-both.php" include-lines="5-" /> |
| 43 | + |
| 44 | + <p> |
| 45 | + Additionally, the inverse method <code>unless</code> is available. |
| 46 | + In this case, the processing logic is the exact opposite. |
| 47 | + </p> |
| 48 | + |
| 49 | + <code-block lang="php" src="advanced-extends-conditional-unless.php" include-lines="5-" /> |
| 50 | + </chapter> |
| 51 | + |
| 52 | + <chapter title="Macroable" id="macroable"> |
| 53 | + <p> |
| 54 | + <code>Feed</code>, <code>FeedItem</code>, and <code>FeedInfo</code> are "macroable", |
| 55 | + which allows you to add additional methods to them at runtime. |
| 56 | + The <code>macro</code> method accepts a closure that will be executed when your macro is called. |
| 57 | + The macro closure may access the feed's other methods via <code>$this</code>, |
| 58 | + just as if it were a real method of the feed classes. |
| 59 | + </p> |
| 60 | + |
| 61 | + <p> |
| 62 | + For example, the following code adds a <code>customFilename</code> method to the <code>Feed</code> class: |
| 63 | + </p> |
| 64 | + |
| 65 | + <code-block lang="php" src="advanced-extends-macro-feed.php" include-lines="5-" /> |
| 66 | + |
| 67 | + <p> |
| 68 | + Additionally, here are examples for <code>FeedInfo</code> and <code>FeedItem</code>: |
| 69 | + </p> |
| 70 | + |
| 71 | + <code-block lang="php" src="advanced-extends-macro-info.php" include-lines="5-" /> |
| 72 | + <code-block lang="php" src="advanced-extends-macro-item.php" include-lines="5-" /> |
| 73 | + </chapter> |
| 74 | +</topic> |
0 commit comments